Hello I simply want to avoid repeating code for each query, and I was wondering if I could call a method from inside a resolver a such:
# pseudo code
class Query(graphene.ObjectType):
field = graphene.Field(SomeType)
def do_boring_task(parent, info, arg):
return "I did something"
def resolve_field(parent, info):
did_something = parent.do_boring_task(arg) # <-- is this possible ?
# do something here
return resolved_fields
I always get a "graphql.error.located_error.GraphQLLocatedError: 'NoneType' object has no attribute 'do_boring_task'" error
Is it possible to do that the way I described it, or is this something that should be done using middleware?
Thanks
Classes inheriting from graphene.ObjectType
are different than a normal class, resolve_field
and do_boring_task
are by default static methods.
Note that first argument of resolve_field
is parent
and not self
, do_boring_task
is a classmethod (static method in other languages) of Query class and does not exist in parent schema, that explains your error.
A quick fix for your problem is as shown below, define the function, outside of the class.
def do_boring_task(args):
return "I did something"
class Query(graphene.ObjectType):
field = graphene.Field(SomeType)
def resolve_field(parent, info):
did_something = do_boring_task(arg)
# do something here
return resolved_fields
Refer these in the blog for more details
Implicit Static Method
Resolvers Outside the class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With