Pagination in GraphQL API
How to do the simplest pagination in GraphQL? (using Star War API) - Stack Overflow
GraphQL Pagination best practices: Using Edges vs Nodes in Connections
Pagination Example
Hello guys,
I really interested in graphql. Recently, I'm following the tutorial and I have a problem when paging the query in graphql.
Assume that we have Post, Comment, Reply
-
Post --(1-n)--> Comment
-
Comment --(1-n)--> Reply
I followed the tutorials and end up with this schema:
schema.graphql
type Post {
id: ID!
content: String!
comments: [Comment!]
}
type Comment {
id: ID!
content: String!
replies: [Reply!]
}
type Reply {
id: ID!
content: String!
}
Query {
posts: [Post!]
}When Front-end side query posts, thanks to graphql, we can do all in 1 query (1):
query GetPosts {
posts{
...
comments {
...
replies {
id
content
}
}
}
}Then I can get all comments and its corresponding replies for each post.
However, the UI design needs pagination (and most of UI models need it). Assume that we have like 100 comments and 100 replies for each comment.
So I end up doing like this:
schema.graphql
...
type Pagination {
pageNumber: Int!
perPage: Int!
totalPage: Int!
}
...
type PostResponse {
posts: [Post!]
pagination: Pagination!
}
type CommentResponse {
comments: [Comment!]
pagination: Pagination!
}
type ReplyResponse {
replies: [Reply!]
pagination: Pagination!
}
// added three queries for each model
type Query {
...
posts(pagination: Pagination): PostResponse
comments(pagination: Pagination): CommentResponse
replies(pagination: Pagination): ReplyResponse
}And map it to each UI model: Post, Comment and Reply.
So eventually, the query (1) is not useful when Frontend needs to fetch each api separately (for pagination).
I think that it is useful for getting recently comments, top comments, but not for listing all comments.
Do you guys have any solution for my case? Thank you in advance.
For this particular demo API you will need to use the cursor and fetch the next set of records until you reach the page you want to display.
This demo API is more suitable for an infinite scroll type of pagination than a paged one since you can always get the next set of records based on where you are.
When you build your own app you can define your own pagination style in the back end based on the needs of your front end. If you want numbered, bookmarkable pages and you're using MySQL then you could use OFFSET and LIMIT to directly access the records you want however this approach has certain limitations.
I also was able to find more answers:
There are two types of paginations:
- Offset pagination
- Cursor pagination
The server has to be set up so that you can use it.
For example, on https://graphql-pokeapi.graphcdn.app/
I was able to use
{
pokemons(limit: 3, offset: 130) {
results {
id
name
}
}
}
and be able to get
{
"data": {
"pokemons": {
"results": [
{
"id": 131,
"name": "lapras"
},
{
"id": 132,
"name": "ditto"
},
{
"id": 133,
"name": "eevee"
}
]
}
}
}
You can try different offset and limit to experiment with it. With this, the traditional
Page: Prev 1 2 3 4 5 6 7 8 9 Next
can be supported, and so can the URL having a ?page=5 so that you can send somebody the URL and be able to see page 5.