-
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.
Added RELAY_CONNECTION_MAX_LIMIT and RELAY_CONNECTION_ENFORCE_FIRST_O…
…R_LAST settings Relay connections will be limited to 100 records by default.
- Loading branch information
1 parent
3803e9a
commit 46a1dde
Showing
4 changed files
with
161 additions
and
6 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
from ..compat import MissingType, JSONField | ||
from ..fields import DjangoConnectionField | ||
from ..types import DjangoObjectType | ||
from ..settings import graphene_settings | ||
from .models import Article, Reporter | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
@@ -452,3 +453,95 @@ class Query(graphene.ObjectType): | |
result = schema.execute(query) | ||
assert not result.errors | ||
assert result.data == expected | ||
|
||
|
||
def test_should_enforce_first_or_last(): | ||
graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = True | ||
|
||
class ReporterType(DjangoObjectType): | ||
|
||
class Meta: | ||
model = Reporter | ||
interfaces = (Node, ) | ||
|
||
class Query(graphene.ObjectType): | ||
all_reporters = DjangoConnectionField(ReporterType) | ||
|
||
r = Reporter.objects.create( | ||
first_name='John', | ||
last_name='Doe', | ||
email='[email protected]', | ||
a_choice=1 | ||
) | ||
|
||
schema = graphene.Schema(query=Query) | ||
query = ''' | ||
query NodeFilteringQuery { | ||
allReporters { | ||
edges { | ||
node { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
''' | ||
|
||
expected = { | ||
'allReporters': None | ||
} | ||
|
||
result = schema.execute(query) | ||
assert len(result.errors) == 1 | ||
assert str(result.errors[0]) == ( | ||
'You must provide a `first` or `last` value to properly ' | ||
'paginate the `allReporters` connection.' | ||
) | ||
assert result.data == expected | ||
|
||
|
||
def test_should_error_if_first_is_greater_than_max(): | ||
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 100 | ||
|
||
class ReporterType(DjangoObjectType): | ||
|
||
class Meta: | ||
model = Reporter | ||
interfaces = (Node, ) | ||
|
||
class Query(graphene.ObjectType): | ||
all_reporters = DjangoConnectionField(ReporterType) | ||
|
||
r = Reporter.objects.create( | ||
first_name='John', | ||
last_name='Doe', | ||
email='[email protected]', | ||
a_choice=1 | ||
) | ||
|
||
schema = graphene.Schema(query=Query) | ||
query = ''' | ||
query NodeFilteringQuery { | ||
allReporters(first: 101) { | ||
edges { | ||
node { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
''' | ||
|
||
expected = { | ||
'allReporters': None | ||
} | ||
|
||
result = schema.execute(query) | ||
assert len(result.errors) == 1 | ||
assert str(result.errors[0]) == ( | ||
'Requesting 101 records on the `allReporters` connection ' | ||
'exceeds the `first` limit of 100 records.' | ||
) | ||
assert result.data == expected | ||
|
||
graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False |