Cheatsheet: GraphQL

Last updated 2026-06-21

Schema

Define object types and the root Query type.

type Query {
    user(id: ID!): User
}

type User {
    id: ID!
    name: String!
    age: Int
}

Use non-null fields and arguments with !.

type Query {
    field1: String!
    field2(arg: Int!): Int
}

Use list types for arrays of values or objects.

type Query {
    tags: [String!]!
    users(ids: [ID!]!): [User!]!
}

Define an input type for structured mutation arguments.

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

Define enums for a fixed set of allowed values.

enum Role {
    ADMIN
    USER
    GUEST
}

Define interfaces for fields shared by multiple object types.

interface Node {
    id: ID!
}

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

Queries

Execute a simple query with a nested selection set.

query {
    viewer {
        id
        name
    }
}

Pass arguments to fields.

query {
    user(id: "123") {
        name
        email
    }
}

Use aliases to request the same field with different arguments.

query {
    primary: user(id: "1") { name }
    secondary: user(id: "2") { name }
}

Use variables instead of hard-coding argument values.

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

Use fragments to reuse field selections.

query {
    user(id: "1") {
        ...UserSummary
    }
}

fragment UserSummary on User {
    id
    name
}

Use inline fragments for unions or interfaces.

query Search($term: String!) {
    search(term: $term) {
        ... on User { id name }
        ... on Repository { id name url }
    }
}

Operations and directives

Execute a mutation and request fields from the payload.

mutation CreateUser($input: CreateUserInput!) {
    createUser(input: $input) {
        user {
            id
            name
        }
    }
}

Execute a subscription for real-time updates where supported by the server.

subscription OnMessageAdded($roomId: ID!) {
    messageAdded(roomId: $roomId) {
        id
        text
        createdAt
    }
}

Conditionally include fields with @include.

query GetUser($withEmail: Boolean!) {
    viewer {
        name
        email @include(if: $withEmail)
    }
}

Conditionally skip fields with @skip.

query GetUser($hideEmail: Boolean!) {
    viewer {
        name
        email @skip(if: $hideEmail)
    }
}

Use operation names to identify requests in logs and tooling.

query ViewerProfile {
    viewer {
        login
        avatarUrl
    }
}

Introspection

List available query fields with introspection.

query {
    __schema {
        queryType {
            fields { name }
        }
    }
}

Inspect a type and its fields.

query {
    __type(name: "User") {
        name
        fields {
            name
            type { name kind }
        }
    }
}

See also: