Search code examples
pythondjangographqlgraphene-python

how to use aliases in graphene-django


I'm using python 3.5 with

Django==1.11.6
graphene==2.0.dev20170802065539
graphene-django==2.0.dev2017083101
graphql-core==2.0.dev20171009101843
graphql-relay==0.4.5

I have a schema that fetch's single objects like this:

class Query(graphene.AbstractType):
   story = graphene.Field(storyType, category=graphene.String(), id=graphene.Int())
   def resolve_story(self, info, **kwargs):
        category = kwargs.get('category')
        id = kwargs.get('id')
        if category is not None:
            return models.story.objects.get(category=models.category.objects.get(name=category))
        if id is not None:
            return models.story.objects.get(pk=id)
        return None

My problem is that I can't use both story(category:"category") and (id:"id") in one query. I read here that I should use aliases but I don't know how. Thank's for any help.


Solution

  • Is as simple as the example just:

    {
      storyId: story(id: 1) {
        name
      }
      storyCategory: story(category: "category_name") {
        name
      }
    }
    

    but you can use filter if that is what you mean doing something like:

            if category and id is not None:
                return models.story.objects.filter(id=id, category=models.category.objects.get(name=category))
    

    and in the query something like:

    story(id: 1, name: "category_name") {
            name
          }