Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Projects

Project: A Blog with DataLoader and Pagination

Build a GraphQL blog API: posts, comments and authors, DataLoader batching with query counting, Relay cursor connections and a Python resolver variant.

A blog looks innocent: posts have an author and comments. But it is the textbook case for the two problems every GraphQL API meets as soon as it has real data: a list of posts triggers a storm of tiny database queries (the N+1 problem), and returning all posts is not an option once there are thousands.

In this project you fix both. You will batch lookups with a hand-written DataLoader and watch a query counter drop, build cursor-based connections, and then see the same batching idea in Python. A real GraphQL server cannot run here, so the schema is real SDL (read-only) and the resolver logic runs in small stand-ins. Read The N+1 Problem and DataLoader and Pagination first if you have not.

The schema

What the blog needs

Posts A paginated list, newest first.
Authors Each post has exactly one.
Comments Each post has many.

Every nested field is a chance for an extra query.

1 / 2
GRAPHQL
type Author {
  id: ID!
  name: String!
}

type Comment {
  id: ID!
  text: String!
}

type Post {
  id: ID!
  title: String!
  author: Author!
  comments: [Comment!]!
}

type PostEdge {
  cursor: String!
  node: Post!
}

type PageInfo {
  hasNextPage: Boolean!
  endCursor: String
}

type PostConnection {
  edges: [PostEdge!]!
  pageInfo: PageInfo!
}

type Query {
  posts(first: Int = 3, after: String): PostConnection!
}

Step 1: See the N+1 problem

The resolvers for Post.author and Post.comments run once per post. With 5 posts, a query for titles, authors and comments costs one query for the posts, five for the authors and five for the comments: 11 in total. The "database" below counts every query it receives. The loader collects every load(key) call made in the same tick (the same turn of the event loop), runs the batch function once with all keys, and hands each caller its own value. It also caches per key, so the same author is fetched only once per request.

Run it and compare the two counts. This is a hand-written teaching version; the real dataloader package (and Strawberry's or Graphene's equivalents) does the same job with more care for edge cases.

Node.js Counting queries with and without a loader

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Three rules make loaders correct:

  • Create loaders per request, in the context. A loader shared across requests would cache one user's data for another and never see updates.
  • The batch function returns values in the order of the keys, with null or an empty list for misses. Databases do not return rows in key order, so reorder them explicitly, as the example does.
  • The loader is the only path to that data. If one resolver bypasses it with a direct query, you lose the batching benefit.

Step 2: Cursor connections

Offset pagination (LIMIT 3 OFFSET 6) breaks when rows are inserted while someone pages, and it gets slower as the offset grows. A cursor says "continue after this exact item". The Relay connection format wraps results in edges (each with a cursor and a node) and a pageInfo. Cursors are meant to be opaque; encoding them in base64 is a common way to discourage clients from picking them apart.

The builder below pages over posts sorted newest first. It asks for one extra item to find out whether a next page exists, instead of running a separate count query.

JS A cursor connection builder

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In a real SQL-backed API the findIndex becomes a condition such as WHERE id < :lastId ORDER BY id DESC LIMIT :first + 1, which uses an index and stays fast on page 1,000. If you sort by something that is not unique (a date), include a unique tie-breaker (the id) in both the cursor and the ORDER BY. Also cap first on the server (say 100), otherwise first: 1000000 is an attack.

Step 3: The Python variant

Python servers meet the same problem. Strawberry ships strawberry.dataloader.DataLoader and Graphene works with aiodataloader (both read-only here; they are async and need those packages). The idea itself is plain Python: gather the keys, query once, and distribute the results. This runs in the browser in real Python:

Python Batching in Python, with a query counter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The Python version needs an explicit dispatch() because plain functions run one after another. Async libraries dispatch automatically at the end of the current event-loop tick, which is why resolvers can simply await loader.load(key).

Extend the project

  • Add Comment.author with a second author lookup and check that the author loader's cache is shared.
  • Add a totalCount field to the connection and discuss its cost on a large table.
  • Add last and before for backward paging.
  • Log queryCount per request in a test and fail the test if it exceeds a budget (Testing GraphQL APIs).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A query lists 20 posts with their authors. Without batching, how many queries typically run?

    That is the N+1 pattern: one query for the list, then one per item for the nested field.
  2. 2. Why should DataLoader instances be created per request?

    A loader caches values by key. Sharing it across requests could show one user's data to another and never refresh.
  3. 3. What must a batch function return?

    The loader matches results to callers by position, so missing keys need a null or empty value, and rows must be reordered.
  4. 4. Why fetch first + 1 items when building a connection?

    If the extra item comes back, hasNextPage is true; it is then dropped from the page.

Summary

  • Nested resolvers run once per parent, so lists cause N+1 queries unless you batch.
  • A DataLoader collects keys within a tick, calls the batch function once and caches per key.
  • Create loaders per request and keep results in key order.
  • Cursor connections (edges, node, cursor, pageInfo) page from a position, not a number.
  • Fetch one extra row for hasNextPage, use a unique tie-breaker and cap first.
  • The same collect-fetch-distribute idea works in Python.

Next: Project: A Secured API Layer.