mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-12 17:22:20 +03:00
Fix hasNextPage - revert to count
This commit is contained in:
parent
c00203499b
commit
6bc0f6d9d6
|
@ -56,8 +56,8 @@ def test_should_query_field():
|
||||||
assert result.data == expected
|
assert result.data == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("max_limit,does_count", [(None, True), (100, False)])
|
@pytest.mark.parametrize("max_limit", [None, 100])
|
||||||
def test_should_query_nested_field(graphene_settings, max_limit, does_count):
|
def test_should_query_nested_field(graphene_settings, max_limit):
|
||||||
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
|
||||||
|
|
||||||
r1 = Reporter(last_name="ABA")
|
r1 = Reporter(last_name="ABA")
|
||||||
|
@ -117,18 +117,11 @@ def test_should_query_nested_field(graphene_settings, max_limit, does_count):
|
||||||
assert not result.errors
|
assert not result.errors
|
||||||
query = str(Reporter.objects.order_by("pk")[:1].query)
|
query = str(Reporter.objects.order_by("pk")[:1].query)
|
||||||
assert result.data["__debug"]["sql"][0]["rawSql"] == query
|
assert result.data["__debug"]["sql"][0]["rawSql"] == query
|
||||||
if does_count:
|
|
||||||
assert "COUNT" in result.data["__debug"]["sql"][1]["rawSql"]
|
assert "COUNT" in result.data["__debug"]["sql"][1]["rawSql"]
|
||||||
assert "tests_reporter_pets" in result.data["__debug"]["sql"][2]["rawSql"]
|
assert "tests_reporter_pets" in result.data["__debug"]["sql"][2]["rawSql"]
|
||||||
assert "COUNT" in result.data["__debug"]["sql"][3]["rawSql"]
|
assert "COUNT" in result.data["__debug"]["sql"][3]["rawSql"]
|
||||||
assert "tests_reporter_pets" in result.data["__debug"]["sql"][4]["rawSql"]
|
assert "tests_reporter_pets" in result.data["__debug"]["sql"][4]["rawSql"]
|
||||||
assert len(result.data["__debug"]["sql"]) == 5
|
assert len(result.data["__debug"]["sql"]) == 5
|
||||||
else:
|
|
||||||
assert len(result.data["__debug"]["sql"]) == 3
|
|
||||||
for i in range(len(result.data["__debug"]["sql"])):
|
|
||||||
assert "COUNT" not in result.data["__debug"]["sql"][i]["rawSql"]
|
|
||||||
assert "tests_reporter_pets" in result.data["__debug"]["sql"][1]["rawSql"]
|
|
||||||
assert "tests_reporter_pets" in result.data["__debug"]["sql"][2]["rawSql"]
|
|
||||||
|
|
||||||
assert result.data["reporter"] == expected["reporter"]
|
assert result.data["reporter"] == expected["reporter"]
|
||||||
|
|
||||||
|
@ -175,8 +168,8 @@ def test_should_query_list():
|
||||||
assert result.data == expected
|
assert result.data == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("max_limit,does_count", [(None, True), (100, False)])
|
@pytest.mark.parametrize("max_limit", [None, 100])
|
||||||
def test_should_query_connection(graphene_settings, max_limit, does_count):
|
def test_should_query_connection(graphene_settings, max_limit):
|
||||||
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
|
||||||
|
|
||||||
r1 = Reporter(last_name="ABA")
|
r1 = Reporter(last_name="ABA")
|
||||||
|
@ -219,20 +212,14 @@ def test_should_query_connection(graphene_settings, max_limit, does_count):
|
||||||
)
|
)
|
||||||
assert not result.errors
|
assert not result.errors
|
||||||
assert result.data["allReporters"] == expected["allReporters"]
|
assert result.data["allReporters"] == expected["allReporters"]
|
||||||
if does_count:
|
|
||||||
assert len(result.data["__debug"]["sql"]) == 2
|
assert len(result.data["__debug"]["sql"]) == 2
|
||||||
assert "COUNT" in result.data["__debug"]["sql"][0]["rawSql"]
|
assert "COUNT" in result.data["__debug"]["sql"][0]["rawSql"]
|
||||||
query = str(Reporter.objects.all()[:1].query)
|
query = str(Reporter.objects.all()[:1].query)
|
||||||
assert result.data["__debug"]["sql"][1]["rawSql"] == query
|
assert result.data["__debug"]["sql"][1]["rawSql"] == query
|
||||||
else:
|
|
||||||
assert len(result.data["__debug"]["sql"]) == 1
|
|
||||||
assert "COUNT" not in result.data["__debug"]["sql"][0]["rawSql"]
|
|
||||||
query = str(Reporter.objects.all()[:1].query)
|
|
||||||
assert result.data["__debug"]["sql"][0]["rawSql"] == query
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("max_limit,does_count", [(None, True), (100, False)])
|
@pytest.mark.parametrize("max_limit", [None, 100])
|
||||||
def test_should_query_connectionfilter(graphene_settings, max_limit, does_count):
|
def test_should_query_connectionfilter(graphene_settings, max_limit):
|
||||||
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = max_limit
|
||||||
|
|
||||||
from ...filter import DjangoFilterConnectionField
|
from ...filter import DjangoFilterConnectionField
|
||||||
|
@ -278,13 +265,7 @@ def test_should_query_connectionfilter(graphene_settings, max_limit, does_count)
|
||||||
)
|
)
|
||||||
assert not result.errors
|
assert not result.errors
|
||||||
assert result.data["allReporters"] == expected["allReporters"]
|
assert result.data["allReporters"] == expected["allReporters"]
|
||||||
if does_count:
|
|
||||||
assert len(result.data["__debug"]["sql"]) == 2
|
assert len(result.data["__debug"]["sql"]) == 2
|
||||||
assert "COUNT" in result.data["__debug"]["sql"][0]["rawSql"]
|
assert "COUNT" in result.data["__debug"]["sql"][0]["rawSql"]
|
||||||
query = str(Reporter.objects.all()[:1].query)
|
query = str(Reporter.objects.all()[:1].query)
|
||||||
assert result.data["__debug"]["sql"][1]["rawSql"] == query
|
assert result.data["__debug"]["sql"][1]["rawSql"] == query
|
||||||
else:
|
|
||||||
assert len(result.data["__debug"]["sql"]) == 1
|
|
||||||
assert "COUNT" not in result.data["__debug"]["sql"][0]["rawSql"]
|
|
||||||
query = str(Reporter.objects.all()[:1].query)
|
|
||||||
assert result.data["__debug"]["sql"][0]["rawSql"] == query
|
|
||||||
|
|
|
@ -129,25 +129,26 @@ class DjangoConnectionField(ConnectionField):
|
||||||
@classmethod
|
@classmethod
|
||||||
def resolve_connection(cls, connection, args, iterable, max_limit=None):
|
def resolve_connection(cls, connection, args, iterable, max_limit=None):
|
||||||
iterable = maybe_queryset(iterable)
|
iterable = maybe_queryset(iterable)
|
||||||
# When slicing from the end, need to retrieve the iterable length.
|
|
||||||
if args.get("last"):
|
|
||||||
max_limit = None
|
|
||||||
if isinstance(iterable, QuerySet):
|
if isinstance(iterable, QuerySet):
|
||||||
_len = max_limit or iterable.count()
|
list_length = iterable.count()
|
||||||
|
list_slice_length = max_limit or list_length
|
||||||
else:
|
else:
|
||||||
_len = max_limit or len(iterable)
|
list_length = len(iterable)
|
||||||
|
list_slice_length = max_limit or list_length
|
||||||
|
|
||||||
connection = connection_from_list_slice(
|
connection = connection_from_list_slice(
|
||||||
iterable,
|
iterable,
|
||||||
args,
|
args,
|
||||||
slice_start=0,
|
slice_start=0,
|
||||||
list_length=_len,
|
list_length=list_length,
|
||||||
list_slice_length=_len,
|
list_slice_length=list_slice_length,
|
||||||
connection_type=connection,
|
connection_type=connection,
|
||||||
edge_type=connection.Edge,
|
edge_type=connection.Edge,
|
||||||
pageinfo_type=PageInfo,
|
pageinfo_type=PageInfo,
|
||||||
)
|
)
|
||||||
connection.iterable = iterable
|
connection.iterable = iterable
|
||||||
connection.length = _len
|
connection.length = list_length
|
||||||
return connection
|
return connection
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -1126,6 +1126,44 @@ def test_should_return_max_limit(graphene_settings):
|
||||||
assert len(result.data["allReporters"]["edges"]) == 4
|
assert len(result.data["allReporters"]["edges"]) == 4
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_have_next_page(graphene_settings):
|
||||||
|
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 4
|
||||||
|
reporters = [Reporter(**kwargs) for kwargs in REPORTERS]
|
||||||
|
Reporter.objects.bulk_create(reporters)
|
||||||
|
|
||||||
|
class ReporterType(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = Reporter
|
||||||
|
interfaces = (Node,)
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
all_reporters = DjangoConnectionField(ReporterType)
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query)
|
||||||
|
# Need first: 4 here to trigger the `has_next_page` logic in graphql-relay
|
||||||
|
# See `arrayconnection.py::connection_from_list_slice`:
|
||||||
|
# has_next_page=isinstance(first, int) and end_offset < upper_bound
|
||||||
|
query = """
|
||||||
|
query AllReporters {
|
||||||
|
allReporters(first: 4) {
|
||||||
|
pageInfo {
|
||||||
|
hasNextPage
|
||||||
|
}
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = schema.execute(query)
|
||||||
|
assert not result.errors
|
||||||
|
assert len(result.data["allReporters"]["edges"]) == 4
|
||||||
|
assert result.data["allReporters"]["pageInfo"]["hasNextPage"]
|
||||||
|
|
||||||
|
|
||||||
def test_should_preserve_prefetch_related(django_assert_num_queries):
|
def test_should_preserve_prefetch_related(django_assert_num_queries):
|
||||||
class ReporterType(DjangoObjectType):
|
class ReporterType(DjangoObjectType):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1172,7 +1210,7 @@ def test_should_preserve_prefetch_related(django_assert_num_queries):
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
schema = graphene.Schema(query=Query)
|
schema = graphene.Schema(query=Query)
|
||||||
with django_assert_num_queries(2) as captured:
|
with django_assert_num_queries(3) as captured:
|
||||||
result = schema.execute(query)
|
result = schema.execute(query)
|
||||||
assert not result.errors
|
assert not result.errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user