Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds enhanced support for proxy models. #603

Merged
merged 6 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions graphene_django/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def __init__(self, *args, **kwargs):
self.__class__ = CNNReporter


class CNNReporterManager(models.Manager):
def get_queryset(self):
return super(CNNReporterManager, self).get_queryset().filter(reporter_type=2)


class CNNReporter(Reporter):
"""
This class is a proxy model for Reporter, used for testing
Expand All @@ -74,6 +79,8 @@ class CNNReporter(Reporter):
class Meta:
proxy = True

objects = CNNReporterManager()


class Article(models.Model):
headline = models.CharField(max_length=100)
Expand Down
86 changes: 22 additions & 64 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import datetime

import pytest
Expand All @@ -7,6 +8,7 @@

from django.db.models import Q

from graphql_relay import to_global_id
import graphene
from graphene.relay import Node

Expand Down Expand Up @@ -895,8 +897,7 @@ class Query(graphene.ObjectType):

def test_proxy_model_support():
"""
This test asserts that we can query for all Reporters,
even if some are of a proxy model type at runtime.
This test asserts that we can query for all Reporters and proxied Reporters.
"""

class ReporterType(DjangoObjectType):
Expand All @@ -905,11 +906,17 @@ class Meta:
interfaces = (Node,)
use_connection = True

reporter_1 = Reporter.objects.create(
class CNNReporterType(DjangoObjectType):
class Meta:
model = CNNReporter
interfaces = (Node,)
use_connection = True

reporter = Reporter.objects.create(
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
)

reporter_2 = CNNReporter.objects.create(
cnn_reporter = CNNReporter.objects.create(
first_name="Some",
last_name="Guy",
email="someguy@cnn.com",
Expand All @@ -919,6 +926,7 @@ class Meta:

class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(ReporterType)
cnn_reporters = DjangoConnectionField(CNNReporterType)

schema = graphene.Schema(query=Query)
query = """
Expand All @@ -930,63 +938,7 @@ class Query(graphene.ObjectType):
}
}
}
}
"""

expected = {
"allReporters": {
"edges": [
{"node": {"id": "UmVwb3J0ZXJUeXBlOjE="}},
{"node": {"id": "UmVwb3J0ZXJUeXBlOjI="}},
]
}
}

result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_proxy_model_fails():
"""
This test asserts that if you try to query for a proxy model,
that query will fail with:
GraphQLError('Expected value of type "CNNReporterType" but got:
CNNReporter.',)

This is because a proxy model has the identical model definition
to its superclass, and defines its behavior at runtime, rather than
at the database level. Currently, filtering objects of the proxy models'
type isn't supported. It would require a field on the model that would
represent the type, and it doesn't seem like there is a clear way to
enforce this pattern across all projects
"""

class CNNReporterType(DjangoObjectType):
class Meta:
model = CNNReporter
interfaces = (Node,)
use_connection = True

reporter_1 = Reporter.objects.create(
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
)

reporter_2 = CNNReporter.objects.create(
first_name="Some",
last_name="Guy",
email="someguy@cnn.com",
a_choice=1,
reporter_type=2, # set this guy to be CNN
)

class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(CNNReporterType)

schema = graphene.Schema(query=Query)
query = """
query ProxyModelQuery {
allReporters {
cnnReporters {
edges {
node {
id
Expand All @@ -999,11 +951,17 @@ class Query(graphene.ObjectType):
expected = {
"allReporters": {
"edges": [
{"node": {"id": "UmVwb3J0ZXJUeXBlOjE="}},
{"node": {"id": "UmVwb3J0ZXJUeXBlOjI="}},
{"node": {"id": to_global_id("ReporterType", reporter.id)}},
{"node": {"id": to_global_id("ReporterType", cnn_reporter.id)}},
]
},
"cnnReporters": {
"edges": [
{"node": {"id": to_global_id("CNNReporterType", cnn_reporter.id)}}
]
}
}

result = schema.execute(query)
assert result.errors
assert not result.errors
assert result.data == expected
6 changes: 5 additions & 1 deletion graphene_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ def is_type_of(cls, root, info):
if not is_valid_django_model(type(root)):
raise Exception(('Received incompatible instance "{}".').format(root))

model = root._meta.model._meta.concrete_model
if cls._meta.model._meta.proxy:
model = root._meta.model
else:
model = root._meta.model._meta.concrete_model

return model == cls._meta.model

@classmethod
Expand Down