GraphQL APIs
Let clients ask for exactly the data they need through a typed schema, resolvers and a single endpoint.
Intermediate · about 8 hours · difficulty 3/5
Learn first: Resource-Oriented REST APIs, ORMs and Query Builders
GraphQL is a query language and runtime for APIs. Instead of many endpoints, you expose one endpoint and a typed schema. Clients send queries describing the exact shape of data they want, and the server returns just that.
Core concepts
- Schema in SDL: types, fields,
Query(reads),Mutation(writes) andSubscription(real-time). - Resolvers: functions that fetch each field's data.
- Introspection: clients and tools can discover the schema, which powers great developer tooling.
When it shines
Many client types (web, iOS, Android) with different data needs, deeply related data, and teams that want a strongly typed contract between frontend and backend.
Costs and pitfalls
- N+1 in resolvers: resolving
authorfor each of 100 posts triggers 100 queries. Use the DataLoader pattern to batch. - Expensive queries: clients can request deeply nested data. Enforce depth and complexity limits and consider persisted queries.
- Caching is harder than with REST because everything is a POST to one URL.
- Authorization must be applied per field or per resolver, not only per endpoint.
Popular servers include Apollo Server, GraphQL Yoga, Strawberry (Python) and graphql-java or Spring for GraphQL.
You are ready to move on when you can build a GraphQL API with queries, mutations, DataLoader batching and a query depth limit.
Key concepts
- Schema definition language
- Queries, mutations, subscriptions
- Resolvers
- DataLoader batching
- Depth and complexity limits
- Apollo Server or GraphQL Yoga
- Federation
Mini project
Build a GraphQL API for a blog (users, posts, comments) and prove with query logs that DataLoader reduces a nested query from 100+ database calls to 3.
Check yourself
Why is HTTP caching harder with GraphQL than with REST, and what techniques help?
Free resources
- Learn GraphQL (GraphQL Foundation)
- Apollo Server documentation (Apollo)