GraphQL Cheatsheet

A beginner-friendly guide to GraphQL. Learn about schemas, queries, mutations, subscriptions, and best practices with examples and diagrams.

What is GraphQL?

GraphQL is a query language for APIs and a runtime for executing those queries. It lets clients ask for exactly the data they need, no more and no less.

GraphQL vs REST

Unlike REST, which often requires multiple endpoints, GraphQL allows fetching all required data in a single request.

REST:   /users/1/posts  → Multiple requests
GraphQL: { user(id:1) { name posts { title } } } → One request

Schema & Types

A GraphQL schema defines types and relationships in your API. It includes scalars (String, Int), enums, objects, lists, and non-nullables.

type User {
  id: ID!
  name: String!
  posts: [Post!]
}

type Post {
  id: ID!
  title: String!
  content: String
}

Queries

Queries are used to fetch data. You can nest fields, use aliases, fragments, and variables for flexibility.

query GetUser($id: ID!) {
  user(id: $id) {
    name
    posts {
      title
    }
  }
}

Mutations

Mutations change data (create, update, delete). They return the changed object.

mutation AddPost($title: String!) {
  addPost(title: $title) {
    id
    title
  }
}

Subscriptions

Subscriptions allow real-time updates over WebSockets.

subscription {
  postAdded {
    id
    title
  }
}

Directives

Directives provide extra instructions for queries.

query GetUser($withPosts: Boolean!) {
  user(id: 1) {
    name
    posts @include(if: $withPosts) {
      title
    }
  }
}

Best Practices

  • Ask only for the data you need to avoid overfetching.
  • Use fragments to keep queries reusable.
  • Define clear schema and stick to naming conventions.
  • Leverage directives and variables for flexibility.

How to use this page

  1. Start with understanding GraphQL vs REST.
  2. Learn schema and type definitions.
  3. Practice queries, then move on to mutations.
  4. Explore real-time subscriptions and directives.

🚀 Explore More Free Developer Tools

Don’t stop here! Supercharge your workflow with our other powerful converters & formatters.

💡 New tools are added regularly — bookmark DevUtilsX and stay ahead!

Want to support my work?

Buy me a coffee