-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
My question on SO has zero answers but describes the problem the best I think:
https://stackoverflow.com/questions/73650751/get-list-of-fields-to-resolve-from-a-graphql-resolver-supporting-include-direct
Summary:
Take this query:
query($userId: String!, $loadPhoto: Boolean = false) {
user(id: $userId) {
name
photo @include(if: $loadPhoto)
}
}
variables:
{
"userId": "2",
"loadPhoto": false
}
When implementing the resolver function for user, I want to know if photo is requested or not because the API I'm calling to fetch the user and optionally the user's photo is much more expensive if the photo should be included, but by checking the node list in the info object, it will always be there, regardless of whether the field will be excluded by a directive, so:
How can I find the child nodes of a node being resolved, after taking into account any @include directives etc?
The above query of course is for a single object so it could be achieved by having a custom resolver for user that delays execution using a DataLoader or similar, and then sets a flag inside a resolver function for the photo field. But it quickly becomes very complex, especially if the user is in turn part of a list of users, etc etc. I started an implementation using a DataLoader which had to group my requests fed to the loader in three steps to actually find what users I should resolve and what fields. It's simply just a mess.
So why not offer a way from the graphql module to get the evaluated list of child nodes?
It seems like
graphql-js/src/execution/execute.ts
Line 531 in a24a9f3
| const { fields: rootFields, patches } = collectFields( |
Or even simply extracted as a public utility function of the library for getting the child fields of a given field including any applied directives?