Skip to content
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

Fix calculation of has_next_page in resolve_connection_from_cache #622

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
8 changes: 6 additions & 2 deletions strawberry_django/relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,15 @@
for node in result
]
has_previous_page = (
nodes[0]._strawberry_row_number > 1 # type: ignore
result[0]._strawberry_row_number > 1 # type: ignore

Check warning on line 191 in strawberry_django/relay.py

View workflow job for this annotation

GitHub Actions / Typing

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
if result
else False
)
Comment on lines 190 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider refactoring to improve robustness and performance

The current implementation could raise an IndexError if result is empty. Additionally, accessing result[0] and result[-1] multiple times may impact performance for larger lists. Consider refactoring like this:

if not result:
    has_previous_page = False
    has_next_page = False
else:
    first_item = result[0]
    last_item = result[-1]
    has_previous_page = first_item._strawberry_row_number > 1
    has_next_page = last_item._strawberry_row_number < last_item._strawberry_total_count

This approach handles the empty list case first and optimizes list access. It also maintains the corrected logic for has_next_page, which is an improvement over the previous version.

Suggested change
has_previous_page = (
nodes[0]._strawberry_row_number > 1 # type: ignore
result[0]._strawberry_row_number > 1 # type: ignore
if result
else False
)
if not result:
has_previous_page = False
else:
first_item = result[0]
has_previous_page = first_item._strawberry_row_number > 1

has_next_page = (
result[-1]._strawberry_row_number < result[-1]._strawberry_total_count # type: ignore

Check warning on line 196 in strawberry_django/relay.py

View workflow job for this annotation

GitHub Actions / Typing

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
if result
else False
)
has_next_page = result._strawberry_row_number < result if result else False

return cls(
edges=edges,
Expand Down
62 changes: 62 additions & 0 deletions tests/relay/test_nested_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest
from strawberry.relay import to_base64
from strawberry.relay.types import PREFIX

from strawberry_django.optimizer import DjangoOptimizerExtension
from tests import utils
from tests.projects.faker import IssueFactory, MilestoneFactory


@pytest.mark.django_db(transaction=True)
def test_nested_pagination(gql_client: utils.GraphQLTestClient):
# Nested pagination with the same arguments for the parent and child connections
query = """
query testNestedConnectionPagination($first: Int, $after: String) {
milestoneConn(first: $first, after: $after) {
edges {
node {
id
issuesWithFilters(first: $first, after: $after) {
edges {
node {
id
}
}
}
}
}
}
}
"""

# Create 4 milestones, each with 4 issues
nested_data = {
milestone: IssueFactory.create_batch(4, milestone=milestone)
for milestone in MilestoneFactory.create_batch(4)
}

# Run the nested pagination query
# We expect only 2 database queries if the optimizer is enabled, otherwise 3 (N+1)
with utils.assert_num_queries(2 if DjangoOptimizerExtension.enabled.get() else 3):
result = gql_client.query(query, {"first": 2, "after": to_base64(PREFIX, 0)})

# We expect the 2nd and 3rd milestones each with their 2nd and 3rd issues
assert not result.errors
assert result.data == {
"milestoneConn": {
"edges": [
{
"node": {
"id": to_base64("MilestoneType", milestone.id),
"issuesWithFilters": {
"edges": [
{"node": {"id": to_base64("IssueType", issue.id)}}
for issue in issues[1:3]
]
},
}
}
for milestone, issues in list(nested_data.items())[1:3]
]
}
}
Loading