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

Fixes filtering in nested nodes. #82

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def connection_resolver(resolver, connection, default_manager, root, args, conte
iterable = default_manager
iterable = maybe_queryset(iterable)
if isinstance(iterable, QuerySet):
iterable &= maybe_queryset(default_manager)
_len = iterable.count()
else:
_len = len(iterable)
Expand Down
83 changes: 83 additions & 0 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import graphene
from graphene.relay import Node

from ..utils import DJANGO_FILTER_INSTALLED
from ..compat import MissingType, RangeField
from ..fields import DjangoConnectionField
from ..types import DjangoObjectType
Expand Down Expand Up @@ -281,3 +282,85 @@ def resolve_all_reporters(self, args, context, info):
}]
}
}


@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
reason="django-filter should be installed")
def test_should_query_node_filtering():
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 = DjangoConnectionField(ReporterType)

r = Reporter.objects.create(
first_name='John',
last_name='Doe',
email='johndoe@example.com',
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 {
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