From 41d32d6d21828eaf78b04e91a071d5fb9d84381b Mon Sep 17 00:00:00 2001 From: Thiago Bellini Ribeiro Date: Wed, 1 Jul 2020 10:15:39 -0300 Subject: [PATCH] Do not break when after is greater than list_length --- graphene_django/fields.py | 5 +++- graphene_django/tests/test_query.py | 37 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 641f423..67559aa 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -144,7 +144,10 @@ class DjangoConnectionField(ConnectionField): min(max_limit, list_length) if max_limit is not None else list_length ) - after = get_offset_with_default(args.get("after"), -1) + 1 + # If after is higher than list_length, connection_from_list_slice + # would try to do a negative slicing which makes django throw an + # AssertionError + after = min(get_offset_with_default(args.get("after"), -1) + 1, list_length) if max_limit is not None and "first" not in args: args["first"] = max_limit diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 0860a4a..3881ed8 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -1,4 +1,5 @@ import datetime +import base64 import pytest from django.db import models @@ -1084,6 +1085,42 @@ def test_should_resolve_get_queryset_connectionfields(): assert result.data == expected +def test_connection_should_limit_after_to_list_length(): + reporter_1 = Reporter.objects.create( + first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1 + ) + reporter_2 = Reporter.objects.create( + first_name="Some", last_name="Guy", email="someguy@cnn.com", a_choice=1 + ) + + class ReporterType(DjangoObjectType): + class Meta: + model = Reporter + interfaces = (Node,) + + class Query(graphene.ObjectType): + all_reporters = DjangoConnectionField(ReporterType) + + schema = graphene.Schema(query=Query) + query = """ + query ReporterPromiseConnectionQuery ($after: String) { + allReporters(first: 1 after: $after) { + edges { + node { + id + } + } + } + } + """ + + after = base64.b64encode(b"arrayconnection:10").decode() + result = schema.execute(query, variable_values=dict(after=after)) + expected = {"allReporters": {"edges": []}} + assert not result.errors + assert result.data == expected + + REPORTERS = [ dict( first_name="First {}".format(i),