GraphQL Schema & Query Cheat Sheet

Quick reference guide for GraphQL: Schema Definition Language (SDL), queries, mutations, resolvers, and subscriptions.

Data Formats
graphql
sdl
api

GraphQL is a query language for your API, and a server-side runtime for executing queries using a type system defined for your data.

Schema Definition Language (SDL)

graphql
# Scalar types: Int, Float, String, Boolean, ID

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

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

input CreateUserInput {
  name: String!
  email: String!
}

type Query {
  user(id: ID!): User
  users(limit: Int = 10): [User!]!
}

type Mutation {
  createUser(input: CreateUserInput!): User!
}

Query & Mutation Execution Syntax

graphql
# Query with aliases and variables
query GetUserProfile($userId: ID!) {
  activeUser: user(id: $userId) {
    id
    name
    email
    posts {
      id
      title
    }
  }
}

# Mutation
mutation AddNewUser($input: CreateUserInput!) {
  createUser(input: $input) {
    id
    name
  }
}

Fragments & Directives

graphql
fragment UserFields on User {
  id
  name
  email
}

query GetUsers($includePosts: Boolean!) {
  users {
    ...UserFields
    posts @include(if: $includePosts) {
      title
    }
  }
}

Common Pitfalls & Tips

[!WARNING] GraphQL APIs return HTTP 200 OK status codes even when errors occur! Always inspect the errors array in the JSON response payload.

[!TIP] Use DataLoader in backend GraphQL resolvers to batch and cache database requests to avoid the N+1 database query problem.