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.

How to access parent field parameters within child resolver

See original GitHub issue

I’m trying to build a schema using Graphene that will support the following example query:

{
  form(id:1) {
    visits(start:1498863600, end: 1498950000) {
      total
    }
  }
}

The expected result from this would be something like this:

{
  "data": {
    "form": {
      "visits": {
        "total": 1123
      }
    }
  }
}

So far, I’ve come up with a schema that’s similar to the following. However I’m not sure how to access the id parameter on the form field from within the visits resolver. I need this id parameter in the visits resolver as I need to retrieve sessions from the DB for a specific form and between a time range (start and end).

import graphene
from lib import DBI


class ResultSetType(graphene.ObjectType):
    total = graphene.Int()

    def resolve_total(self, args, context, info):
        return 1000  # Hardcoded example


class FormType(graphene.ObjectType):
    id = graphene.ID()
    visits = graphene.Field(ResultSetType)

    def resolve_visits(self, args, context, info):
        """
        I understand that here I can access the 'start' and 'end'
        parameters for this field using args.get('start'), but how
        do I access the 'id' parameter from the parent 'form' field?
        
        My naive attempt of using`args.get('id')` returns `None`
        """
        return DBI.get_form_sessions(form_id=args.get('id'), start_ts=args.get('start'), end_ts=args.get('end'))


class RootType(graphene.ObjectType):
    form = graphene.Field(FormType, id=graphene.ID())

    def resolve_form(self, args, context, info):
        return FormType(id=args.get('id'))

How do I access parent field parameters from within a child field resolver?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

6reactions
JoeNylandcommented, Jul 28, 2017

For anyone facing a similar issue, I worked out how to do this. self.id holds the ID of the FormType object.

For example, the following would be a replacement for the resolve_visits method above:

    def resolve_visits(self, args, context, info):
        return DBI.get_form_sessions(form_id=self.id, start_ts=args.get('start'), end_ts=args.get('end'))
0reactions
JoeNylandcommented, Sep 25, 2019

@pawarchinmay27 @athlordJojo @mglraimundo Please open a new issue as mine has been solved here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access arguments on child resolver from parent call in ...
The GraphQL arguments you are accessing via $ctx. args are available at the field level. So when you access them when resolving the...
Read more >
Resolvers - Apollo GraphQL Docs
A resolver can optionally accept four positional arguments: (parent, args, contextValue, info) . Learn more about these arguments. · The args argument is...
Read more >
How to get the args of the parent resolver in the Apollo Server?
Query Schema: messages(userId: ID!): Blah! type Blah { totalCount: Int! } I'm using the userId in the totalCount resolver.
Read more >
Use the GraphQL Parent Object in a Child Resolver | egghead.io
GraphQL resolvers are nested. Data resolved out of a parent resolver can be accessed by a child resolver. This is useful when you...
Read more >
Resolvers – GraphQL Tools
Resolver Function Signature · obj : The object that contains the result returned from the resolver on the parent field, or, in the...
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