Skip to content
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
13 changes: 9 additions & 4 deletions src/Resolve/ResolveCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ protected function buildPagination(
'endCursor' => $edgesAndCursors['cursors']['last'],
'startCursor' => $edgesAndCursors['cursors']['start'],
'hasNextPage' => $edgesAndCursors['cursors']['end'] !== $edgesAndCursors['cursors']['last'],
'hasPreviousPage' => $edgesAndCursors['cursors']['first'] !== null
&& $edgesAndCursors['cursors']['start'] !== $edgesAndCursors['cursors']['first'],
'hasPreviousPage' => $edgesAndCursors['cursors']['start'] !== base64_encode((string) 0),
],
];
}
Expand All @@ -215,6 +214,7 @@ protected function buildEdgesAndCursors(Collection $items, array $offsetAndLimit
'start' => base64_encode((string) 0),
];

$startCursor = null;
foreach ($items as $item) {
$cursors['last'] = base64_encode((string) ($index + $offsetAndLimit['offset']));

Expand All @@ -223,15 +223,20 @@ protected function buildEdgesAndCursors(Collection $items, array $offsetAndLimit
'cursor' => $cursors['last'],
];

if (! $startCursor) {
$startCursor = $cursors['last'];
}

if (! $cursors['first']) {
$cursors['first'] = $cursors['last'];
}

$index++;
}

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

return [
'cursors' => $cursors,
Expand Down
13 changes: 9 additions & 4 deletions src/Resolve/ResolveEntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public function buildPagination(
'endCursor' => $edgesAndCursors['cursors']['last'],
'startCursor' => $edgesAndCursors['cursors']['start'],
'hasNextPage' => $edgesAndCursors['cursors']['end'] !== $edgesAndCursors['cursors']['last'],
'hasPreviousPage' => $edgesAndCursors['cursors']['first'] !== null
&& $edgesAndCursors['cursors']['start'] !== $edgesAndCursors['cursors']['first'],
'hasPreviousPage' => $edgesAndCursors['cursors']['start'] !== base64_encode((string) 0),
],
];
}
Expand Down Expand Up @@ -155,6 +154,7 @@ protected function buildEdgesAndCursors(QueryBuilder $queryBuilder, array $offse
$paginator = new Paginator($queryBuilder->getQuery());
}

$startCursor = null;
foreach ($paginator->getQuery()->getResult() as $result) {
$cursors['last'] = base64_encode((string) ($index + $offsetAndLimit['offset']));

Expand All @@ -163,15 +163,20 @@ protected function buildEdgesAndCursors(QueryBuilder $queryBuilder, array $offse
'cursor' => $cursors['last'],
];

if (! $startCursor) {
$startCursor = $cursors['last'];
}

if (! $cursors['first']) {
$cursors['first'] = $cursors['last'];
}

$index++;
}

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

return [
'cursors' => $cursors,
Expand Down
59 changes: 53 additions & 6 deletions test/Feature/Type/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public function testFirst(): 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 @@ -48,10 +48,54 @@ public function testFirst(): void

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

$this->assertEquals($data['performance']['pageInfo']['startCursor'], $data['performance']['edges'][0]['cursor']);
$this->assertEquals($data['performance']['pageInfo']['endCursor'], $data['performance']['edges'][1]['cursor']);
$this->assertEquals($data['performances']['pageInfo']['startCursor'], $data['performances']['edges'][0]['cursor']);
$this->assertEquals($data['performances']['pageInfo']['endCursor'], $data['performances']['edges'][1]['cursor']);

$this->assertEquals(2, count($data['performance']['edges']));
$this->assertTrue($data['performances']['pageInfo']['hasNextPage']);
$this->assertFalse($data['performances']['pageInfo']['hasPreviousPage']);

$this->assertEquals(2, count($data['performances']['edges']));
}

public function testFirstWithOffset(): void
{
$driver = new Driver($this->getEntityManager());
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'performances' => $driver->completeConnection(Performance::class),
],
]),
]);

$query = '{
performances (pagination: { first: 2 after: "MQ==" }) {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
edges {
cursor
node {
id
}
}
}
}';
$result = GraphQL::executeQuery($schema, $query);

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

$this->assertEquals($data['performances']['pageInfo']['startCursor'], $data['performances']['edges'][0]['cursor']);
$this->assertEquals($data['performances']['pageInfo']['endCursor'], $data['performances']['edges'][1]['cursor']);

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

$this->assertEquals(2, count($data['performances']['edges']));
}

public function testCollectionFirst(): void
Expand All @@ -72,7 +116,7 @@ public function testCollectionFirst(): void
cursor
node {
id
performances (pagination: { first: 2 }) {
performances (pagination: { first: 2 after: "MQ==" }) {
pageInfo {
hasNextPage
hasPreviousPage
Expand Down Expand Up @@ -103,6 +147,9 @@ public function testCollectionFirst(): void
$data['artists']['edges'][0]['node']['performances']['edges'][1]['cursor'],
);

$this->assertTrue($data['artists']['edges'][0]['node']['performances']['pageInfo']['hasNextPage']);
$this->assertTrue($data['artists']['edges'][0]['node']['performances']['pageInfo']['hasPreviousPage']);

$this->assertEquals(2, count($data['artists']['edges'][0]['node']['performances']['edges']));
}

Expand Down
Loading