Graphene-django: DjangoObjectType duplicate models breaks Relay node resolution

Created on 6 Feb 2017  路  3Comments  路  Source: graphql-python/graphene-django

If you define the following two types, or types that follow this pattern (Same model, different names). The relay node resolver resolves the incorrect name (and therefore incorrect globalID) for the type registered 2nd.

class ThingA(DjangoObjectType):
    class Meta:
        model = Thing
        name = 'ThingA'
        interfaces = (relay.Node, )

class ThingB(DjangoObjectType):
    class Meta:
        model = Thing
        name = 'ThingB'
        interfaces = (relay.Node, )

Query (ThingA globalID) :

{
  node(id: "GhpbmdBOjE=") {
    id // "GhpbmdBOjE=" Correct!
  }
}

Query (ThingB globalID)

{
  node(id: "VGhpbmdCOjE=") {
    id // "GhpbmdBOjE=" Incorrect :(
  }
}

It appears that there is some commented out code to warn about this. However, it is commented out so I am unsure what the intention of the author was.

I am really just looking to see if this is "as designed". @syrusakbary

Most helpful comment

Right now you can make this work with using a new registry for the second definition.

from graphene_django.registry import Registry

class ThingB(DjangoObjectType):
    class Meta:
        #
        registry = Registry()

Also, this issue #104 might be related :)

All 3 comments

Right now you can make this work with using a new registry for the second definition.

from graphene_django.registry import Registry

class ThingB(DjangoObjectType):
    class Meta:
        #
        registry = Registry()

Also, this issue #104 might be related :)

Right now you can make this work with using a new registry for the second definition.

from graphene_django.registry import Registry

class ThingB(DjangoObjectType):
    class Meta:
        #
        registry = Registry()

Also, this issue #104 might be related :)

Hi @syrusakbary ,

May i know how to implement the below structure and make it works, thank you very much.

model:

class A(models.Model):
    field1 = models.CharField()
    field2 = models.CharField()

class B(models.Model):
    a = models.ForeignKey(A)

scheme type:

class AType1(DjangoObjectType):
    class Meta:
        model = A
        only_fields = 'field1'

class AType2(DjangoObjectType):
    class Meta:
        model = A
        only_fields = 'field2'


class BType1(DjangoObjectType):
    class Meta:
        model = B
        only_fields = ('a__field1')  // just return the foreign object field1

class BType2(DjangoObjectType):
    class Meta:
        model = B
        only_fields = ('a__field2')  // just return the foreign object field2

class Query(graphene.ObjectType):
    b1 = graphene.List(BType1)
    b2 = graphene.List(BType2)
    ...

None of the solutions above seem to work any more - I am on graphene==2.1.8 and graphene-django==2.11.0

Does anyone have any suggestions of what else I can try? This is currently blocking a fairly major feature for us.

Specifically I'm trying to get this query to output the same IDs for users and publicUsers:

{
   viewer {
       users {
          id
       }
       publicUsers {
          id
       }
   }
}

where

class PublicUser(DjangoObjectType):
    class Meta:
        model = UserModel
        interfaces = (graphene.relay.Node,)

class User(DjangoObjectType):
    class Meta:
        model = UserModel
        interfaces = (graphene.relay.Node,)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hyusetiawan picture hyusetiawan  路  4Comments

licx picture licx  路  3Comments

BrianChapman picture BrianChapman  路  3Comments

MythicManiac picture MythicManiac  路  3Comments

x9sheikh picture x9sheikh  路  4Comments