jmunsch
jmunsch

Reputation: 24089

return django model property field in graphene graphql query

How to add a @property field from a django model graphql results?:

class MyModel(...):
    ...
    @property
    def some_property(self):
        return 'property here'
class MyObject(DjangoObjectType):
    class Meta:
        model = MyModel
        interfaces = (relay.Node,)
        filter_fields = []
query GetMyModel(id: ID!){
    getMyModel(id: $id){
        someProperty
    }
}

Upvotes: 2

Views: 810

Answers (1)

jmunsch
jmunsch

Reputation: 24089

An example by adding the some_property field:

class MyObject(DjangoObjectType):

    some_property = graphene.String()

    class Meta:
        model = MyModel
        interfaces = (relay.Node,)
        filter_fields = []

query:

query GetMyModel(id: ID!){
    getMyModel(id: $id){
        someProperty
    }
}

input:

{"variables": {"id": btoa("MyObject:123")}}

Upvotes: 3

Related Questions