GraphQL Cheat Sheet
Covers GraphQL schema definition, queries, mutations, resolvers, and common patterns like fragments and batching for flexible APIs.
2 PagesIntermediateFeb 28, 2026
Schema Definition Language
Defining types, queries, mutations, and subscriptions.
graphql
type Book { id: ID! title: String! author: Author! publishedYear: Int}type Author { id: ID! name: String! books: [Book!]!}type Query { books: [Book!]! book(id: ID!): Book}type Mutation { addBook(title: String!, authorId: ID!): Book!}type Subscription { bookAdded: Book!}
Queries & Mutations
Fetching and writing data with variables, aliases, and fragments.
graphql
# Query with variables and nested field selectionquery GetBook($id: ID!) { book(id: $id) { title author { name } }}# Mutationmutation AddNewBook { addBook(title: "Dune", authorId: "42") { id title }}# Aliases and fragmentsquery { dune: book(id: "1") { ...BookFields } hobbit: book(id: "2") { ...BookFields }}fragment BookFields on Book { title publishedYear}
Resolver Example
Apollo Server-style resolver map, including a nested field resolver.
javascript
const resolvers = { Query: { books: (parent, args, context) => context.db.books.findAll(), book: (parent, { id }, context) => context.db.books.findById(id), }, Mutation: { addBook: (parent, { title, authorId }, context) => context.db.books.create({ title, authorId }), }, Book: { // Nested resolver to resolve the author field author: (book, args, context) => context.db.authors.findById(book.authorId), },};
Core Concepts
Vocabulary every GraphQL API relies on.
- Query- Read-only operation; client specifies the exact shape of the response
- Mutation- Operation that writes/modifies data on the server
- Subscription- Long-lived operation that streams updates, typically over WebSockets
- Resolver- Function that returns the data for one specific field in the schema
- Schema- Strongly-typed contract (SDL) defining every possible query, mutation, and type
- N+1 problem- Naive resolvers issuing one DB query per item; fixed with batching (DataLoader)
- Introspection- Ability to query the schema itself, used by tools like GraphiQL
Pro Tip
Use DataLoader (or an equivalent per-request batching cache) inside resolvers to avoid the N+1 query problem — without it, fetching 100 books' authors triggers 100 separate database round-trips.
Was this cheat sheet helpful?
Explore Topics
#GraphQL#GraphQLCheatSheet#WebDevelopment#Intermediate#SchemaDefinitionLanguage#QueriesMutations#ResolverExample#CoreConcepts#Databases#APIs#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance