Skip to content

Commit 9c32e6c

Browse files
committed
Pagination per resource
1 parent a6412ba commit 9c32e6c

File tree

2 files changed

+61
-44
lines changed

2 files changed

+61
-44
lines changed

core/configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ api_platform:
104104
enabled: true
105105
graphql_playground:
106106
enabled: true
107+
collection:
108+
pagination:
109+
enabled: true
107110

108111
elasticsearch:
109112
# To enable or disable Elasticsearch support.

core/graphql.md

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -127,49 +127,6 @@ For example, to query a book having as identifier `89`, you have to run the foll
127127

128128
Note that in this example, we're retrieving two fields: `title` and `isbn`.
129129

130-
### Pagination
131-
132-
API Platform natively enables a cursor-based pagination for collections.
133-
It supports [GraphQL's Complete Connection Model](https://graphql.org/learn/pagination/#complete-connection-model) and is compatible with [Relay's Cursor Connections Specification](https://facebook.github.io/relay/graphql/connections.htm).
134-
135-
Here is an example query leveraging the pagination system:
136-
137-
```graphql
138-
{
139-
offers(first: 10, after: "cursor") {
140-
totalCount
141-
pageInfo {
142-
endCursor
143-
hasNextPage
144-
}
145-
edges {
146-
cursor
147-
node {
148-
id
149-
}
150-
}
151-
}
152-
}
153-
```
154-
155-
The two parameters `first` and `after` are necessary to make the paginated query work, more precisely:
156-
157-
* `first` corresponds to the items per page starting from the beginning;
158-
* `after` corresponds to the `cursor` from which the items are returned.
159-
160-
The current page always has an `endCursor` present in the `pageInfo` field.
161-
To get the next page, you would add the `endCursor` from the current page as the `after` parameter.
162-
163-
```graphql
164-
{
165-
offers(first: 10, after: "endCursor") {
166-
}
167-
}
168-
```
169-
170-
When the property `hasNextPage` of the `pageInfo` field is false, you've reached the last page.
171-
If you move forward, you'll end up having an empty result.
172-
173130
### Custom Queries
174131

175132
To create a custom query, first of all you need to create its resolver.
@@ -343,7 +300,7 @@ You custom queries will be available like this:
343300
}
344301
```
345302

346-
# Mutations
303+
## Mutations
347304

348305
If you don't know what mutations are yet, the documentation about them is [here](https://graphql.org/learn/queries/#mutations).
349306

@@ -728,6 +685,8 @@ For example, if you want to search the offers with a green or a red product you
728685
API Platform natively enables a cursor-based pagination for collections.
729686
It supports [GraphQL's Complete Connection Model](https://graphql.org/learn/pagination/#complete-connection-model) and is compatible with [Relay's Cursor Connections Specification](https://facebook.github.io/relay/graphql/connections.htm).
730687

688+
### Using the Cursor-based Pagination
689+
731690
Here is an example query leveraging the pagination system:
732691

733692
```graphql
@@ -784,6 +743,61 @@ For the previous page, you would add the `startCursor` from the current page as
784743
How do you know when you have reached the last page? It is the aim of the property `hasNextPage` or `hasPreviousPage` in `pageInfo`.
785744
When it is false, you know it is the last page and moving forward or backward will give you an empty result.
786745

746+
### Disabling the Pagination
747+
748+
See also the [pagination documentation](pagination.md#disabling-the-pagination).
749+
750+
### Globally
751+
752+
The pagination can be disabled for all GraphQL resources using this configuration:
753+
754+
```yaml
755+
# api/config/packages/api_platform.yaml
756+
api_platform:
757+
graphql:
758+
collection:
759+
pagination:
760+
enabled: false
761+
```
762+
763+
### For a Specific Resource
764+
765+
It can also be disabled for a specific resource (REST and GraphQL):
766+
767+
```php
768+
<?php
769+
// api/src/Entity/Book.php
770+
771+
use ApiPlatform\Core\Annotation\ApiResource;
772+
773+
/**
774+
* @ApiResource(attributes={"pagination_enabled"=false})
775+
*/
776+
class Book
777+
{
778+
// ...
779+
}
780+
```
781+
782+
### For a Specific Resource Collection Operation
783+
784+
You can also disable the pagination for a specific collection operation:
785+
786+
```php
787+
<?php
788+
// api/src/Entity/Book.php
789+
790+
use ApiPlatform\Core\Annotation\ApiResource;
791+
792+
/**
793+
* @ApiResource(graphql={"collection_query"={"pagination_enabled"=false}})
794+
*/
795+
class Book
796+
{
797+
// ...
798+
}
799+
```
800+
787801
## Security
788802

789803
To add a security layer to your queries and mutations, follow the [security](security.md) documentation.

0 commit comments

Comments
 (0)