Graphene: relay.Node.Field returning null

Created on 28 Sep 2017  路  3Comments  路  Source: graphql-python/graphene

I have a super simple schema/model that behaves somewhat unexpectedly.

_allUsers_ and _allOrgs_ queries return correct results.

_viewer_ and _org_ return null.

Bonus issue: the commented resolvers (if I uncomment them) _resolve_viewer_ and _resolve_org_ don't even get called, but they _resolve_all_orgs_ does.

import graphene
from graphene import relay
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Org
from django.contrib.auth.models import User

from graphene_django import DjangoObjectType
import graphene

class UserNode(DjangoObjectType):
    class Meta:
        model = User
        filter_fields = {
            'id': ['exact'],
        }
        interfaces = (graphene.relay.Node, )

class OrgNode(DjangoObjectType):
    class Meta:
        model = Org
        interfaces = (graphene.relay.Node, )


class Query(graphene.AbstractType):
    all_users = DjangoFilterConnectionField(UserNode)
    all_orgs = DjangoFilterConnectionField(OrgNode)
    viewer = relay.Node.Field(UserNode)
    org = relay.Node.Field(OrgNode)

    # def resolve_viewer(self, args, context, info):
    #     print ("resolve_viewer", args, context)
    #     return User.objects.get(pk=1)
    #
    # def resolve_org(self, args, context, info):
    #     print ("resolve_org", args, context)
    #     return Org.objects.get(pk=1)
    #
    # def resolve_all_orgs(self, args, context, info):
    #     print ("resolve_all_orgs", args, context)
    #     return Org.objects.all()

Queries (I tried supplying both integers and strings as id's):

query v{
  viewer(id:1){
    id
  }

}

query o{
  org(id:"2"){
    name
  }
}

query users{
allUsers{
    edges{
      node{
        id
      }
    }
  }
}


query orgs{
  allOrgs{
    edges{
      node{
        id
      }
    }
  }
}

And the results:

{
  "data": {
    "viewer": null
  }
}

{
  "data": {
    "org": null
  }
}

{
  "data": {
    "allUsers": {
      "edges": [
        {
          "node": {
            "id": "VXNlck5vZGU6MQ=="
          }
        },
        {
          "node": {
            "id": "VXNlck5vZGU6Mg=="
          }
        }
      ]
    }
  }
}


{
  "data": {
    "allOrgs": {
      "edges": [
        {
          "node": {
            "id": "T3JnTm9kZTox"
          }
        },
        {
          "node": {
            "id": "T3JnTm9kZToy"
          }
        }
      ]
    }
  }
}

Most helpful comment

I figured out. It's returning null because id of the model has to be GraphQL idnot the Django id.

All 3 comments

I figured out. It's returning null because id of the model has to be GraphQL idnot the Django id.

@mraak thank you so much for this issue, mentioning that the id used for a relay.Node.Field() has to be a GraphQL id (base64 encoded MyType:idofthetype) and that you created a project readme with useful information. I was trying to get the relay.Node.Field working on an object type to lookup a related object based on some parent information and you made me realise that instead of using:

course(id: "c1cd7622-9a69-4aa7-b0e1-e69f8a150a10") {
  name
}}

I would have to use

node(id:"Q291cnNlVHlwZTpjMWNkNzYyMi05YTY5LTRhYTctYjBlMS1lNjlmOGExNTBhMTA=") {
  ... on CourseType{
    name
  }
}

for this kind of field 馃憤

https://github.com/graphql-python/graphene/issues/552#issuecomment-333738311

where I can find the graphql id?

Was this page helpful?
0 / 5 - 0 ratings