Search code examples
djangographqlgraphene-python

Combine DjangoObjectType and ObjectType


I have a simple django model with a calculated property field clicks. The model looks like this:

class Link(models.Model):
    url = models.URLField()

    @property
    def clicks(self):
        """
        Property does some calculations and returns a list of dictionaries:

        """
        # removed calculation for simplicity
        return [{'dt': 1, 'clicks': 100}, {'dt': 2, 'clicks': 201}] 

I want to make this model accesible in my graphql endpoint. So I created the following Types and Query:

class Stats(graphene.ObjectType):
    clicks = graphene.String()
    dt = graphene.String()


class LinkType(DjangoObjectType):
    clicks = graphene.List(Stats, source='clicks')

    class Meta:
        model = Link


class Query(object):
    link = graphene.Field(LinkType, id=graphene.Int())

    def resolve_link(self, info, **kwargs):
        id = kwargs.get('id')
        url = kwargs.get('url')
        if id is not None:
            return Link.objects.get(pk=id)
        return None

Now I should be able to use the following query in my graphql explorer:

{
  link(id: 3) {
    id,
    url,
    clicks{
      clicks,
      dt
    }
  }
}

My expected result would be like this:

{
  id: 3,
  url: "www.google.de",
  clicks: [
    dt: 1, clicks: 100},
    dt: 2, clicks: 201}
  ]
}

But the nested values of clicks and dt are null:

{
  id: 3,
  url: "www.google.de",
  clicks: [
    dt: null, clicks: null},
    dt: null, clicks: null}
  ]
}

So what am I doing wrong here? How can I convert a list of dicts to an ObjectType in graphene?

EDIT:

I used a modified version of @mark-chackerian answer to solve the problem: Seems like I was expecting too much "magic" from graphene and I have to explicitly tell it how every field is resolved.

class Stats(graphene.ObjectType):
    clicks = graphene.String()
    dt = graphene.String()

    def resolve_clicks(self, info):
        return self['clicks']

    def resolve_dt(self, info):
        return self['dt']

Solution

  • You have to tell graphene more explicitly how to make your list of Stats objects.

    Try something like this:

    class LinkType(DjangoObjectType):
        clicks = graphene.List(Stats)
    
        class Meta:
            model = Link
    
        def resolve_clicks(self, info):
            return [Stats(dt=click_dict['dt'], clicks=click_dict['clicks') for click_dict in self.clicks]