Skip to content

Commit

Permalink
fix(graphql): remove count query if paginationInfo is not requested
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Leune committed Dec 21, 2023
1 parent fe07a7f commit 6690814
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/GraphQl/Resolver/Factory/CollectionResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function __construct(private readonly ReadStageInterface $readStage, priv
public function __invoke(string $resourceClass = null, string $rootClass = null, Operation $operation = null): callable
{
return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation): ?array {
if (!isset($info->getFieldSelection()['paginationInfo'])) {
$operation = $operation->withPaginationPartial(true);

Check warning on line 45 in src/GraphQl/Resolver/Factory/CollectionResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/CollectionResolverFactory.php#L44-L45

Added lines #L44 - L45 were not covered by tests
}

// If authorization has failed for a relation field (e.g. via ApiProperty security), the field is not present in the source: null can be returned directly to ensure the collection isn't in the response.
if (null === $resourceClass || null === $rootClass || (null !== $source && !\array_key_exists($info->fieldName, $source))) {
return null;
Expand All @@ -49,6 +53,7 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
$resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => true, 'is_mutation' => false, 'is_subscription' => false];

$collection = ($this->readStage)($resourceClass, $rootClass, $operation, $resolverContext);

if (!is_iterable($collection)) {
throw new \LogicException('Collection from read stage should be iterable.');
}
Expand Down
22 changes: 12 additions & 10 deletions src/GraphQl/Resolver/Stage/SerializeStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function __invoke(object|array|null $itemOrCollection, string $resourceCl
$data = $this->normalizer->normalize($itemOrCollection, ItemNormalizer::FORMAT, $normalizationContext);
}
}

if ($isCollection && is_iterable($itemOrCollection)) {
if (!$this->pagination->isGraphQlEnabled($operation, $context)) {
$data = [];
Expand All @@ -87,8 +86,8 @@ public function __invoke(object|array|null $itemOrCollection, string $resourceCl
}
} else {
$data = 'cursor' === $this->pagination->getGraphQlPaginationType($operation) ?
$this->serializeCursorBasedPaginatedCollection($itemOrCollection, $normalizationContext, $context) :
$this->serializePageBasedPaginatedCollection($itemOrCollection, $normalizationContext);
$this->serializeCursorBasedPaginatedCollection($operation, $itemOrCollection, $normalizationContext, $context) :
$this->serializePageBasedPaginatedCollection($operation, $itemOrCollection, $normalizationContext);

Check warning on line 90 in src/GraphQl/Resolver/Stage/SerializeStage.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Stage/SerializeStage.php#L89-L90

Added lines #L89 - L90 were not covered by tests
}
}

Expand All @@ -109,7 +108,7 @@ public function __invoke(object|array|null $itemOrCollection, string $resourceCl
* @throws \LogicException
* @throws \UnexpectedValueException
*/
private function serializeCursorBasedPaginatedCollection(iterable $collection, array $normalizationContext, array $context): array
private function serializeCursorBasedPaginatedCollection(Operation $operation, iterable $collection, array $normalizationContext, array $context): array

Check warning on line 111 in src/GraphQl/Resolver/Stage/SerializeStage.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Stage/SerializeStage.php#L111

Added line #L111 was not covered by tests
{
$args = $context['args'];

Expand Down Expand Up @@ -173,16 +172,19 @@ private function serializeCursorBasedPaginatedCollection(iterable $collection, a
/**
* @throws \LogicException
*/
private function serializePageBasedPaginatedCollection(iterable $collection, array $normalizationContext): array
private function serializePageBasedPaginatedCollection(Operation $operation, iterable $collection, array $normalizationContext): array

Check warning on line 175 in src/GraphQl/Resolver/Stage/SerializeStage.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Stage/SerializeStage.php#L175

Added line #L175 was not covered by tests
{
if (!($collection instanceof PaginatorInterface)) {
if (!($collection instanceof PaginatorInterface) && !($collection instanceof PartialPaginatorInterface)) {

Check warning on line 177 in src/GraphQl/Resolver/Stage/SerializeStage.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Stage/SerializeStage.php#L177

Added line #L177 was not covered by tests
throw new \LogicException(sprintf('Collection returned by the collection data provider must implement %s.', PaginatorInterface::class));
}

$data = $this->getDefaultPageBasedPaginatedData();
$data['paginationInfo']['totalCount'] = $collection->getTotalItems();
$data['paginationInfo']['lastPage'] = $collection->getLastPage();
$data['paginationInfo']['itemsPerPage'] = $collection->getItemsPerPage();
$data = [];
if (!$operation->getPaginationPartial()) {
$data = $this->getDefaultPageBasedPaginatedData();
$data['paginationInfo']['totalCount'] = $collection->getTotalItems();

Check failure on line 184 in src/GraphQl/Resolver/Stage/SerializeStage.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Call to an undefined method ApiPlatform\State\Pagination\PartialPaginatorInterface::getTotalItems().
$data['paginationInfo']['lastPage'] = $collection->getLastPage();

Check failure on line 185 in src/GraphQl/Resolver/Stage/SerializeStage.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Call to an undefined method ApiPlatform\State\Pagination\PartialPaginatorInterface::getLastPage().
$data['paginationInfo']['itemsPerPage'] = $collection->getItemsPerPage();

Check warning on line 186 in src/GraphQl/Resolver/Stage/SerializeStage.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Stage/SerializeStage.php#L181-L186

Added lines #L181 - L186 were not covered by tests
}

foreach ($collection as $object) {
$data['collection'][] = $this->normalizer->normalize($object, ItemNormalizer::FORMAT, $normalizationContext);
Expand Down

0 comments on commit 6690814

Please sign in to comment.