Combine DjangoObjectType and ObjectType

怎甘沉沦 提交于 2019-12-24 09:08:50

问题


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']

回答1:


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]


来源:https://stackoverflow.com/questions/48267583/combine-djangoobjecttype-and-objecttype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!