GraphQL Tutorial
Learn GraphQL step by step: 37 lessons in 10 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is GraphQL? GraphQL is a query language for APIs where the client names exactly the fields it needs. Learn the problem it solves, its four parts and how a query runs.
- GraphQL vs REST Compare GraphQL and REST with the same data: round trips, payload size, versioning, caching and tooling, plus when plain REST is still the better choice.
- Your First Query Read and write your first GraphQL query: operations, selection sets and fields, the data and errors envelope, and how a query travels as JSON over POST.
- GraphiQL, Playground and Sandbox Tools Compare GraphiQL, GraphQL Playground, Apollo Sandbox, Insomnia and Postman: the editor, variables and headers panes, history, the schema explorer, embedding, and turning tools off in production.
- Setting Up a Server: Express, Apollo Server and Yoga Set up a GraphQL server in Node.js step by step: install, write typeDefs and resolvers, start Apollo Server or GraphQL Yoga, mount on Express, and test the endpoint with curl.
Writing Queries
- Fields, Arguments and Nested Selections Learn how GraphQL fields work: scalar and object fields, nested selections through relationships, arguments on any field and default values.
- Aliases and Fragments Use GraphQL aliases to request the same field twice and fragments to stop repeating selections. Covers named and inline fragments with type conditions.
- Variables and Directives Pass dynamic values safely with GraphQL variables, name your operations, and switch fields on or off with the @include and @skip directives.
- Mutations and Subscriptions Learn the three GraphQL operation types: queries read, mutations change data one after another, and subscriptions stream events. Includes runnable stand-ins.
The Schema
- Schema Definition Language: Types and Scalars Read and write GraphQL's Schema Definition Language: object types, the five built-in scalars, root types and descriptions, with a runnable SDL parser stand-in.
- Lists and Non-Null Types Understand ! and [ ] in GraphQL: the four list/non-null combinations, what null means, and how an error bubbles up to the nearest nullable field.
- Enums, Interfaces and Unions Model fixed value sets with enums and shared or alternative shapes with interfaces and unions. Learn __typename and inline fragments with a runnable stand-in.
- Input Types, Custom Scalars and Introspection Learn input objects, custom scalars like DateTime, the @deprecated directive and introspection queries (__schema, __type), with runnable stand-ins.
Resolvers and Execution
- Resolvers Learn how GraphQL resolvers work: the (parent, args, context, info) signature, default resolvers, field-by-field execution and context, with a runnable executor.
- How Execution Works and How Errors Behave Follow a GraphQL request through parse, validate and execute, and learn how field errors, the errors array, null bubbling and serial mutations behave.
- The N+1 Problem and DataLoader Why nested GraphQL resolvers fire one query per item, and how batching with a per-request DataLoader cuts hundreds of queries down to two.
Schema Design
- Schema Design Best Practices Design a GraphQL schema that lasts: model the domain, name things consistently, write specific mutations with payload types, and avoid flags and JSON blobs.
- Pagination: Offset, Cursor and Connections Paginate GraphQL lists the robust way: see why offsets drift, then build cursor pagination with edges, nodes, pageInfo and opaque base64 cursors.
- Modeling Errors Decide which failures belong in the top-level errors array and which belong in the schema, and model them with result unions, payload fields and codes.
- Global Object Identification and the Node Interface Learn the Node interface and the node(id) query: opaque global IDs, refetching any object by ID, how caches benefit, and what Relay-compatible schemas expect.
Security and Performance
- Authentication and Authorization Secure a GraphQL API: authenticate once in HTTP middleware, put the user in context, enforce rules in the business layer, and control access per field.
- Query Depth, Complexity and Other Attacks Learn how abusive GraphQL queries bring servers down and how depth limits, cost analysis, timeouts, persisted queries and rate limits defend against them.
- Caching and Performance Why HTTP caching is harder with GraphQL, and how persisted queries, response and field caches, normalized client caches and resolver timing fix it.
Clients and Transport
- GraphQL over HTTP See exactly what a GraphQL request and response look like on the wire: POST bodies, GET for persisted queries, status codes, CORS, and WebSocket or SSE.
- Clients, Apollo and Code Generation Compare plain fetch with Apollo Client, urql and Relay, and learn normalized caching, optimistic updates and generating TypeScript types from a schema.
- Subscriptions, Live Updates and File Uploads Compare subscriptions, polling and live queries, learn pub/sub topics and event filtering, and see why file uploads usually happen outside GraphQL.
- GraphQL with NestJS See how NestJS turns the GraphQL ideas you know into classes: code-first and schema-first schemas, resolver classes, field resolvers, context, and where to read more.
Evolving and Testing
- Testing GraphQL APIs Learn how to test GraphQL APIs: unit-test resolvers, run queries against the schema without HTTP, test auth and errors, and check schema contracts in CI.
- Schema Evolution, Federation and Tooling Change a GraphQL schema without breaking clients: additive changes, deprecation, breaking-change detection, registries, federation and field usage.
- GraphQL Server Architectures Compare GraphQL server architectures: a server connected to a database, a layer over existing services, the hybrid approach and a backend-for-frontend, with trade-offs and examples.
Projects
- Project: A Bookstore Query API Build a read-only GraphQL bookstore API: write the SDL for authors, books and reviews, add resolvers over in-memory data and run nested filtered queries.
- Project: A Todo API with Mutations Build a todo API in GraphQL: input objects, mutation payload types, validation errors as data and an optimistic client flow, all as runnable stand-ins.
- 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.
- Project: A Secured API Layer Combine auth context, role-based field access, depth and cost limits, safe error formatting and tests in one runnable GraphQL security stand-in.
Practice and Review
- Practice Exercises with Solutions 15 graded GraphQL exercises: write and fix queries, repair schemas, design a mutation, find the N+1, add pagination and spot nullability bugs.
- GraphQL Interview Questions 35 GraphQL interview questions with concise answers: concepts, schema, resolvers, N+1, security, caching, federation and REST, plus runnable stand-ins.
- GraphQL Cheat Sheet A one-page GraphQL cheat sheet: query syntax, SDL types and modifiers, directives, operation patterns, error and pagination shapes, pitfalls and lesson links.