Queries & Fields — GraphQL

A query reads data: write the fields you want, and the response has the same layout, holding only those fields (title alone is 48 bytes, against 76 bytes for…

You hand a note to a friend who's going to the bookshop: "Book number 1: title and price. From its author: just the name." Your friend comes back with the same note, every line filled in, and nothing added that you didn't write.

A GraphQL query works like that. A query is how you read data: you write the fields you want, nested the way the data is related, and the server answers with JSON laid out the same way.

Picking fields: the selection set

Every example uses the Bookshop schema from the previous lesson. The simplest query:

{
  book(id: 1) {
    title
    price
  }
}
{ "data": { "book": { "title": "The Rainbow Troops", "price": 89000 } } }

The result is 62 bytes. Compare that with REST's GET /books/1, which always sends five fields (76 bytes). Ask for title alone and the answer shrinks to 48 bytes. That is the main gain of picking fields: data nobody needs isn't sent, which matters most on mobile networks.

The rule is the same in any schema: { user(id: 1) { name email } } returns the name and email of the user with id 1, nothing more.

Two rules for selection sets:

Nested fields

An object field can hold more object fields, as deep as the schema allows. Author number 1 with their books:

{
  author(id: 1) {
    name
    books {
      title
      price
    }
  }
}
{
  "data": {
    "author": {
      "name": "Andrea Hirata",
      "books": [{ "title": "The Rainbow Troops", "price": 89000 }]
    }
  }
}

books is a list ([Book!]!), so the result is an array, and its selection set applies to every book in it.

Arguments

An argument is a value you send to a field, written in parentheses after its name. The schema decides which field takes which arguments:

{
  books(status: IN_STOCK) {
    title
    stock
  }
}
{ "data": { "books": [{ "title": "The Rainbow Troops", "stock": 12 }, { "title": "Beauty Is a Wound", "stock": 5 }] } }

Aliases: the same field twice

Two book fields with different arguments in one query would fight over the same key in the response, "book":

{
  book(id: 1) {
    title
  }
  book(id: 2) {
    title
  }
}

The server rejects it:

{
  "errors": [
    {
      "message": "Fields \"book\" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.",
      "locations": [{ "line": 2, "column": 3 }, { "line": 5, "column": 3 }]
    }
  ]
}

The whole query is rejected, with no data. The fix is an alias, another name for the key in the response, written newName: field:

{
  rainbow: book(id: 1) {
    title
  }
  mankind: book(id: 2) {
    title
    stock
  }
}
{
  "data": {
    "rainbow": { "title": "The Rainbow Troops" },
    "mankind": { "title": "This Earth of Mankind", "stock": 0 }
  }
}

An alias only renames the key in the response. The field that runs is still book, and each alias may pick different fields.

__typename: the type's name

Every object has a built-in __typename field holding its type's name in the schema. It doesn't need to be written in the schema:

{
  book(id: 1) {
    __typename
    title
    author {
      __typename
      name
    }
  }
}
{ "data": { "book": { "__typename": "Book", "title": "The Rainbow Troops", "author": { "__typename": "Author", "name": "Andrea Hirata" } } } }

It's useful when one field can hold several types (unions and interfaces), and libraries such as Apollo Client use it to cache data per type.

Fields that aren't in the schema

A field that doesn't exist is rejected before anything runs. If its name is close to an existing field, the server suggests it:

⚠️ Small slips that get a query rejected

📌 Operation names and field order

An operation can have a name: query BookDetail { book(id: 1) { title } }. The result is the same, but the name shows up in server logs and devtools, so it's easy to find. When one document holds more than one operation, each must be named, and the request must say which one to run with operationName. Without it the server answers Must provide operation name if query contains multiple operations.

The keys in the response follow the order of the fields in the query: { book(id: 1) { price title } } answers { "price": 89000, "title": "The Rainbow Troops" }. Values that change, such as an id from user input, are sent as variables, covered in the "Variables & Fragments" lesson.

TL;DR: A query reads data: write the fields you want, and the response has the same layout, holding only those fields (title alone is 48 bytes, against 76 bytes for /books/1). An object field must have a selection set { ... }, a scalar field must not. Fields can be nested (author { books { title } }), and lists give arrays. Arguments go in parentheses: required when marked !, and only those in the schema. Two of the same field with different arguments need aliases (rainbow: book(id: 1)), otherwise the whole query is rejected. __typename gives the type's name. A field that doesn't exist is rejected, sometimes with a Did you mean suggestion.

⚡ Pick fields with a selection set

Write:

{
  book(id: 1) {
    title
    price
  }
}

Meaning: The braces after book are the selection set: the list of fields picked. Only title and price are sent, 62 bytes, while GET /books/1 always sends five fields (76 bytes). Data nobody needs isn't sent.

⚡ An object must have a selection set

Guess first: you write just author, with no braces. Does the server send all of the author's data?

No. The query is rejected. A field whose type is an object must have a selection set, so write author { name }. The other way round, a scalar field like title must not have one.

Meaning: GraphQL has no * like SQL. Every field you want has to be written out, nested ones too: author { books { title } }.

⚡ Aliases: the same field twice

Write:

{
  rainbow: book(id: 1) {
    title
  }
  mankind: book(id: 2) {
    title
    stock
  }
}
Guess first: two book fields with different arguments and no aliases. What does the server answer?

The whole query is rejected: Fields "book" conflict because they have differing arguments. The two fields fight over the same key, "book", in the response.

Meaning: An alias only renames the key in the response. The field that runs is still book.

What you will learn