4

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()"?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129

1 Answers1

5

"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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129