Youssef Samir

Get in touch

GraphQL

A cheat sheet for common GraphQL queries, mutations, and best practices to optimize API interactions.

Introduction

GraphQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data.

Main Concepts

Schema

  • Schema: Defines the types and relationships in your data.
  • Type System: Strongly typed, including scalars (Int, Float, String, Boolean, ID) and object types.
  • Query: Entry point for read operations.
  • Mutation: Entry point for write operations.
  • Subscription: Entry point for real-time updates.

Queries

# Basic Query
query {
  user(id: "1") {
    id
    name
    email
  }
}

# Query with Parameters
query getUser($id: ID!) {
  user(id: $id) {
    id
    name
    email
  }
}

# Nested Query
query {
  user(id: "1") {
    id
    name
    posts {
      id
      title
    }
  }
}

Mutations

# Basic Mutation
mutation {
  createUser(input: { name: "John", email: "john@example.com" }) {
    id
    name
    email
  }
}

# Mutation with Parameters
mutation createUser($input: CreateUserInput!) {
  createUser(input: $input) {
    id
    name
    email
  }
}

Subscriptions

# Basic Subscription
subscription {
  userCreated {
    id
    name
    email
  }
}

# Subscription with Parameters
subscription onUserCreated($email: String) {
  userCreated(email: $email) {
    id
    name
    email
  }
}

Type Definitions

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

# Scalar Types
scalar Date

# Enum Type
enum Role {
  ADMIN
  USER
}

# Input Types
input CreateUserInput {
  name: String!
  email: String!
}

# Query Type
type Query {
  user(id: ID!): User
  users: [User]
}

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

# Subscription Type
type Subscription {
  userCreated: User
}

Directives

  • @include(if: Boolean): Includes the field only if the argument is true.
  • @skip(if: Boolean): Skips the field if the argument is true.
  • @deprecated(reason: String): Marks a field or enum value as deprecated.
query {
  user(id: "1") {
    id
    name
    email @include(if: $includeEmail)
  }
}

Variables

query getUser($id: ID!, $includeEmail: Boolean = true) {
  user(id: $id) {
    id
    name
    email @include(if: $includeEmail)
  }
}

Fragments

Reusable units for query structures.

# Fragment Definition
fragment userFields on User {
  id
  name
  email
}

# Using Fragments
query {
  user(id: "1") {
    ...userFields
  }
}

Error Handling

Errors in GraphQL are returned in the errors field of the response.

{
  "errors": [
    {
      "message": "User not found",
      "locations": [{ "line": 2, "column": 3 }],
      "path": ["user"]
    }
  ],
  "data": null
}

Tools and Libraries

  • Apollo Client: A comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL.
  • Relay: A JavaScript framework for building data-driven React applications with GraphQL.
  • GraphiQL: An in-browser IDE for exploring GraphQL.

Best Practices

  1. Define a Schema: Establish a strong schema that accurately represents your data model.
  2. Versioning: Avoid breaking changes by using techniques like field deprecation.
  3. Documentation: Use descriptions in your schema to provide clear documentation.
  4. Authentication and Authorization: Ensure your schema and resolvers handle authentication and authorization appropriately.
  5. Batching and Caching: Use tools like DataLoader to batch and cache requests to optimize performance.