Guides
Connection spec
On this page
The Connection spec is an opinionated specification of a convention for exposing a paginated list of items in a GraphQL schema. Smart clients like Relay use it to handle pagination for you.
Connections are sometimes called "Relay connections", but they are not specific to Relay: they are a best practice for any GraphQL API.
Most applications are built around lists of data, so a consistent way to read paginated lists lets clients build helpful, optimized abstractions for infinite scrolling, pagination, preloading and more.
Tip
When possible, model list fields as connections, even if your server does not paginate yet. You can then add pagination later without breaking clients.
The npm library graphql-relay provides helpful functions that implement simple connection fields, even from plain arrays.
Example connection
Gratos' support for generic types is a perfect fit for connections. Here is a generic, reusable Connection type. Feel free to copy these reusable types into your own code base. The spec requires pageInfo, hasNextPage and hasPreviousPage to be non-null, and Gratos makes every field nullable by default, so those three fields are marked @killsParentOnException.
import type { Int } from "gratos";
/** @gqlQueryField */
export function users(args: {
first?: Int | null;
after?: string | null;
last?: Int | null;
before?: string | null;
}): Connection<User> {
const users = [{ name: "John" }];
return connectionFromArray(users, args);
}
/** --- Reusable Connection Types --- */
/** @gqlType */
export type Connection<T> = {
/** @gqlField */
edges: Edge<T>[];
/**
* @gqlField
* @killsParentOnException
*/
pageInfo: PageInfo;
};
/** @gqlType */
export type Edge<T> = {
/** @gqlField */
node: T;
/** @gqlField */
cursor: string;
};
/** @gqlType */
export type PageInfo = {
/** @gqlField */
startCursor: string | null;
/** @gqlField */
endCursor: string | null;
/**
* @gqlField
* @killsParentOnException
*/
hasNextPage: boolean;
/**
* @gqlField
* @killsParentOnException
*/
hasPreviousPage: boolean;
};
/** @gqlType */
type User = {
/** @gqlField */
name: string;
};
// A minimal version of `connectionFromArray` from the module `graphql-relay`, whose
// cursors are opaque strings: here, each item's index.
export function connectionFromArray<T>(
data: ReadonlyArray<T>,
args: {
first?: Int | null;
after?: string | null;
last?: Int | null;
before?: string | null;
},
): Connection<T> {
if ((args.first ?? 0) < 0 || (args.last ?? 0) < 0) {
throw new Error("`first` and `last` must not be negative.");
}
// A cursor bounds the page only when it names an edge, as the spec's algorithm says.
const indexOf = (cursor: string | null | undefined): number | undefined => {
const index = Number(cursor);
return Number.isInteger(index) && index >= 0 && index < data.length && String(index) === cursor
? index
: undefined;
};
const after = indexOf(args.after);
const before = indexOf(args.before);
const lower = after === undefined ? 0 : after + 1;
// `before` is looked for among the edges `after` left.
const upper = before === undefined || before < lower ? data.length : before;
let start = lower;
let end = upper;
if (args.first != null) {
end = Math.min(end, start + args.first);
}
if (args.last != null) {
start = Math.max(start, end - args.last);
}
const edges = data.slice(start, end).map((node, index) => ({
node,
cursor: String(start + index),
}));
return {
edges,
pageInfo: {
startCursor: edges[0]?.cursor ?? null,
endCursor: edges.at(-1)?.cursor ?? null,
hasNextPage: end < upper,
// As the spec says: with `last`, more edges between the cursors than `last`, before `first`.
hasPreviousPage: args.last != null && upper - lower > args.last,
},
};
}type PageInfo {
endCursor: String
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
}
type Query {
users(after: String, before: String, first: Int, last: Int): UserConnection
}
type User {
name: String
}
type UserConnection {
edges: [UserEdge!]
pageInfo: PageInfo!
}
type UserEdge {
cursor: String
node: User
}Info
For connections served by Effect resolvers, a Node interface, and Relay reading them, see this site's Relay guide.