Get requested fields in resolve function
See original GitHub issueI have seen that the info
parameter in the resolve
function provides a info.field_asts
, which provides information about the selected fields, but when fragments are provided you get something like:
[Field(alias=None, name=Name(value=u'customer'), arguments=[], directives=[], selection_set=SelectionSet(selections=[Field(alias=None, name=Name(value=u'id'), arguments=[], directives=[], selection_set=None), FragmentSpread(name=Name(value=u'__RelayQueryFragment0wau8gf'), directives=[])]))]
which means for fragments we can’t really figure out which fields are selected at runtime.
Our use-case for knowing the fields in the resolve functions is,that we only want to calculate the fields that are actually requested because some of the fields are expensive to calculate.
Edit: Or are the resolve
methods for specific fields meant to be used for that? E.g. resolve_full_name
on the Customer
node?
Also happy to provide an example of that would make it easier.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:18 (11 by maintainers)
Top Results From Across the Web
How to get requested fields inside GraphQL resolver?
First, I need to get the requested fields. I can already get the whole query as a string. For example, in the resolver,...
Read more >GraphQL performance tip: Select fields from requests all the ...
I came across graphql-fields which can return the requested fields based on the info param, it also works with advanced cases like union...
Read more >Resolvers - Apollo GraphQL Docs
A resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data...
Read more >Determining which fields were requested by a query - gqlgen
CollectAllFields is the simplest way to get the set of queried fields. It will return a slice of strings of the field names...
Read more >Resolvers - graphql-compose
In terms of graphql-compose this field config is called as Resolver . ... wrap args , type , resolve (get resolver and create...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okay, I have a solution here 😃 A function that gives you a list of selected fields.
Goal
Our goal is to have a query like this:
and from within a resolver we want to produce the following list of selected fields:
['id', 'object', 'field']
notice that nested fields are not included:
object { id name }
is provided, but onlyobject
is mentioned.Basic implementation
This function simply goes through the AST at the current level, picks up all the fields, and returns their names as a list.
It can be used only in the most basic cases because:
... fragmentName
)... on Droid { }
)Usage:
A feature-complete implementation
This implementation has support for everything GraphQL itself supports because it relies on
context.collect_fields()
, but it’s also the slowest one, and it requires you to provide the runtime type in order to resolve fragments.Drawbacks:
graphql
has already evaluated it, but sadly, we don’t have access to that informationUsage:
The Combination of the Two
Since both functions are quite useful, here’s a function that combines the best of both:
License: MIT, or Beerware
After much work, here’s a much nicer code snippet to get requested fields:
https://gist.github.com/mixxorz/dc36e180d1888629cf33