Skip to content

Commit d50955a

Browse files
authored
Do not break when after is greater than list_length (#999)
1 parent 8ddad41 commit d50955a

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

graphene_django/fields.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ def resolve_connection(cls, connection, args, iterable, max_limit=None):
144144
min(max_limit, list_length) if max_limit is not None else list_length
145145
)
146146

147-
after = get_offset_with_default(args.get("after"), -1) + 1
147+
# If after is higher than list_length, connection_from_list_slice
148+
# would try to do a negative slicing which makes django throw an
149+
# AssertionError
150+
after = min(get_offset_with_default(args.get("after"), -1) + 1, list_length)
148151

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

graphene_django/tests/test_query.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import base64
23

34
import pytest
45
from django.db import models
@@ -1084,6 +1085,42 @@ class Query(graphene.ObjectType):
10841085
assert result.data == expected
10851086

10861087

1088+
def test_connection_should_limit_after_to_list_length():
1089+
reporter_1 = Reporter.objects.create(
1090+
first_name="John", last_name="Doe", email="johndoe@example.com", a_choice=1
1091+
)
1092+
reporter_2 = Reporter.objects.create(
1093+
first_name="Some", last_name="Guy", email="someguy@cnn.com", a_choice=1
1094+
)
1095+
1096+
class ReporterType(DjangoObjectType):
1097+
class Meta:
1098+
model = Reporter
1099+
interfaces = (Node,)
1100+
1101+
class Query(graphene.ObjectType):
1102+
all_reporters = DjangoConnectionField(ReporterType)
1103+
1104+
schema = graphene.Schema(query=Query)
1105+
query = """
1106+
query ReporterPromiseConnectionQuery ($after: String) {
1107+
allReporters(first: 1 after: $after) {
1108+
edges {
1109+
node {
1110+
id
1111+
}
1112+
}
1113+
}
1114+
}
1115+
"""
1116+
1117+
after = base64.b64encode(b"arrayconnection:10").decode()
1118+
result = schema.execute(query, variable_values=dict(after=after))
1119+
expected = {"allReporters": {"edges": []}}
1120+
assert not result.errors
1121+
assert result.data == expected
1122+
1123+
10871124
REPORTERS = [
10881125
dict(
10891126
first_name="First {}".format(i),

0 commit comments

Comments
 (0)