How to access parent field parameters within child resolver
See original GitHub issueI’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:
- Created 6 years ago
- Comments:5
Top 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 >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
For anyone facing a similar issue, I worked out how to do this.
self.id
holds the ID of theFormType
object.For example, the following would be a replacement for the
resolve_visits
method above:@pawarchinmay27 @athlordJojo @mglraimundo Please open a new issue as mine has been solved here.