GraphQL
Beginner
1 min read
Pagination with Connections
Example
# Schema: Relay-style connection
type PostConnection {
edges: [PostEdge!]!
pageInfo: PageInfo!
totalCount: Int!
}
type PostEdge {
node: Post!
cursor: String!
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
endCursor: String
}
type Query {
posts(first: Int, after: String, last: Int, before: String): PostConnection!
}
# Query: first page
query FirstPage {
posts(first: 10) {
totalCount
edges {
cursor
node { id title publishedAt }
}
pageInfo { hasNextPage endCursor }
}
}
# Query: next page using endCursor from previous response
query NextPage {
posts(first: 10, after: "cursor_from_previous_response") {
edges {
node { id title }
}
pageInfo { hasNextPage endCursor }
}
}
Related Resources
GraphQL Reference
Complete tag & property list
GraphQL How-To Guides
Step-by-step practical guides
GraphQL Exercises
Practice what you've learned
More in GraphQL