Queries & Mutations

{
  books {
    title
    author
  }
}

Short Syntax

Query

{
  books(where: { id: "cku14lfvs7u5m0b07bbxkto86"}) {
    id
    title
    author
  }
}

Arguments

Query

query getBookById($id: ID! = "cku14rhh47zgi0b48ap2m21xx") {
  books(where: { id: $id }) {
    title
  }
}

Operation Name & Variables

Query

query Books($withGenres: Boolean!) {
  books {
    genres @include(if: $withGenres) {
     title
    }
 }
}

Directives

Query

{
  fantasy: books(where: {genres_every: { title: "Fantasy" }}) {
    genres {
      id
      title
    }
  }
  scifi: books(where: {genres_every: { title: "Sci-Fi" }}) {
    genres {
      id
      title
    }
  }
}

Aliases

Query

mutation {
  createBook(data: {title: "A new hope", author: "George Lucas"}) {
    id
    title
    author
  }
}

Mutation

mutation {
  publishBook(where: { id: "cku14rhh47zgi0b48ap2m21xx" }) {
    title
  }
}

Mutation

Queries vs Mutation

A mutation can contain multiple fields, just like a query. There's one important distinction between queries and mutations, other than the name:

 

While query fields are executed in parallel, mutation fields run in series, one after the other.

fragment bookFields on Book {
  title
  author
  genres {
    title
  }
}

{
  archived: books(where: { archived: true }) {
    ...bookFields
  }
  notArchived: books(where: { archived: false }) {
    ...bookFields
  }
}

Fragments