Graphql Cheat Sheet

From WikiOD

Here is the cheatsheat of Graphql and basic guide on how to use Graphql

Intro[edit | edit source]

Queries[edit | edit source]

{: .-three-column}

Basic query[edit | edit source]

{ status }

[edit | edit source]

{ status: 'available' }

{: .-setup}

Nesting[edit | edit source]

{ hero { name height } }

[edit | edit source]

{ hero:
    { name: "Luke Skywalker",
      height: 1.74 } }

{: .-setup}

Lists[edit | edit source]

{ friends { name } }

[edit | edit source]

{ friends:
    [ { name: "Luke Skywalker" },
      { name: "Han Solo" },
      { name: "R2D2" } ] }

{: .-setup}

GraphQL queries look the same for both single items or lists of items.

Lookups[edit | edit source]

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

[edit | edit source]

{ hero:
    { id: "1000",
    { name: "Luke Skywalker" } }

{: .-setup}

Aliases[edit | edit source]

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

[edit | edit source]

{ luke:
    { name: "Luke Skywalker" },
    han:
    { name: "Han Solo" } }

{: .-setup}

Operation names and variables[edit | edit source]

Query[edit | edit source]

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

Just to make things less ambiguous. Also, to use variables, you need an operation name.

Variables[edit | edit source]

{ id: '1000' }

Mutations[edit | edit source]

Query[edit | edit source]

{ createReview($review) { id } }

Variables[edit | edit source]

{ review: { stars: 5 } }

[edit | edit source]

{ createReview: { id: 5291 } }

Mutations are just fields that do something when queried.

Multiple types[edit | edit source]

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

Great for searching.

Over HTTP[edit | edit source]

GET[edit | edit source]

fetch('http://myapi/graphql?query={ me { name } }')

POST[edit | edit source]

fetch('http://myapi/graphql', {
  body: JSON.stringify({
    query: '...',
    operationName: '...',
    variables: { ... }
  })
})

Schema[edit | edit source]

{: .-three-column}

Basic schemas[edit | edit source]

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

type User {
  id: ID!
  name: String
}

See: sogko/graphql-shorthand-notation-cheat-sheet

Built in types[edit | edit source]

Scalar types[edit | edit source]

Int | Integer |
Float | Float |
String | String |
Boolean | Boolean |
ID | ID |

Type definitions[edit | edit source]

scalar | Scalar type |
type | Object type |
interface | Interface type |
union | Union type |
enum | Enumerable type |
input | Input object type |

Type modifiers[edit | edit source]

String | Nullable string |
String! | Required string |
[String] | List of strings |
[String]! | Required list of strings |
[String!]! | Required list of required strings |

Mutations[edit | edit source]

type Mutation {
  users(params: ListUsersInput) [User]!
}

Interfaces[edit | edit source]

interface Entity {
  id: ID!
}

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

Enums[edit | edit source]

enum DIRECTION {
  LEFT
  RIGHT
}

type Root {
  direction: DIRECTION!
}

{: data-line=“1,2,3,4”}

Unions[edit | edit source]

type Artist { ··· }
type Album { ··· }

union Result = Artist | Album

type Query {
  search(q: String) [Result]
}

{: data-line=“4”}

References[edit | edit source]

Credit:rstacruz