Paginating large result sets
It is quite common to have to paginate over large result sets.
GraphQLite offers a simple way to do that using Porpaginas.
Porpaginas is a set of PHP interfaces that can be implemented by result iterators. It comes with a native support for PHP arrays, Doctrine and TDBM.
If you are a Laravel user, Eloquent does not come with a Porpaginas iterator. However, the GraphQLite Laravel bundle comes with its own pagination system.
Installation
You will need to install the Porpaginas library to benefit from this feature.
$ composer require beberlei/porpaginas
Usage
In your query, simply return a class that implements Porpaginas\Result
:
class MyController
{
/**
* @Query
* @return Product[]
*/
public function products(): Porpaginas\Result
{
// Some code that returns a list of products
// If you are using Doctrine, something like:
return new Porpaginas\Doctrine\ORM\ORMQueryResult($doctrineQuery);
}
}
Notice that:
- the method return type MUST BE
Porpaginas\Result
or a class implementingPorpaginas\Result
- you MUST add a
@return
statement to help GraphQLite find the type of the list
Once this is done, you can paginate directly from your GraphQL query:
products {
items(limit: 10, offset: 20) {
id
name
}
count
}
Results are wrapped into an item field. You can use the "limit" and "offset" parameters to apply pagination automatically.
The "count" field returns the total count of items.