-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
bellini666
merged 2 commits into
strawberry-graphql:main
from
BrightpathProgress:bugfix/relay-prefetch-attribute-error
Sep 4, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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, accessingresult[0]
andresult[-1]
multiple times may impact performance for larger lists. Consider refactoring like this: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.