From 150fc82a4ed23e097b6444451da3530287900f24 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Thu, 25 Jun 2020 13:01:17 +0100 Subject: [PATCH] Set first amount to max limit if not set --- graphene_django/fields.py | 3 +++ graphene_django/tests/test_query.py | 7 ++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index ac7ce45d0..641f423ea 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -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, diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 64f54bb46..0860a4a6b 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -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() @@ -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) { @@ -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"]