Skip to content

[WIP] Fixes DjangoFilterConnectionField filter slices being ignored with a custom resolver #126

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
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
6 changes: 4 additions & 2 deletions graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def connection_resolver(resolver, connection, default_manager, root, args, conte
iterable = maybe_queryset(iterable)
if isinstance(iterable, QuerySet):
if iterable is not default_manager:
iterable &= maybe_queryset(default_manager)
_len = iterable.count()
iterable = list(set(iterable).intersection(maybe_queryset(default_manager)))
_len = len(iterable)
else:
_len = iterable.count()
else:
_len = len(iterable)
connection = connection_from_list_slice(
Expand Down
198 changes: 196 additions & 2 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from django.utils.functional import SimpleLazyObject
from py.test import raises

from django_filters import FilterSet, NumberFilter

import graphene
from graphene.relay import Node

from ..utils import DJANGO_FILTER_INSTALLED
from ..compat import MissingType, JSONField
from ..fields import DjangoConnectionField
from ..filter.fields import DjangoFilterConnectionField
from ..types import DjangoObjectType
from .models import Article, Reporter

Expand Down Expand Up @@ -42,7 +45,6 @@ class Meta:
model = Reporter
only_fields = ('id', )


class Query(graphene.ObjectType):
reporter = graphene.Field(ReporterType)

Expand Down Expand Up @@ -360,7 +362,199 @@ class Query(graphene.ObjectType):
}]
}
}


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():
class ReporterType(DjangoObjectType):

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

class ArticleType(DjangoObjectType):

class Meta:
model = Article
interfaces = (Node, )
filter_fields = ('lang', 'headline')

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

r = Reporter.objects.create(
first_name='John',
last_name='Doe',
email='[email protected]',
a_choice=1
)
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 3',
pub_date=datetime.date.today(),
reporter=r,
editor=r,
lang='en'
)

schema = graphene.Schema(query=Query)
query = '''
query NodeFilteringQuery {
allReporters {
edges {
node {
id
articles(lang: "es", headline: "Article Node 1") {
edges {
node {
id
}
}
}
}
}
}
}
'''

expected = {
'allReporters': {
'edges': [{
'node': {
'id': 'UmVwb3J0ZXJUeXBlOjE=',
'articles': {
'edges': [{
'node': {
'id': 'QXJ0aWNsZVR5cGU6MQ=='
}
}]
}
}
}]
}
}

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


def test_should_query_filter_node_limit():
class ReporterFilter(FilterSet):
limit = NumberFilter(method='filter_limit')

def filter_limit(self, queryset, name, value):
return queryset[:value]

class Meta:
model = Reporter
fields = ['first_name', ]

class ReporterType(DjangoObjectType):

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

class ArticleType(DjangoObjectType):

class Meta:
model = Article
interfaces = (Node, )
filter_fields = ('lang', )

class Query(graphene.ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType,
filterset_class=ReporterFilter
)

def resolve_all_reporters(self, args, context, info):
return Reporter.objects.order_by('a_choice')

Reporter.objects.create(
first_name='Bob',
last_name='Doe',
email='[email protected]',
a_choice=2
)
r = Reporter.objects.create(
first_name='John',
last_name='Doe',
email='[email protected]',
a_choice=1
)

Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
reporter=r,
editor=r,
lang='en'
)

schema = graphene.Schema(query=Query)
query = '''
query NodeFilteringQuery {
allReporters(limit: 1) {
edges {
node {
id
articles(lang: "es") {
edges {
node {
id
}
}
}
}
}
}
}
'''

expected = {
'allReporters': {
'edges': [{
'node': {
'id': 'UmVwb3J0ZXJUeXBlOjE=',
'articles': {
'edges': [{
'node': {
'id': 'QXJ0aWNsZVR5cGU6MQ=='
}
}]
}
}
}]
}
}

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