This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

GraphQL Cheatsheet

A quick reference guide for GraphQL: query syntax, arguments, aliases, fragments, mutations, schema definitions, scalars, unions, and HTTP endpoints.

· Published

GraphQL is a query language for APIs and a runtime for fulfilling queries with existing application data.

Queries

Basic Query & Response

{ status }

JSON Response:

{
  "data": {
    "status": "available"
  }
}

Nested Objects

{
  hero {
    name
    height
  }
}

Lists

{
  friends {
    name
  }
}

Arguments

{
  hero(id: "1000") {
    id
    name
  }
}

Aliases

{
  luke: hero(id: "1000") {
    name
  }
  han: hero(id: "1001") {
    name
  }
}

Operation Names & Variables

Query Definition:

query FindHero($id: String!) {
  hero(id: $id) {
    name
  }
}

Variables Payload (JSON):

{
  "id": "1000"
}

Mutations

mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
  createReview(episode: $ep, review: $review) {
    stars
    commentary
  }
}

Inline Fragments (Union & Interface Queries)

{
  search(q: "john") {
    id
    ... on User {
      name
    }
    ... on Comment {
      body
      author {
        name
      }
    }
  }
}

Over HTTP

GET Request

fetch('https://api.example.com/graphql?query={me{name}}')

POST Request

fetch('https://api.example.com/graphql', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    query: 'query GetHero($id: String!) { hero(id: $id) { name } }',
    variables: { id: '1000' }
  })
})

Schema Definition Language (SDL)

Basic Types

type Query {
  me: User
  users(limit: Int): [User]
}

type User {
  id: ID!
  name: String
}

Built-in Scalar Types

Scalar Description
Int Signed 32-bit integer
Float Signed double-precision floating-point value
String UTF-8 character sequence
Boolean true or false
ID Unique identifier serialized as string

Type Definition Keywords

Keyword Description
type Object type representing a set of fields
scalar Custom primitive scalar type
interface Abstract type defining a set of fields that types must implement
union Abstract type grouping multiple distinct object types
enum Enumeration of discrete allowed string values
input Complex object passed as an argument into queries or mutations

Type Modifiers

Modifier Description
String Nullable string
String! Non-nullable string
[String] Nullable list containing nullable strings
[String]! Non-nullable list containing nullable strings
[String!]! Non-nullable list containing non-nullable strings

Interfaces

interface Entity {
  id: ID!
}

type User implements Entity {
  id: ID!
  name: String
}

Enums

enum Direction {
  NORTH
  EAST
  SOUTH
  WEST
}

Unions

union SearchResult = Human | Droid | Starship

type Query {
  search(text: String): [SearchResult]
}

References

Related topics