-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from graphql-python/fix-filter-and-resolver
Fixed FilterConnectionFields with limit in the FilterSet
- Loading branch information
Showing
5 changed files
with
296 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,13 @@ | |
|
||
if DJANGO_FILTER_INSTALLED: | ||
import django_filters | ||
from django_filters import FilterSet, NumberFilter | ||
|
||
from graphene_django.filter import (GlobalIDFilter, DjangoFilterConnectionField, | ||
GlobalIDMultipleChoiceFilter) | ||
from graphene_django.filter.tests.filters import ArticleFilter, PetFilter, ReporterFilter | ||
else: | ||
pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed')) | ||
pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed or not compatible')) | ||
|
||
pytestmark.append(pytest.mark.django_db) | ||
|
||
|
@@ -365,3 +367,170 @@ class Query(ObjectType): | |
all_reporters = DjangoFilterConnectionField(ReporterFilterNode) | ||
|
||
assert ReporterFilterNode._meta.fields['child_reporters'].node_type == ReporterFilterNode | ||
|
||
|
||
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(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.now(), | ||
reporter=r, | ||
editor=r, | ||
lang='es' | ||
) | ||
Article.objects.create( | ||
headline='Article Node 2', | ||
pub_date=datetime.now(), | ||
reporter=r, | ||
editor=r, | ||
lang='en' | ||
) | ||
|
||
schema = Schema(query=Query) | ||
query = ''' | ||
query NodeFilteringQuery { | ||
allReporters(limit: 1) { | ||
edges { | ||
node { | ||
id | ||
firstName | ||
articles(lang: "es") { | ||
edges { | ||
node { | ||
id | ||
lang | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
''' | ||
|
||
expected = { | ||
'allReporters': { | ||
'edges': [{ | ||
'node': { | ||
'id': 'UmVwb3J0ZXJUeXBlOjI=', | ||
'firstName': 'John', | ||
'articles': { | ||
'edges': [{ | ||
'node': { | ||
'id': 'QXJ0aWNsZVR5cGU6MQ==', | ||
'lang': 'ES' | ||
} | ||
}] | ||
} | ||
} | ||
}] | ||
} | ||
} | ||
|
||
result = schema.execute(query) | ||
assert not result.errors | ||
assert result.data == expected | ||
|
||
|
||
def test_should_query_filter_node_double_limit_raises(): | ||
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 Query(ObjectType): | ||
all_reporters = DjangoFilterConnectionField( | ||
ReporterType, | ||
filterset_class=ReporterFilter | ||
) | ||
|
||
def resolve_all_reporters(self, args, context, info): | ||
return Reporter.objects.order_by('a_choice')[:2] | ||
|
||
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 | ||
) | ||
|
||
schema = Schema(query=Query) | ||
query = ''' | ||
query NodeFilteringQuery { | ||
allReporters(limit: 1) { | ||
edges { | ||
node { | ||
id | ||
firstName | ||
} | ||
} | ||
} | ||
} | ||
''' | ||
|
||
result = schema.execute(query) | ||
assert len(result.errors) == 1 | ||
assert str(result.errors[0]) == ( | ||
'Received two sliced querysets (high mark) in the connection, please slice only in one.' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ class Meta: | |
model = Reporter | ||
only_fields = ('id', ) | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
reporter = graphene.Field(ReporterType) | ||
|
||
|
@@ -360,7 +359,96 @@ 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 |