2

I am trying to make a simple query with graphene-django but i can not get the DB, it gives me null.

I think the code is ok, what is going wrong, I am working for hours on it.

Do you have any idea, what it is?

Thanks in advance


import graphene
from graphene_django.types import DjangoObjectType, ObjectType
from myProject.models import Times


class TimesType(DjangoObjectType):
    class Meta:
        model=Times
        fields="__all__"


class Query(ObjectType):
    today_times = graphene.Field(TimesType, id=graphene.ID())
    all_times = graphene.List(TimesType)
   
    def resolve_todaytimes(self, info, id=None):
        return Times.objects.get(pk=id)

    def resolve_alltimes(root, info, **kwargs):
        return Times.objects.all()


schema = graphene.Schema(query=Query, mutation=Mutation)

query {todayTimes(id:"1029"){id}}

{
  "data": {
    "todayTimes": null
  }
}

Khan Khan
  • 119
  • 9

1 Answers1

2

The resolver method should be named in resolve_<FieldName> format

class Query(ObjectType):
    today_times = graphene.Field(TimesType, id=graphene.ID())
    all_times = graphene.List(TimesType)
   
    def resolve_today_times(self, info, id=None): # not `resolve_todaytimes`
        return Times.objects.get(pk=id)

    def resolve_all_times(root, info, **kwargs): # not `resolve_alltimes`
        return Times.objects.all()

Alternatively, you can use the resolver parameter to set the callable resolver as,

def resolve_todaytimes(self, info, id=None):
    return Times.objects.get(pk=id)


def resolve_alltimes(root, info, **kwargs):
    return Times.objects.all()


class Query(ObjectType):
    today_times = graphene.Field(
        TimesType,
        id=graphene.ID(),
        resolver=resolve_todaytimes
    )
    all_times = graphene.List(
        TimesType,
        resolver=resolve_alltimes
    )
JPG
  • 82,442
  • 19
  • 127
  • 206