question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to check for requested output in resolver

See original GitHub issue

Is it possible to check what is being requested in the resolver for a query? For example, if I have the following schema:

type Query {
    getUser(username: ID){
        email: String
        phone: String
    }
}

Can I create a resolver like this?

...
@query.field("getUser")
def get_user(obj, info, username):
    requested_data = ... # This is what I'm looking for
    response = {}
    if "email" in requested_data:
        response["email"] = get_email(username)
    if "phone" in requested_data:
        response["phone"] = get_phone(username)
    return response

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rafalpcommented, Sep 13, 2019

Closing this one. Feel free to ping me back @tbsf if you feel there’s something else to add.

Potentially we could have a guide on our docs showing how one can introspect which fields are asked in resolver to, say, eagerly fetch data needed in child resolver

0reactions
andreasnuessleincommented, Mar 12, 2020

Adding some information for other people that stumble here:

First of all, this is probably what you’re looking for:

for field_node in info.field_nodes:
    print([f.name.value for f in field_node.selection_set.selections])

# or more to the point:
fields = set()
for fnode in info.field_nodes:
    fields.update({f.name.value for f in fnode.selection_set.selections})

“Why the loop around the inner (more obvious) loop” you might ask…

Taking this example

query {
  getUsername(id:2) {
    email
    phone
  }
  getUsername(id:2) {
    phone
  }
  getSomethingElse(id:5) {
    field_a
    field_b
  }
}
def getUsername(obj: Any, info: GraphQLResolveInfo):
    for fnode in info.field_nodes:
        print([f.name.value for f in fnode.selection_set.selections])

will output:

['email', 'phone']                                                                                                                                                          
['phone']
# but not 
['field_a', 'field_b'] # from getSomethingElse

Which a) is kinda efficient - Ariadne doesn’t call the getUserName twice but rather once with all the required arguments and b) means only the field_nodes that actually map to getUserName are in this list.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolver Output and Voltage - Dynapar Encoders
Check the input voltage. If the resolver input voltage is not high enough, the output voltage will be low. Check the resolver for...
Read more >
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 >
Resolvers - Apollo GraphQL Docs
Apollo Server needs to know how to populate data for every field in your schema so that it can respond to requests for...
Read more >
TRACE RESOLVER - IBM
When the trace resolver output has been collected, check the following in the trace output: Fix or check any problems reported at the...
Read more >
Test and debug resolvers (VTL) - AWS AppSync
When a GraphQL resolver is invoked, it contains a context object that has relevant information about the request for you to program against....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found