Skip to content

Add a partial paginator to prevent SQL COUNT queries #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
44 changes: 44 additions & 0 deletions features/hydra/collection.feature
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,50 @@ Feature: Collections support
}
"""

Scenario: Enable the partial pagination client side
When I send a "GET" request to "/dummies?page=7&partial=1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/Dummy$"},
"@id": {"pattern": "^/dummies$"},
"@type": {"pattern": "^hydra:Collection$"},
"hydra:totalItems": {"type":"number", "maximum": 30},
"hydra:member": {
"type": "array",
"items": {
"type": "object",
"properties": {
"@id": {
"oneOf": [
{"pattern": "^/dummies/19$"},
{"pattern": "^/dummies/20$"},
{"pattern": "^/dummies/21$"}
]
}
}
},
"maxItems": 3
},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/dummies\\?partial=1&page=7$"},
"@type": {"pattern": "^hydra:PartialCollectionView$"},
"hydra:next": {"pattern": "^/dummies\\?partial=1&page=8$"},
"hydra:previous": {"pattern": "^/dummies\\?partial=1&page=6$"}
},
"additionalProperties": false
}
}
}
"""

Scenario: Disable the pagination client side
When I send a "GET" request to "/dummies?pagination=0"
Then the response status code should be 200
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ parameters:
- '#Parameter \#2 \$dqlPart of method Doctrine\\ORM\\QueryBuilder::add\(\) expects Doctrine\\ORM\\Query\\Expr\\Base, Doctrine\\ORM\\Query\\Expr\\Join\[\] given#' # Fixed in Doctrine's master
- '#Call to an undefined method Doctrine\\Common\\Persistence\\ObjectManager::getConnection\(\)#'
- '#Parameter \#1 \$callable of static method Doctrine\\Common\\Annotations\\AnnotationRegistry::registerLoader\(\) expects callable, mixed\[\] given#'
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryResult(Item|Collection)ExtensionInterface::getResult\(\) invoked with 3 parameters, 1 required#'
75 changes: 75 additions & 0 deletions src/Bridge/Doctrine/Orm/AbstractPaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Doctrine\Orm;

use ApiPlatform\Core\DataProvider\PartialPaginatorInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\ORM\Query;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;

abstract class AbstractPaginator implements \IteratorAggregate, PartialPaginatorInterface
{
protected $paginator;
protected $iterator;
protected $firstResult;
protected $maxResults;

/**
* @throws InvalidArgumentException
*/
public function __construct(DoctrinePaginator $paginator)
{
$query = $paginator->getQuery();

if (null === ($firstResult = $query->getFirstResult()) || null === $maxResults = $query->getMaxResults()) {
throw new InvalidArgumentException(sprintf('"%1$s::setFirstResult()" or/and "%1$s::setMaxResults()" was/were not applied to the query.', Query::class));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw the test below but I don't get why we need such message (as we didn't needed it before)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because these two methods can potentially return null. See the Doctrine PHPDoc:

Returns NULL if {link setFirstResult} was not applied to this query.
Returns NULL if {link setMaxResults} was not applied to this query.

We always call Query::setFirstResult() and Query::setMaxResults() before instantiating our Paginator class, it's why we never had the bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always call Query::setFirstResult() and Query::setMaxResults() before instantiating our Paginator class, it's why we never had the bug.

Yes that's the reason why we don't need the test no? I mean we're still always using setFirstResult and setMaxResults or I miss something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but this is a library and this class is not internal so users are free to use it as they want. IMO we have to prevent a fatal error by preventing this specific use case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay wasn't sure this was the reason but I thought about it! Thanks for clarifying!


$this->paginator = $paginator;
$this->firstResult = $firstResult;
$this->maxResults = $maxResults;
}

/**
* {@inheritdoc}
*/
public function getCurrentPage(): float
{
return floor($this->firstResult / $this->maxResults) + 1.;
}

/**
* {@inheritdoc}
*/
public function getItemsPerPage(): float
{
return (float) $this->maxResults;
}

/**
* {@inheritdoc}
*/
public function getIterator(): \Traversable
{
return $this->iterator ?? $this->iterator = $this->paginator->getIterator();
}

/**
* {@inheritdoc}
*/
public function count(): int
{
return iterator_count($this->getIterator());
}
}
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Orm/CollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getCollection(string $resourceClass, string $operationName = nul
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName);

if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
return $extension->getResult($queryBuilder, $resourceClass, $operationName);
}
}

Expand Down
52 changes: 50 additions & 2 deletions src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Extension;

