Skip to content

Commit

Permalink
Do not break when after is greater than list_length
Browse files Browse the repository at this point in the history
  • Loading branch information
bellini666 committed Jul 1, 2020
1 parent 8ddad41 commit 41d32d6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ def resolve_connection(cls, connection, args, iterable, max_limit=None):
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
Expand Down
37 changes: 37 additions & 0 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import base64

import pytest
from django.db import models
Expand Down Expand Up @@ -1084,6 +1085,42 @@ class Query(graphene.ObjectType):
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),
Expand Down

0 comments on commit 41d32d6

Please sign in to comment.