Skip to content

Fix Bug causing DjangoFilterConnectionField to ignore annotations, .reverse() #315

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
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
2 changes: 1 addition & 1 deletion graphene_django/filter/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def merge_querysets(default_queryset, queryset):
low = default_queryset.query.low_mark or queryset.query.low_mark
high = default_queryset.query.high_mark or queryset.query.high_mark
default_queryset.query.clear_limits()
queryset = default_queryset & queryset
queryset = queryset & default_queryset
queryset.query.set_limits(low, high)
return queryset

Expand Down
138 changes: 137 additions & 1 deletion graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import pytest

from graphene import Field, ObjectType, Schema, Argument, Float
from graphene import Field, ObjectType, Schema, Argument, Float, Boolean, String
from graphene.relay import Node
from graphene_django import DjangoObjectType
from graphene_django.forms import (GlobalIDFormField,
GlobalIDMultipleChoiceField)
from graphene_django.tests.models import Article, Pet, Reporter
from graphene_django.utils import DJANGO_FILTER_INSTALLED

# for annotation test
from django.db.models import TextField, Value
from django.db.models.functions import Concat

pytestmark = []

if DJANGO_FILTER_INSTALLED:
Expand Down Expand Up @@ -534,3 +538,135 @@ def resolve_all_reporters(self, info, **args):
assert str(result.errors[0]) == (
'Received two sliced querysets (high mark) in the connection, please slice only in one.'
)

def test_order_by_is_perserved():
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node, )
filter_fields = ()

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterType, reverse_order=Boolean())

def resolve_all_reporters(self, info, reverse_order=False, **args):
reporters = Reporter.objects.order_by('first_name')

if reverse_order:
return reporters.reverse()

return reporters

Reporter.objects.create(
first_name='b',
)
r = Reporter.objects.create(
first_name='a',
)

schema = Schema(query=Query)
query = '''
query NodeFilteringQuery {
allReporters(first: 1) {
edges {
node {
firstName
}
}
}
}
'''
expected = {
'allReporters': {
'edges': [{
'node': {
'firstName': 'a',
}
}]
}
}

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


reverse_query = '''
query NodeFilteringQuery {
allReporters(first: 1, reverseOrder: true) {
edges {
node {
firstName
}
}
}
}
'''

reverse_expected = {
'allReporters': {
'edges': [{
'node': {
'firstName': 'b',
}
}]
}
}

reverse_result = schema.execute(reverse_query)

assert not reverse_result.errors
assert reverse_result.data == reverse_expected

def test_annotation_is_perserved():
class ReporterType(DjangoObjectType):
full_name = String()

def resolve_full_name(instance, info, **args):
return instance.full_name

class Meta:
model = Reporter
interfaces = (Node, )
filter_fields = ()

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterType)

def resolve_all_reporters(self, info, **args):
return Reporter.objects.annotate(
full_name=Concat('first_name', Value(' '), 'last_name', output_field=TextField())
)

Reporter.objects.create(
first_name='John',
last_name='Doe',
)

schema = Schema(query=Query)

query = '''
query NodeFilteringQuery {
allReporters(first: 1) {
edges {
node {
fullName
}
}
}
}
'''
expected = {
'allReporters': {
'edges': [{
'node': {
'fullName': 'John Doe',
}
}]
}
}

result = schema.execute(query)

assert not result.errors
assert result.data == expected