Graphene-django: Custom resolver to fetch data from methods

Created on 30 Sep 2016  路  1Comment  路  Source: graphql-python/graphene-django

Hi
I have a question. I cannot find any suitable example. Is there a possibility to add an extra field which will be returning data from particular method ? I mean I have a Node defined (node inherits from DjangoObjectType) and my model has some properties (method with @property decorator). I would like to be able to add this data into graphql response. I've tried with resolve_{name} method but there is no access to particular instance inside this method. Maybe I missed something.

Thanks, great project !

Most helpful comment

Hey, it's soo easy. I didn't notice that self in Node is just an object of our model.

So you need to only add field:
property = graphene.String()

and resolver:
def resolve_property(self, _args, *_kwargs):
return self.property

and it works like a charm.

>All comments

Hey, it's soo easy. I didn't notice that self in Node is just an object of our model.

So you need to only add field:
property = graphene.String()

and resolver:
def resolve_property(self, _args, *_kwargs):
return self.property

and it works like a charm.

Was this page helpful?
0 / 5 - 0 ratings