Module P-11·27 min read

Schema definition language, queries/mutations/subscriptions, resolvers, the N+1 problem and DataLoader, auth in GraphQL — and when to choose GraphQL over REST.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module P-11 — GraphQL with Apollo Server

What this module covers: REST and GraphQL solve the same problem — exposing data over HTTP — with different trade-offs. GraphQL gives clients precise control over what data they receive, eliminates over-fetching and under-fetching, and makes schema changes visible and versioned. This module covers the Schema Definition Language, building a GraphQL API with Apollo Server and Express, writing resolvers for queries and mutations, implementing subscriptions for real-time data, solving the N+1 problem with DataLoader, adding authentication, and knowing when to reach for GraphQL instead of REST.


REST vs GraphQL: The Core Trade-Off

A GraphQL query is a restaurant order where you list precisely which items you want off the menu; REST is a fixed set-menu — you get everything on the plate whether you wanted the pickled onions or not.

Consider a mobile screen showing a user's profile with their five most recent posts and follower count.

REST approach — three requests:

text

Problems:

  • Three round-trips (three network waterfalls on mobile)
  • /users/42 returns 20 fields; the screen needs 4
  • You're either over-fetching (wasting bandwidth) or under-fetching (needing more calls)
  • Adding a new screen requirement changes which endpoints you need to call

GraphQL approach — one request, exactly the data needed:

graphql

One request, four fields from user, three fields per post. The client defines the shape. Adding a new screen just changes the query.

GraphQL shines when: you have multiple clients with different data needs (web, mobile, partner integrations), your data is deeply relational, or your API is consumed by teams you don't control.

REST shines when: you have a simple CRUD API, need HTTP caching, are building a public API that tools should be able to discover automatically, or your team knows REST well.


Setup: Apollo Server with Express

bash
typescript

Context: Authentication in GraphQL

GraphQL doesn't have middleware like Express. Authentication goes into the context function — called once per request and passed to every resolver:

typescript

Resolvers

Resolvers are functions that return the data for each field. They receive (parent, args, context, info):

typescript
typescript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.