Skip to content

Commit 7a953cf

Browse files
committed
fixed hasPreviousPage
1 parent 8c9e033 commit 7a953cf

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

src/Resolve/ResolveCollectionFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ protected function buildPagination(
193193
'endCursor' => $edgesAndCursors['cursors']['last'],
194194
'startCursor' => $edgesAndCursors['cursors']['start'],
195195
'hasNextPage' => $edgesAndCursors['cursors']['end'] !== $edgesAndCursors['cursors']['last'],
196-
'hasPreviousPage' => $edgesAndCursors['cursors']['first'] !== null
197-
&& $edgesAndCursors['cursors']['start'] !== $edgesAndCursors['cursors']['first'],
196+
'hasPreviousPage' => $edgesAndCursors['cursors']['start'] !== base64_encode((string) 0),
198197
],
199198
];
200199
}
@@ -215,6 +214,7 @@ protected function buildEdgesAndCursors(Collection $items, array $offsetAndLimit
215214
'start' => base64_encode((string) 0),
216215
];
217216

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

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

226+
if (! $startCursor) {
227+
$startCursor = $cursors['last'];
228+
}
229+
226230
if (! $cursors['first']) {
227231
$cursors['first'] = $cursors['last'];
228232
}
229233

230234
$index++;
231235
}
232236

233-
$endIndex = $itemCount ? $itemCount - 1 : 0;
234-
$cursors['end'] = base64_encode((string) $endIndex);
237+
$endIndex = $itemCount ? $itemCount - 1 : 0;
238+
$cursors['end'] = base64_encode((string) $endIndex);
239+
$cursors['start'] = $startCursor ?? $cursors['start'];
235240

236241
return [
237242
'cursors' => $cursors,

src/Resolve/ResolveEntityFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ public function buildPagination(
123123
'endCursor' => $edgesAndCursors['cursors']['last'],
124124
'startCursor' => $edgesAndCursors['cursors']['start'],
125125
'hasNextPage' => $edgesAndCursors['cursors']['end'] !== $edgesAndCursors['cursors']['last'],
126-
'hasPreviousPage' => $edgesAndCursors['cursors']['first'] !== null
127-
&& $edgesAndCursors['cursors']['start'] !== $edgesAndCursors['cursors']['first'],
126+
'hasPreviousPage' => $edgesAndCursors['cursors']['start'] !== base64_encode((string) 0),
128127
],
129128
];
130129
}
@@ -155,6 +154,7 @@ protected function buildEdgesAndCursors(QueryBuilder $queryBuilder, array $offse
155154
$paginator = new Paginator($queryBuilder->getQuery());
156155
}
157156

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

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

166+
if (! $startCursor) {
167+
$startCursor = $cursors['last'];
168+
}
169+
166170
if (! $cursors['first']) {
167171
$cursors['first'] = $cursors['last'];
168172
}
169173

170174
$index++;
171175
}
172176

173-
$endIndex = $paginator->count() ? $paginator->count() - 1 : 0;
174-
$cursors['end'] = base64_encode((string) $endIndex);
177+
$endIndex = $paginator->count() ? $paginator->count() - 1 : 0;
178+
$cursors['end'] = base64_encode((string) $endIndex);
179+
$cursors['start'] = $startCursor ?? $cursors['start'];
175180

176181
return [
177182
'cursors' => $cursors,

test/Feature/Type/PaginationTest.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public function testFirst(): void
2323
'query' => new ObjectType([
2424
'name' => 'query',
2525
'fields' => [
26-
'performance' => $driver->completeConnection(Performance::class),
26+
'performances' => $driver->completeConnection(Performance::class),
2727
],
2828
]),
2929
]);
3030

3131
$query = '{
32-
performance (pagination: { first: 2 }) {
32+
performances (pagination: { first: 2 }) {
3333
pageInfo {
3434
hasNextPage
3535
hasPreviousPage
@@ -48,10 +48,54 @@ public function testFirst(): void
4848

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

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

54-
$this->assertEquals(2, count($data['performance']['edges']));
54+
$this->assertTrue($data['performances']['pageInfo']['hasNextPage']);
55+
$this->assertFalse($data['performances']['pageInfo']['hasPreviousPage']);
56+
57+
$this->assertEquals(2, count($data['performances']['edges']));
58+
}
59+
60+
public function testFirstWithOffset(): void
61+
{
62+
$driver = new Driver($this->getEntityManager());
63+
$schema = new Schema([
64+
'query' => new ObjectType([
65+
'name' => 'query',
66+
'fields' => [
67+
'performances' => $driver->completeConnection(Performance::class),
68+
],
69+
]),
70+
]);
71+
72+
$query = '{
73+
performances (pagination: { first: 2 after: "MQ==" }) {
74+
pageInfo {
75+
hasNextPage
76+
hasPreviousPage
77+
startCursor
78+
endCursor
79+
}
80+
edges {
81+
cursor
82+
node {
83+
id
84+
}
85+
}
86+
}
87+
}';
88+
$result = GraphQL::executeQuery($schema, $query);
89+
90+
$data = $result->toArray()['data'];
91+
92+
$this->assertEquals($data['performances']['pageInfo']['startCursor'], $data['performances']['edges'][0]['cursor']);
93+
$this->assertEquals($data['performances']['pageInfo']['endCursor'], $data['performances']['edges'][1]['cursor']);
94+
95+
$this->assertTrue($data['performances']['pageInfo']['hasNextPage']);
96+
$this->assertTrue($data['performances']['pageInfo']['hasPreviousPage']);
97+
98+
$this->assertEquals(2, count($data['performances']['edges']));
5599
}
56100

57101
public function testCollectionFirst(): void
@@ -72,7 +116,7 @@ public function testCollectionFirst(): void
72116
cursor
73117
node {
74118
id
75-
performances (pagination: { first: 2 }) {
119+
performances (pagination: { first: 2 after: "MQ==" }) {
76120
pageInfo {
77121
hasNextPage
78122
hasPreviousPage
@@ -103,6 +147,9 @@ public function testCollectionFirst(): void
103147
$data['artists']['edges'][0]['node']['performances']['edges'][1]['cursor'],
104148
);
105149

150+
$this->assertTrue($data['artists']['edges'][0]['node']['performances']['pageInfo']['hasNextPage']);
151+
$this->assertTrue($data['artists']['edges'][0]['node']['performances']['pageInfo']['hasPreviousPage']);
152+
106153
$this->assertEquals(2, count($data['artists']['edges'][0]['node']['performances']['edges']));
107154
}
108155

0 commit comments

Comments
 (0)