GraphQL Connections #
A connection is the recommended way to implement @[pagination][] in a @[GraphQL][] @[API][application-programming-interface].
To help visualize connections, consider this diagram, which may or may not be formatted badly (I'll investigate better diagrams later...):
Start with the actual thing you intend to paginate. This is just a regular model, containing whatever data is necessary. From here on, we refer to this thing as the node.
type Thing {
id: Id!
# whatever you want in here
}
Connecting those nodes (to the connection) are edges, which contain a reference to the node itself, as well as some information about how to locate that node, such as the cursor value for this element of the pagination.
I have also seen the edge also contain
nextNodeandprevNode, but I think that is just an extension?
type ThingEdge {
node: Thing!
cursor: String!
}
Finally, the connection itself contains the edges as well as some information about the whole connection and the pagination state.
The connection may also include other things, like a list of nodes for direct access without having to go through edges, but again that is probably just an extension?
type ThingConnection {
totalCount: Int!
pageInfo: PageInfo!
edges: [ThingEdge]!
nodes: [Thing]!
}
The page info contains information about the page that is currently being viewed. This data is specific to your particular implementation of pagination, but should include enough data that the client is able to understand how many pages there are, what cursors to use next, and so on.
type PageInfo {
total: Int!
hasNextPage: Boolean!
hasPrevPage: Boolean!
endCursor: String!
startCursor: String!
}
Lastly, to tie it all together, these things are put into the query object somewhere,
typically on a pluralized property, or a property named like __Connection. This
field takes parameters allowing the client to specify which page to load, how many
items, and so on. This is where additional search parameters would be added as well.
type Query {
things(before: String, after: String, limit: Int): ThingConnection!
}
schema {
query: Query!
}
The resulting @[query][graphql-query] then would look something like this:
query GetThings($after: String) {
things(after: $after) {
pageInfo {
endCursor
hasNextPage
}
edges {
node { id }
}
}
}