How GraphQL works inside

Lesson 2.5 · 4 min read · by Vinay C

We'll cover the following

Visual summary

Infographic summarizing How GraphQL works inside: A schema defines types, and each field has a resolver, a small function that knows where to get that field (a DB…

Download imageFree to share, please link back to this lesson.

A schema defines types, and each field has a resolver, a small function that knows where to get that field (a DB query, a REST call, a gRPC call, another service). The server parses the query, runs only the resolvers for requested fields, and assembles one JSON response. If you ask only for name, the email resolver never runs.

The N+1 problem and batching

News feed query: { feed(limit: 10) { title author { name } } }. One call loads the 10 most recent posts. Then a naive author resolver runs once per post and calls the user service each time: 1 + 10 = 11 calls. At 100 posts it is 101. The author IDs are arbitrary (say 87, 12, 305, 12, 87, ...) and often repeat. The fix is batching: a tool like DataLoader collects every author lookup from that one query, removes duplicates, and issues one batched call.

SELECT id, name FROM users WHERE id IN (12, 87, 305, ...)   -- 1 call for all authors, not 10

Column selection: not magic

  • Batching and column picking happen in your resolver and data-layer code, not in GraphQL itself.
  • A well-built resolver reads which fields were requested and selects only those columns (SELECT name). Prisma, Hasura and Postgraphile can do this automatically.
  • The naive version does SELECT * and lets GraphQL filter the response. The client still gets only name; the waste is between your server and the DB.
  • ORMs help but do not batch by themselves; you wire DataLoader up.
Client query -> GraphQL layer (which fields?) -> resolver -> batched DB or service call

At scale

Federation (Apollo Federation) lets each team own part of the schema while one gateway stitches them together.

Trade-offs

  • Pro: no over-fetching or under-fetching, and fewer round trips for mobile.
  • Con: one expensive query can overload backends, so add depth and complexity limits.
  • Con: HTTP caching is harder than REST (everything is a POST to one URL), so caching moves to the resolver or field level.
  • Con: you must handle N+1 yourself.

Pick it when many clients (web, mobile, partners) have different data needs. Skip it for simple CRUD or internal service-to-service calls, where REST or gRPC is simpler.

Summary: say it in the interview

  • "GraphQL resolvers batch with DataLoader to avoid N+1, and select only the requested fields."