Skip to content

Commit fcf7a70

Browse files
authored
Fix pagination (#2560)
1 parent e3f228a commit fcf7a70

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

features/graphql/collection.feature

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,12 @@ Feature: GraphQL collection support
197197
Then the response status code should be 200
198198
And the response should be in JSON
199199
And the header "Content-Type" should be equal to "application/json"
200-
And the JSON node "data.dummies.pageInfo.endCursor" should be equal to "Mw=="
200+
And the JSON node "data.dummies.pageInfo.endCursor" should be equal to "MQ=="
201201
And the JSON node "data.dummies.pageInfo.hasNextPage" should be true
202202
And the JSON node "data.dummies.totalCount" should be equal to 4
203203
And the JSON node "data.dummies.edges[1].node.name" should be equal to "Dummy #2"
204204
And the JSON node "data.dummies.edges[1].cursor" should be equal to "MQ=="
205-
And the JSON node "data.dummies.edges[1].node.relatedDummies.pageInfo.endCursor" should be equal to "Mw=="
205+
And the JSON node "data.dummies.edges[1].node.relatedDummies.pageInfo.endCursor" should be equal to "MQ=="
206206
And the JSON node "data.dummies.edges[1].node.relatedDummies.pageInfo.hasNextPage" should be true
207207
And the JSON node "data.dummies.edges[1].node.relatedDummies.totalCount" should be equal to 4
208208
And the JSON node "data.dummies.edges[1].node.relatedDummies.edges[0].node.name" should be equal to "RelatedDummy12"
@@ -277,9 +277,11 @@ Feature: GraphQL collection support
277277
And the header "Content-Type" should be equal to "application/json"
278278
And the JSON node "data.dummies.edges" should have 1 element
279279
And the JSON node "data.dummies.pageInfo.hasNextPage" should be false
280+
And the JSON node "data.dummies.pageInfo.endCursor" should be equal to "Mw=="
280281
And the JSON node "data.dummies.edges[0].node.name" should be equal to "Dummy #4"
281282
And the JSON node "data.dummies.edges[0].cursor" should be equal to "Mw=="
282283
And the JSON node "data.dummies.edges[0].node.relatedDummies.pageInfo.hasNextPage" should be false
284+
And the JSON node "data.dummies.edges[0].node.relatedDummies.pageInfo.endCursor" should be equal to "Mw=="
283285
And the JSON node "data.dummies.edges[0].node.relatedDummies.edges" should have 2 elements
284286
And the JSON node "data.dummies.edges[0].node.relatedDummies.edges[1].node.name" should be equal to "RelatedDummy44"
285287
And the JSON node "data.dummies.edges[0].node.relatedDummies.edges[1].cursor" should be equal to "Mw=="

src/GraphQl/Resolver/Factory/CollectionResolverFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
112112

113113
$data = ['totalCount' => 0.0, 'edges' => [], 'pageInfo' => ['endCursor' => null, 'hasNextPage' => false]];
114114
if ($collection instanceof PaginatorInterface && ($totalItems = $collection->getTotalItems()) > 0) {
115-
$data['pageInfo']['endCursor'] = base64_encode((string) ($totalItems - 1));
116-
$data['pageInfo']['hasNextPage'] = $collection->getCurrentPage() !== $collection->getLastPage() && (float) $collection->count() === $collection->getItemsPerPage();
115+
$nbPageItems = $collection->count();
116+
$data['pageInfo']['endCursor'] = base64_encode((string) ($offset + $nbPageItems - 1));
117+
$data['pageInfo']['hasNextPage'] = $collection->getCurrentPage() !== $collection->getLastPage() && (float) $nbPageItems === $collection->getItemsPerPage();
117118
$data['totalCount'] = $totalItems;
118119
}
119120

tests/GraphQl/Resolver/Factory/CollectionResolverFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testCreatePaginatorCollectionResolver()
165165
$resolveInfo = new ResolveInfo('relatedDummies', [], null, new ObjectType(['name' => '']), '', new Schema([]), null, null, null, null);
166166

167167
$this->assertEquals(
168-
['edges' => [['node' => 'normalizedObject1', 'cursor' => 'Mg==']], 'pageInfo' => ['endCursor' => 'MTY=', 'hasNextPage' => true], 'totalCount' => 17],
168+
['edges' => [['node' => 'normalizedObject1', 'cursor' => 'Mg==']], 'pageInfo' => ['endCursor' => 'OQ==', 'hasNextPage' => true], 'totalCount' => 17.],
169169
$resolver(null, ['after' => $cursor], null, $resolveInfo)
170170
);
171171
}

0 commit comments

Comments
 (0)