Skip to content

Commit 048c57d

Browse files
authored
Fix calculation of has_next_page in resolve_connection_from_cache (#622)
1 parent 98393a3 commit 048c57d

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

Diff for: strawberry_django/relay.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,15 @@ def resolve_connection_from_cache(
188188
for node in result
189189
]
190190
has_previous_page = (
191-
nodes[0]._strawberry_row_number > 1 # type: ignore
191+
result[0]._strawberry_row_number > 1 # type: ignore
192+
if result
193+
else False
194+
)
195+
has_next_page = (
196+
result[-1]._strawberry_row_number < result[-1]._strawberry_total_count # type: ignore
192197
if result
193198
else False
194199
)
195-
has_next_page = result._strawberry_row_number < result if result else False
196200

197201
return cls(
198202
edges=edges,

Diff for: tests/relay/test_nested_pagination.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pytest
2+
from strawberry.relay import to_base64
3+
from strawberry.relay.types import PREFIX
4+
5+
from strawberry_django.optimizer import DjangoOptimizerExtension
6+
from tests import utils
7+
from tests.projects.faker import IssueFactory, MilestoneFactory
8+
9+
10+
@pytest.mark.django_db(transaction=True)
11+
def test_nested_pagination(gql_client: utils.GraphQLTestClient):
12+
# Nested pagination with the same arguments for the parent and child connections
13+
query = """
14+
query testNestedConnectionPagination($first: Int, $after: String) {
15+
milestoneConn(first: $first, after: $after) {
16+
edges {
17+
node {
18+
id
19+
issuesWithFilters(first: $first, after: $after) {
20+
edges {
21+
node {
22+
id
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
"""
31+
32+
# Create 4 milestones, each with 4 issues
33+
nested_data = {
34+
milestone: IssueFactory.create_batch(4, milestone=milestone)
35+
for milestone in MilestoneFactory.create_batch(4)
36+
}
37+
38+
# Run the nested pagination query
39+
# We expect only 2 database queries if the optimizer is enabled, otherwise 3 (N+1)
40+
with utils.assert_num_queries(2 if DjangoOptimizerExtension.enabled.get() else 3):
41+
result = gql_client.query(query, {"first": 2, "after": to_base64(PREFIX, 0)})
42+
43+
# We expect the 2nd and 3rd milestones each with their 2nd and 3rd issues
44+
assert not result.errors
45+
assert result.data == {
46+
"milestoneConn": {
47+
"edges": [
48+
{
49+
"node": {
50+
"id": to_base64("MilestoneType", milestone.id),
51+
"issuesWithFilters": {
52+
"edges": [
53+
{"node": {"id": to_base64("IssueType", issue.id)}}
54+
for issue in issues[1:3]
55+
]
56+
},
57+
}
58+
}
59+
for milestone, issues in list(nested_data.items())[1:3]
60+
]
61+
}
62+
}

0 commit comments

Comments
 (0)