Fork me on GitHub
#graphql
<
2021-03-05
>
chrisulloa19:03:54

Has anyone encountered an issue with Lacinia where if you have arguments to a field, the resolver is asynchronous, and the parent resolver is asynchronous, the parent and child execute in parallel? I have a child resolver that depends on the parent to pass down context values, but when the child is asynchronous and has arguments the execution order is not what I expect it to be.

chrisulloa19:03:06

I’m thinking maybe this is part of the GraphQL spec, but I haven’t been able to track it down.

chrisulloa19:03:24

Normally it waits for the parent to resolve before resolving the child even when both are async, but adding arguments to the field resolver causes that to change.

emccue20:03:23

graphql lets resolvers work in parallel

emccue20:03:59

i think its part of the spec that order of execution for queries is undefined

chrisulloa21:03:23

gotcha makes sense, thanks

chrisulloa21:03:16

This is the part of the Lacinia docs I read that I thought suggested it would happen in the order I stated above

chrisulloa21:03:24

https://lacinia.readthedocs.io/en/latest/resolve/selections.html

In execution order, resolution occurs top to bottom, so the hero selection occurs first, then (potentially in parallel) friends, home_planet, and (hero) name. These last two are leaf nodes, because they are scalar values. The list of characters (from the friends field) then has its name field selected. The result map then constructs from bottom to top.

mafcocinco21:03:06

is there a key in the context (or maybe something in the selection protocols) that allows a resolver to find out what query/mutation/subscription it was invoked from?

mafcocinco21:03:21

preferably something that is part of the public api.

hlship02:03:14

Nothing like this currently exists. I’m very curious how this would be useful.

hlship02:03:35

Generally, if children need information from a higher level, a parent resolver puts data into the context that is passed down to children.

mafcocinco14:03:33

yeah, that will work. Can just leave bread crumbs that way.

hlship18:03:05

Also, if it's a parent resolver and immediate child resolver, then just stuff the extra data into the resolved value (probably using namespaced keys to avoid any conflicts). It's only when operating at a further remove that the context approach makes sense.

hlship18:03:37

An example: we collect a search term from a field argument in our root query operation; that goes into the context, because we need it a couple of layers deeper when we're filtering items by the search key. There's (in our app) layers of orders, and item groups, between the query operation resolver, and the matching items resolver that actually needs the search key.

thumbsup_all 3
mafcocinco18:03:32

excellent suggestion.