mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-22 01:27:01 +03:00
Do not break when after is greater than list_length (#999)
This commit is contained in:
parent
8ddad41bb7
commit
d50955a173
|
@ -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
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in New Issue
Block a user