Skip to content

Commit e0f8c38

Browse files
authoredDec 13, 2024··
fix(laravel): graphql fix relationship loading (#6857)
fix loading relationships by only loading those related to the model Closes: #6791 Signed-off-by: Tobias Oitzinger <tobiasoitzinger@gmail.com>
1 parent f6f9d8c commit e0f8c38

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed
 

‎src/GraphQl/State/Processor/NormalizeProcessor.php

+6
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

+1
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

+7-1
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
}

‎src/Laravel/Tests/GraphQlTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ public function testGetBooks(): void
4848
$this->assertArrayNotHasKey('errors', $data);
4949
}
5050

51+
public function testGetBooksWithSimplePagination(): void
52+
{
53+
BookFactory::new()->has(AuthorFactory::new())->count(9)->create();
54+
$response = $this->postJson('/api/graphql', ['query' => '{
55+
simplePaginationBooks(page: 1) {
56+
collection {
57+
id
58+
},
59+
paginationInfo {
60+
itemsPerPage,
61+
currentPage,
62+
lastPage,
63+
totalCount,
64+
hasNextPage
65+
}
66+
}
67+
}'], ['accept' => ['application/json']]);
68+
$response->assertStatus(200);
69+
$data = $response->json();
70+
$this->assertArrayHasKey('data', $data);
71+
$this->assertCount(3, $data['data']['simplePaginationBooks']['collection']);
72+
$this->assertEquals(3, $data['data']['simplePaginationBooks']['paginationInfo']['itemsPerPage']);
73+
$this->assertEquals(1, $data['data']['simplePaginationBooks']['paginationInfo']['currentPage']);
74+
$this->assertEquals(3, $data['data']['simplePaginationBooks']['paginationInfo']['lastPage']);
75+
$this->assertEquals(9, $data['data']['simplePaginationBooks']['paginationInfo']['totalCount']);
76+
$this->assertTrue($data['data']['simplePaginationBooks']['paginationInfo']['hasNextPage']);
77+
$this->assertArrayNotHasKey('errors', $data);
78+
}
79+
5180
public function testGetBooksWithPaginationAndOrder(): void
5281
{
5382
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

‎src/Laravel/workbench/app/Models/Book.php

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
new QueryParameter(key: 'order[:property]', filter: OrderFilter::class),
5757
],
5858
),
59+
new QueryCollection(
60+
paginationItemsPerPage: 3,
61+
name: 'simplePagination',
62+
paginationType: 'page',
63+
),
5964
new Mutation(name: 'create'),
6065
]
6166
)]

0 commit comments

Comments
 (0)
Please sign in to comment.