Skip to content

Do not break when after is greater than list_length #999

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

Merged
merged 1 commit into from
Jul 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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