Open tony opened 2 years ago
Thanks for the very detailed issue. The workaround worked for me except for a specific case:
Let's say there is a Project
model which has many User
.
class ProjectType(FixRelayNodeResolutionMixin, DjangoObjectType):
class Meta:
model = Project
In the above example the wrong UserType
will be resolved even with the fix. The solution I found was to manually define and resolve UserType
on ProjectType
.
You can also create a separate django model that's a proxy of your Org model and use that as the model.
# models.py
class Org(models.Model):
...
class AnonymousOrg(Org):
class Meta:
proxy = True
# types.py
import graphene_django
from .models import Org as OrgModel, AnonymousOrg as AnonymousOrgModel
class Org(graphene_django.DjangoObjectType):
class Meta:
model = OrgModel
fields = (
"id",
"name",
"billing"
)
class AnonymousOrg(graphene_django.DjangoObjectType):
class Meta:
model = AnonymousOrgModel
fields = (
"id",
"name",
)
The only thing is there's a pitfall if there are many to many relationships and presumably many to one ie relationships that are set on the other model.
What is the Current Behavior?
Assume a fixed schema with two (or more) different GraphQL object types using
graphene_django.DjangoObjectType
linked to the same Django model:Assume a query to
Org
of ID7eca71ed-ff04-4473-9fd1-0a587705f885
.Response (incorrect):
It returns the other object type
'AnonymousOrg:7eca71ed-ff04-4473-9fd1-0a587705f885'
, despite the relay ID specifying it was anOrg
object.What is the Expected Behavior?
Should return the object type specified in the relay ID.
Return (expected):
Motivation / Use Case for Changing the Behavior
node(id: "")
based queries to handle object types based on the same Django model.Environment
History
May 24, 2020: Issue #971 posted just linking a complete description. While it's good to recreate it, the lack of description effectively made it unsearchable to many trying to look it up and hidden (StackOverflow posts and comments are being made and none of them cite any bug).
Feb 2, 2017: PR #104 by @Tritlo.
Feb 6, 2017: Bug reported by @nickhudkins #107.
Feb 12, 2017: #107 closed by @syrusakbary:
Feb 20, 2017: Replaced by #115 by @syrusakbary:
Merged to master https://github.com/graphql-python/graphene-django/commit/c635db5e5a83bb777c99514f06e3c906163eb57b.
However, no history of it remains in trunk. It seems to have been rebased out of master without any revert or explanation: docs/registry.rst is removed.
It's not clear what the registry does, but it looks like different issues are being convoluted with this one.
When a relay ID is passed, it should return the object of the type encoded in the ID, e.g.
This would return the GraphQL type
Org
. But instead it's not deterministic, it will return any GraphQL object type using the same model, and disregard the object type.Other
Workaround
Graphene 2
Version 1
@boolangery posted a workaround on May 25, 2020:
Version 2
Graphene 3
via August 19th, 2024, adaptation of above: