Skip to content

Commit b3c191a

Browse files
committed
fix(laravel): fix graphql simple pagination
fix hasNextPage not working, add currentPage selection Closes: #6856 Signed-off-by: Tobias Oitzinger <[email protected]>
1 parent c899a3d commit b3c191a

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/GraphQl/State/Processor/NormalizeProcessor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ private function serializePageBasedPaginatedCollection(iterable $collection, arr
213213
}
214214
$data['paginationInfo']['totalCount'] = $collection->getTotalItems();
215215
}
216+
if (isset($selection['paginationInfo']['currentPage'])) {
217+
if (!($collection instanceof PartialPaginatorInterface)) {
218+
throw new \LogicException(\sprintf('Collection returned by the collection data provider must implement %s to return currentPage field.', PartialPaginatorInterface::class));
219+
}
220+
$data['paginationInfo']['currentPage'] = $collection->getCurrentPage();
221+
}
216222
if (isset($selection['paginationInfo']['lastPage'])) {
217223
if (!($collection instanceof PaginatorInterface)) {
218224
throw new \LogicException(\sprintf('Collection returned by the collection data provider must implement %s to return lastPage field.', PaginatorInterface::class));

src/GraphQl/Type/TypeBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ private function getPageBasedPaginationFields(GraphQLType $resourceType): array
278278
'itemsPerPage' => GraphQLType::nonNull(GraphQLType::int()),
279279
'lastPage' => GraphQLType::nonNull(GraphQLType::int()),
280280
'totalCount' => GraphQLType::nonNull(GraphQLType::int()),
281+
'currentPage' => GraphQLType::nonNull(GraphQLType::int()),
281282
'hasNextPage' => GraphQLType::nonNull(GraphQLType::boolean()),
282283
],
283284
];

src/Laravel/Eloquent/Paginator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Laravel\Eloquent;
1515

16+
use ApiPlatform\State\Pagination\HasNextPagePaginatorInterface;
1617
use ApiPlatform\State\Pagination\PaginatorInterface;
1718
use Illuminate\Pagination\LengthAwarePaginator;
1819
use IteratorAggregate;
@@ -21,7 +22,7 @@
2122
* @implements IteratorAggregate<mixed,object>
2223
* @implements PaginatorInterface<object>
2324
*/
24-
final class Paginator implements PaginatorInterface, \IteratorAggregate
25+
final class Paginator implements PaginatorInterface, HasNextPagePaginatorInterface, \IteratorAggregate
2526
{
2627
/**
2728
* @param LengthAwarePaginator<object> $paginator
@@ -60,4 +61,9 @@ public function getIterator(): \Traversable
6061
{
6162
return $this->paginator->getIterator();
6263
}
64+
65+
public function hasNextPage(): bool
66+
{
67+
return $this->paginator->hasMorePages();
68+
}
6369
}

0 commit comments

Comments
 (0)