use ApiPlatform\Core\Bridge\Doctrine\Orm\AbstractPaginator;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryChecker;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
Expand Down Expand Up @@ -44,8 +45,11 @@ final class PaginationExtension implements QueryResultCollectionExtensionInterfa
private $enabledParameterName;
private $itemsPerPageParameterName;
private $maximumItemPerPage;
private $partial;
private $clientPartial;
private $partialParameterName;

public function __construct(ManagerRegistry $managerRegistry, RequestStack $requestStack, ResourceMetadataFactoryInterface $resourceMetadataFactory, bool $enabled = true, bool $clientEnabled = false, bool $clientItemsPerPage = false, int $itemsPerPage = 30, string $pageParameterName = 'page', string $enabledParameterName = 'pagination', string $itemsPerPageParameterName = 'itemsPerPage', int $maximumItemPerPage = null)
public function __construct(ManagerRegistry $managerRegistry, RequestStack $requestStack, ResourceMetadataFactoryInterface $resourceMetadataFactory, bool $enabled = true, bool $clientEnabled = false, bool $clientItemsPerPage = false, int $itemsPerPage = 30, string $pageParameterName = 'page', string $enabledParameterName = 'pagination', string $itemsPerPageParameterName = 'itemsPerPage', int $maximumItemPerPage = null, bool $partial = false, bool $clientPartial = false, string $partialParameterName = 'partial')
{
$this->managerRegistry = $managerRegistry;
$this->requestStack = $requestStack;
Expand All @@ -58,6 +62,9 @@ public function __construct(ManagerRegistry $managerRegistry, RequestStack $requ
$this->enabledParameterName = $enabledParameterName;
$this->itemsPerPageParameterName = $itemsPerPageParameterName;
$this->maximumItemPerPage = $maximumItemPerPage;
$this->partial = $partial;
$this->clientPartial = $clientPartial;
$this->partialParameterName = $partialParameterName;
}

/**
Expand Down Expand Up @@ -108,14 +115,55 @@ public function supportsResult(string $resourceClass, string $operationName = nu
/**
* {@inheritdoc}
*/
public function getResult(QueryBuilder $queryBuilder)
public function getResult(QueryBuilder $queryBuilder/*, string $resourceClass, string $operationName = null*/)
{
$resourceClass = $operationName = null;

if (func_num_args() >= 2) {
$resourceClass = func_get_arg(1);
} else {
@trigger_error(sprintf('Method %s() will have a 2nd `string $resourceClass` argument in version 3.0. Not defining it is deprecated since 2.2.', __METHOD__), E_USER_DEPRECATED);
}

if (func_num_args() >= 3) {
$operationName = func_get_arg(2);
} else {
@trigger_error(sprintf('Method %s() will have a 3rd `string $operationName = null` argument in version 3.0. Not defining it is deprecated since 2.2.', __METHOD__), E_USER_DEPRECATED);
}

$doctrineOrmPaginator = new DoctrineOrmPaginator($queryBuilder, $this->useFetchJoinCollection($queryBuilder));
$doctrineOrmPaginator->setUseOutputWalkers($this->useOutputWalkers($queryBuilder));

$resourceMetadata = null === $resourceClass ? null : $this->resourceMetadataFactory->create($resourceClass);

if ($this->isPartialPaginationEnabled($this->requestStack->getCurrentRequest(), $resourceMetadata, $operationName)) {
return new class($doctrineOrmPaginator) extends AbstractPaginator {
};
}

return new Paginator($doctrineOrmPaginator);
}

private function isPartialPaginationEnabled(Request $request = null, ResourceMetadata $resourceMetadata = null, string $operationName = null): bool
{
$enabled = $this->partial;
$clientEnabled = $this->clientPartial;

if ($resourceMetadata) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null !== (same for all tests below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I do it after yesterday's conversation? 😛

$enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_partial', $enabled, true);

if ($request) {
$clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_partial', $clientEnabled, true);
}
}

if ($clientEnabled && $request) {
$enabled = filter_var($request->query->get($this->partialParameterName, $enabled), FILTER_VALIDATE_BOOLEAN);
}

return $enabled;
}

private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null): bool
{
$enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public function supportsResult(string $resourceClass, string $operationName = nu

/**
* @param QueryBuilder $queryBuilder
* @param string $resourceClass
* @param string|null $operationName
*
* @return mixed
*/
public function getResult(QueryBuilder $queryBuilder);
public function getResult(QueryBuilder $queryBuilder/*, string $resourceClass, string $operationName = null*/);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public function supportsResult(string $resourceClass, string $operationName = nu

/**
* @param QueryBuilder $queryBuilder
* @param string $resourceClass
* @param string|null $operationName
*
* @return mixed
*/
public function getResult(QueryBuilder $queryBuilder);
public function getResult(QueryBuilder $queryBuilder/*, string $resourceClass, string $operationName = null*/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep consistency with QueryResultCollectionExtensionInterface but yes, maybe it's useless...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep it, at least we have them if we need it later :P.

}
69 changes: 3 additions & 66 deletions src/Bridge/Doctrine/Orm/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,95 +14,32 @@
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;

use ApiPlatform\Core\DataProvider\PaginatorInterface;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrineOrmPaginator;

/**
* Decorates the Doctrine ORM paginator.
*
* @author Kévin Dunglas <[email protected]>
*/
final class Paginator implements \IteratorAggregate, PaginatorInterface
final class Paginator extends AbstractPaginator implements PaginatorInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implements PaginatorInterface can be removed (already implemented by the abstract class).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nop, AbstractPaginator implements PartialPaginatorInterface.

Copy link
Member

@dunglas dunglas Sep 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, keep it as is :) Sorry.

{
private $paginator;

/**
* @var int
*/
private $firstResult;

/**
* @var int
*/
private $maxResults;

/**
* @var int
*/
private $totalItems;

/**
* @var \Traversable
*/
private $iterator;

public function __construct(DoctrineOrmPaginator $paginator)
{
$this->paginator = $paginator;
$query = $paginator->getQuery();
$this->firstResult = $query->getFirstResult();
$this->maxResults = $query->getMaxResults();
$this->totalItems = count($paginator);
}

/**
* {@inheritdoc}
*/
public function getCurrentPage(): float
{
return floor($this->firstResult / $this->maxResults) + 1.;
}

/**
* {@inheritdoc}
*/
public function getLastPage(): float
{
return ceil($this->totalItems / $this->maxResults) ?: 1.;
}

/**
* {@inheritdoc}
*/
public function getItemsPerPage(): float
{
return (float) $this->maxResults;
return ceil($this->getTotalItems() / $this->maxResults) ?: 1.;
}

/**
* {@inheritdoc}
*/
public function getTotalItems(): float
{
return (float) $this->totalItems;
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
if (null === $this->iterator) {
$this->iterator = $this->paginator->getIterator();
}

return $this->iterator;
}

/**
* {@inheritdoc}
*/
public function count()
{
return count($this->getIterator());
return (float) ($this->totalItems ?? $this->totalItems = count($this->paginator));
}
}
4 changes: 2 additions & 2 deletions src/Bridge/Doctrine/Orm/SubresourceDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ public function getSubresource(string $resourceClass, array $identifiers, array
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName);

if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
return $extension->getResult($queryBuilder, $resourceClass, $operationName);
}
}
} else {
foreach ($this->itemExtensions as $extension) {
$extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operationName, $context);

if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
return $extension->getResult($queryBuilder, $resourceClass, $operationName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,16 @@ private function handleConfig(ContainerBuilder $container, array $config, array
$container->setParameter('api_platform.collection.order', $config['collection']['order']);
$container->setParameter('api_platform.collection.order_parameter_name', $config['collection']['order_parameter_name']);
$container->setParameter('api_platform.collection.pagination.enabled', $config['collection']['pagination']['enabled']);
$container->setParameter('api_platform.collection.pagination.partial', $config['collection']['pagination']['partial']);
$container->setParameter('api_platform.collection.pagination.client_enabled', $config['collection']['pagination']['client_enabled']);
$container->setParameter('api_platform.collection.pagination.client_items_per_page', $config['collection']['pagination']['client_items_per_page']);
$container->setParameter('api_platform.collection.pagination.client_partial', $config['collection']['pagination']['client_partial']);
$container->setParameter('api_platform.collection.pagination.items_per_page', $config['collection']['pagination']['items_per_page']);
$container->setParameter('api_platform.collection.pagination.maximum_items_per_page', $config['collection']['pagination']['maximum_items_per_page']);
$container->setParameter('api_platform.collection.pagination.page_parameter_name', $config['collection']['pagination']['page_parameter_name']);
$container->setParameter('api_platform.collection.pagination.enabled_parameter_name', $config['collection']['pagination']['enabled_parameter_name']);
$container->setParameter('api_platform.collection.pagination.items_per_page_parameter_name', $config['collection']['pagination']['items_per_page_parameter_name']);
$container->setParameter('api_platform.collection.pagination.partial_parameter_name', $config['collection']['pagination']['partial_parameter_name']);
$container->setParameter('api_platform.http_cache.etag', $config['http_cache']['etag']);
$container->setParameter('api_platform.http_cache.max_age', $config['http_cache']['max_age']);
$container->setParameter('api_platform.http_cache.shared_max_age', $config['http_cache']['shared_max_age']);
Expand Down
Loading