Graphene-django: Show Model Properties as Fields

Created on 20 Oct 2017  路  11Comments  路  Source: graphql-python/graphene-django

It should be possible to add Fields to a DjangoObjectType query, either on the actual ObjectType or by parsing @property from the model.

Most helpful comment

Automatically sending all @propertys along would be a large assumption. We have a few cases where we want @property attributes to show up in the schema, and we have just been able to do:

class MyObject(DjangoObjectType):

    name_of_property_on_model = graphene.String()
    ...

And then it is getting picked up automatically and doesn't need any custom resolving. Of course, you should use whichever graphene type matches the property's return value. Let me know if that works for you too.

All 11 comments

It is possible https://docs.python.org/3/library/inspect.html#inspect.isdatadescriptor
to do it manually, you can add the field how you want and in the resolver just return the property from self.

Yes, manually works fine, but I have Property on my base Model (for example an encrypted id) that I always want to transmit, can you elaborate on the isdatadescriptor?

https://www.pythonsheets.com/notes/python-basic.html#using-annotation-to-check-type
here is an example that might give you an idea on how to use inspect to get the behavior you want

Are you suggesting to subclass DjangoObjectType? Not sure how those links are helping me.

Automatically sending all @propertys along would be a large assumption. We have a few cases where we want @property attributes to show up in the schema, and we have just been able to do:

class MyObject(DjangoObjectType):

    name_of_property_on_model = graphene.String()
    ...

And then it is getting picked up automatically and doesn't need any custom resolving. Of course, you should use whichever graphene type matches the property's return value. Let me know if that works for you too.

hmm, no resolver necessary .. that's interesting. makes since tho after looking at the code.

That seems to work. Do you think there's an easy to extend the DjangoObjectType to automatically do that with properties?

@JulianSchneider

I haven't tried it myself, but yes, I would like to think you could subclass DjangoObjectType to add all properties. I would still encourage you not to as it is potentially dangerous when someone else comes along and adds a @property to a model, unknowingly exposing new data to the graphql endpoint.

If you primary use is to add a global property that is shared by all of your models, then I think the easiest and most future-proof solution would be to add a "mixin" to your objects. Something like this:

class EncryptedIdMixin(object):
    encrypted_id = graphene.String()

...

class MyObject(EncryptedIdMixin, DjangoObjectType):

    class Meta:
        ...

It's impossible to subclass DjangoObjectType currently (see #423)

Could we add it to the docs somewhere that django-graphene automatically maps property like described above? Very helpful @spockNinja :) Thanks.

Looks like we reached some resolution here. @lggwettmann we're working hard at improving the docs and we hope our new examples will make this clear.

Was this page helpful?
0 / 5 - 0 ratings