Pagination & Relationships
A GraphQL schema is not a flat list of records — it is a graph. The whole point of the type system is that a field on one type can return another type, so a single query can walk from a user to their posts to each post’s comments without a second round trip. Two questions decide how good that graph feels to use: how do types link to one another, and how do you page through the long lists those links produce?
This module answers both. We start with relationships — the edges of the graph — then look at the two dominant ways to paginate, and finish with the performance trap that catches almost everyone the first time they resolve a list of related data.
What this module covers
Section titled “What this module covers”By the end you will be able to model connections between types, choose a pagination style on purpose rather than by habit, and keep a relationship-heavy query fast. The five lessons are:
- Pagination & Relationships (you are here) — the big picture and a nested query.
- Relationships — one-to-many and many-to-many as fields that return types and lists.
- Offset Pagination — simple
limit/offsetpaging, and where it breaks down. - Relay Connections — the cursor connection spec:
edges,node,cursor,pageInfo. - N+1 and DataLoader — why a list resolver fires one query per item, and how batching fixes it.
This builds directly on what you already know about the type system and resolvers — a relationship is just a field whose type is another object type, and pagination is just arguments on a list field.
The shape of a relationship
Section titled “The shape of a relationship”flowchart LR Author["type Author"] Post["type Post"] Author -->|"posts(first): [Post!]! — needs paging"| Post Post -->|"author: Author! — single link"| Author
Reading the diagram: an Author has a posts field that returns a list of Post, and each Post has an author field pointing back. The list is where pagination lives — posts rarely returns every post, so it takes arguments like first or limit to ask for a window. The single link (author) needs no pagination because it returns at most one value.
A nested query, run for real
Section titled “A nested query, run for real”The example below defines Author and Post types, links them in both directions, and runs one query that crosses the relationship — fetching an author together with a window of their posts. Press Run to execute it with the real GraphQL engine.
Look at the output. We started at one author, stepped into a windowed list of posts (only two, because of limit: 2), and from each post stepped back into its author. One request, three hops across the graph. The rest of this module is about doing each of those hops well — modeling the links cleanly and paging the lists predictably.