Skip to content

Commit

Permalink
Fixed endCursor (was pointing to end of collection)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Jan 10, 2025
1 parent 9b319aa commit 8c9e033
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/Resolve/ResolveCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected function buildPagination(
'edges' => $edgesAndCursors['edges'],
'totalCount' => $itemCount,
'pageInfo' => [
'endCursor' => $edgesAndCursors['cursors']['end'],
'endCursor' => $edgesAndCursors['cursors']['last'],
'startCursor' => $edgesAndCursors['cursors']['start'],
'hasNextPage' => $edgesAndCursors['cursors']['end'] !== $edgesAndCursors['cursors']['last'],
'hasPreviousPage' => $edgesAndCursors['cursors']['first'] !== null
Expand Down Expand Up @@ -230,7 +230,8 @@ protected function buildEdgesAndCursors(Collection $items, array $offsetAndLimit
$index++;
}

$cursors['end'] = $cursors['last'] ?? base64_encode('0');
$endIndex = $itemCount ? $itemCount - 1 : 0;
$cursors['end'] = base64_encode((string) $endIndex);

return [
'cursors' => $cursors,
Expand Down
5 changes: 3 additions & 2 deletions src/Resolve/ResolveEntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function buildPagination(
'edges' => $edgesAndCursors['edges'],
'totalCount' => $edgesAndCursors['totalCount'],
'pageInfo' => [
'endCursor' => $edgesAndCursors['cursors']['end'],
'endCursor' => $edgesAndCursors['cursors']['last'],
'startCursor' => $edgesAndCursors['cursors']['start'],
'hasNextPage' => $edgesAndCursors['cursors']['end'] !== $edgesAndCursors['cursors']['last'],
'hasPreviousPage' => $edgesAndCursors['cursors']['first'] !== null
Expand Down Expand Up @@ -170,7 +170,8 @@ protected function buildEdgesAndCursors(QueryBuilder $queryBuilder, array $offse
$index++;
}

$cursors['end'] = $cursors['last'] ?? base64_encode((string) 0);
$endIndex = $paginator->count() ? $paginator->count() - 1 : 0;
$cursors['end'] = base64_encode((string) $endIndex);

return [
'cursors' => $cursors,
Expand Down
8 changes: 4 additions & 4 deletions test/Feature/Type/PageInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public function testPageInfoHasNextPage(): void
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'performance' => $driver->completeConnection(Performance::class),
'performances' => $driver->completeConnection(Performance::class),
],
]),
]);

$query = '{
performance (pagination: { first: 2 }) {
performances (pagination: { first: 2 }) {
pageInfo {
hasNextPage
hasPreviousPage
Expand All @@ -88,8 +88,8 @@ public function testPageInfoHasNextPage(): void

$data = $result->toArray()['data'];

$this->assertTrue($data['performance']['pageInfo']['hasNextPage']);
$this->assertFalse($data['performance']['pageInfo']['hasPreviousPage']);
$this->assertTrue($data['performances']['pageInfo']['hasNextPage']);
$this->assertFalse($data['performances']['pageInfo']['hasPreviousPage']);
}

public function testPageInfoHasPreviousPage(): void
Expand Down

0 comments on commit 8c9e033

Please sign in to comment.