|
| 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