Cheatsheet: GraphQL

Schema

Define a GraphQL schema

type Query {
    field1: String
    field2: Int
}

not null fields or arguments

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

array type

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

Execute a query

Execute a query

query {
    field1
}

Execute a query with arguments

query {
    field1(arg: "value")
}

Execute a query with aliases

query {
    field1(arg: "value1"): firstValue
    field2(arg: "value2"): secondValue
}

Execute a query with fragments

query {
    field1 {
        ...fragmentName
    }
}

fragment fragmentName on Type {
    field1
    field2
}

Mutations

Execute a mutation

mutation {
    mutationName(arg: "value") {
        field1
    }
}

Define an input type

input InputType {
    field1: String
    field2: Int
}