Search code examples
pythonpython-3.xdjangographqlgraphene-django

DjangoListField() vs graphene.List() in Graphene Django


I used both "DjangoListField()" and "graphene.List()" with the Resolver to list all objects.

"DjangoListField()" in schema.py:

import graphene
from graphene_django import DjangoObjectType
from graphene_django import DjangoListField
from .models import Category

class CategoryType(DjangoObjectType):
    class Meta:
        model = Category
        fields = ("id","name")

class Query(graphene.ObjectType):

    all_categories = DjangoListField(CategoryType) # DjangoListField()

    def resolve_all_categories(root, info): # Resolver to list all objects
        return Category.objects.all()

schema = graphene.Schema(query=Query)

"graphene.List()" in schema.py:

import graphene
from graphene_django import DjangoObjectType
from .models import Category

class CategoryType(DjangoObjectType):
    class Meta:
        model = Category
        fields = ("id","name")

class Query(graphene.ObjectType):

    all_categories = graphene.List(CategoryType) # graphene.List()

    def resolve_all_categories(root, info): # Resolver to list all objects
        return Category.objects.all()

schema = graphene.Schema(query=Query)

Then, I queried "allCategories" for both code in schema.py above one by one:

query {
  allCategories {
    id
    name
  }
}

But the result is the same to list all objects:

{
  "data": {
    "allCategories": [
      {
        "id": "1",
        "name": "category1"
      },
      {
        "id": "2",
        "name": "category2"
      }
    ]
  }
}

What is the difference between "DjangoListField()" and "graphene.List()"?


Solution

  • "DjangoListField()" has the Default Resolver to list all objects but "graphene.List()" doesn't have it so for "graphene.List()", you need to explicitly define the Resolver to list all objects otherwise you get "null".

    So, if you remove Resolver from your code with "DjangoListField()":

    import graphene
    from graphene_django import DjangoObjectType
    from graphene_django import DjangoListField
    from .models import Category
    
    class CategoryType(DjangoObjectType):
        class Meta:
            model = Category
            fields = ("id","name")
    
    class Query(graphene.ObjectType):
    
        all_categories = DjangoListField(CategoryType) # DjangoListField()
    
    schema = graphene.Schema(query=Query)
    

    Then, you query "allCategories":

    query {
      allCategories {
        id
        name
      }
    }
    

    Finally, you can still list all objects:

    {
      "data": {
        "allCategories": [
          {
            "id": "1",
            "name": "category1"
          },
          {
            "id": "2",
            "name": "category2"
          }
        ]
      }
    }
    

    But, if you remove Resolver from your code with "graphene.List()":

    import graphene
    from graphene_django import DjangoObjectType
    from .models import Category
    
    class CategoryType(DjangoObjectType):
        class Meta:
            model = Category
            fields = ("id","name")
    
    class Query(graphene.ObjectType):
    
        all_categories = graphene.List(CategoryType) # graphene.List()
    
    schema = graphene.Schema(query=Query)
    

    Then, you query "allCategories":

    query {
      allCategories {
        id
        name
      }
    }
    

    Finally, you cannot list all objects instead you get "null":

    {
      "data": {
        "allCategories": null
      }
    }
    

    You can get more detail about "DjangoListField()" in this page DjangoListField.