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

Set first amount to max limit if not set #993

Merged
merged 1 commit into from
Jun 25, 2020
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
3 changes: 3 additions & 0 deletions graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def resolve_connection(cls, connection, args, iterable, max_limit=None):

after = get_offset_with_default(args.get("after"), -1) + 1

if max_limit is not None and "first" not in args:
args["first"] = max_limit

connection = connection_from_list_slice(
iterable[after:],
args,
Expand Down
7 changes: 2 additions & 5 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ class Query(graphene.ObjectType):


def test_should_have_next_page(graphene_settings):
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 6
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 4
reporters = [Reporter(**kwargs) for kwargs in REPORTERS]
Reporter.objects.bulk_create(reporters)
db_reporters = Reporter.objects.all()
Expand All @@ -1141,9 +1141,6 @@ class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(ReporterType)

schema = graphene.Schema(query=Query)
# Need first: 4 here to trigger the `has_next_page` logic in graphql-relay
# See `arrayconnection.py::connection_from_list_slice`:
# has_next_page=isinstance(first, int) and end_offset < upper_bound
query = """
query AllReporters($first: Int, $after: String) {
allReporters(first: $first, after: $after) {
Expand All @@ -1160,7 +1157,7 @@ class Query(graphene.ObjectType):
}
"""

result = schema.execute(query, variable_values=dict(first=4))
result = schema.execute(query, variable_values={})
assert not result.errors
assert len(result.data["allReporters"]["edges"]) == 4
assert result.data["allReporters"]["pageInfo"]["hasNextPage"]
Expand Down