The GraphQL cheatsheet covers schema definition, queries, mutations, subscriptions, variables, fragments, directives, and introspection. Includes real-world patterns for client and server development.
No results found
How to Use This GraphQL Cheatsheet
GraphQL is a query language and runtime for APIs. Unlike REST with multiple endpoints, GraphQL has a single endpoint where clients specify exactly what data they need. This cheatsheet covers both schema definition (server-side) and query syntax (client-side).
Schema First vs Code First
Schema-first: write the SDL (Schema Definition Language) first, then implement resolvers. Code-first: use a library like TypeGraphQL or NestJS to generate the schema from TypeScript decorators. Both approaches produce the same runtime SDL.
Variables Over Interpolation
Always use variables for dynamic values: query GetUser($id: ID!) { user(id: $id) { name } } with variables {"id": "123"}. Never interpolate values into the query string — it prevents caching and risks injection.
Frequently Asked Questions
Is this GraphQL cheatsheet free?
Yes, completely free with no signup. All examples are copyable.
What is the difference between GraphQL queries and mutations?
Queries are read operations (like GET in REST). Mutations are write operations (like POST/PUT/DELETE). Technically both are executed the same way, but mutations signal intent to modify data. Queries can run in parallel; mutations run serially by convention. Subscriptions are real-time event streams.
What are GraphQL fragments?
Fragments are reusable field selections. Define with fragment Name on Type { fields... } and use with ...FragmentName in queries. They prevent duplication when you need the same fields in multiple queries. Inline fragments (... on TypeName) are used for polymorphic types in unions and interfaces.
What is the N+1 problem in GraphQL?
When resolving a list of items, each item may trigger a separate database query (N queries for N items + 1 for the list). Example: fetching 100 users with their posts runs 100+1 queries. The DataLoader pattern batches and deduplicates these requests, turning N+1 into just 2 queries.
How do GraphQL variables work?
Variables allow dynamic values in queries without string interpolation. Define in the query signature: query GetUser($id: ID!) {...} and pass values separately as a JSON object: {"id": "123"}. This prevents injection attacks, enables query caching, and separates static structure from dynamic data.