Skip to content

Fix distinct bug #290

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

Merged
merged 5 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def get_manager(self):

@classmethod
def merge_querysets(cls, default_queryset, queryset):
if default_queryset.query.distinct and not queryset.query.distinct:
queryset = queryset.distinct()
elif queryset.query.distinct and not default_queryset.query.distinct:
default_queryset = default_queryset.distinct()
return queryset & default_queryset

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions graphene_django/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class FilmDetails(models.Model):


class Film(models.Model):
genre = models.CharField(max_length=2, help_text='Genre', choices=[
('do', 'Documentary'),
('ot', 'Other')
], default='ot')
reporters = models.ManyToManyField('Reporter',
related_name='films')

Expand Down
66 changes: 62 additions & 4 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.utils.functional import SimpleLazyObject
from py.test import raises

from django.db.models import Q

import graphene
from graphene.relay import Node

Expand All @@ -17,6 +19,8 @@
Article,
CNNReporter,
Reporter,
Film,
FilmDetails,
)

pytestmark = pytest.mark.django_db
Expand Down Expand Up @@ -431,6 +435,60 @@ class Query(graphene.ObjectType):
assert result.data == expected


@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
reason="django-filter should be installed")
def test_should_query_node_filtering_with_distinct_queryset():
class FilmType(DjangoObjectType):

class Meta:
model = Film
interfaces = (Node, )
filter_fields = ('genre',)

class Query(graphene.ObjectType):
films = DjangoConnectionField(FilmType)

# def resolve_all_reporters_with_berlin_films(self, args, context, info):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry last one :) Is this needed?

I guess after resolving this @spockNinja or @syrusakbary can merge :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a way to integrate it with the existing test suite but I didn't see a straightforward way to construct a query that would lead to one "half" of the resulting query being distinct and the other not. Doing it in this way keeps the addition localized to where it is needed. If you have a suggestion on how to integrate it with the existing model instead please let me know!

# return Reporter.objects.filter(Q(films__film__location__contains="Berlin") | Q(a_choice=1))

def resolve_films(self, info, **args):
return Film.objects.filter(Q(details__location__contains="Berlin") | Q(genre__in=['ot'])).distinct()

f = Film.objects.create(
)
fd = FilmDetails.objects.create(
location="Berlin",
film=f
)

schema = graphene.Schema(query=Query)
query = '''
query NodeFilteringQuery {
films {
edges {
node {
genre
}
}
}
}
'''

expected = {
'films': {
'edges': [{
'node': {
'genre': 'OT'
}
}]
}
}

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


@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
reason="django-filter should be installed")
def test_should_query_node_multiple_filtering():
Expand Down Expand Up @@ -676,7 +734,7 @@ class Query(graphene.ObjectType):

def resolve_all_reporters(self, info, **args):
return Promise.resolve([Reporter(id=1)])

schema = graphene.Schema(query=Query)
query = '''
query ReporterPromiseConnectionQuery {
Expand Down Expand Up @@ -724,7 +782,7 @@ class Query(graphene.ObjectType):

def resolve_all_reporters(self, info, **args):
return Reporter.objects.all()

schema = graphene.Schema(query=Query)
query = '''
query ReporterLastQuery {
Expand Down Expand Up @@ -779,7 +837,7 @@ class Query(graphene.ObjectType):

def resolve_all_reporters(self, info, **args):
return Reporter.objects.all()

schema = graphene.Schema(query=Query)
query = '''
query ReporterLastQuery {
Expand Down Expand Up @@ -1012,7 +1070,7 @@ 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:
GraphQLError('Expected value of type "CNNReporterType" but got:
CNNReporter.',)

This is because a proxy model has the identical model definition
Expand Down