Merge pull request #82 from gamingexperience/fix/node-filtering

Fixes filtering in nested nodes.
This commit is contained in:
Syrus Akbary 2017-01-14 18:35:52 -08:00 committed by GitHub
commit 2d1ab4b92a
2 changed files with 84 additions and 0 deletions

View File

@ -49,6 +49,7 @@ class DjangoConnectionField(ConnectionField):
iterable = default_manager
iterable = maybe_queryset(iterable)
if isinstance(iterable, QuerySet):
iterable &= maybe_queryset(default_manager)
_len = iterable.count()
else:
_len = len(iterable)

View File

@ -8,6 +8,7 @@ from py.test import raises
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
@ -281,3 +282,85 @@ def test_should_query_connectionfields():
}]
}
}
@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