Fields and selection sets

Beginner · 3 min

Learn the anatomy of a GraphQL query, from the operation to nested selection sets, how fields map to the response, and the rules the validator enforces.

Before thisObject types and fieldsYour first query in GraphiQL

My Digital Diaries (fka My Sitecore Diaries) · 8 min · from 5:13

Fields and selection sets

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by My Digital Diaries (fka My Sitecore Diaries)Report a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A query is an operation containing a selection set, a pair of braces listing the fields you want, and selection sets can nest as deep as the schema's links go.
  • The response mirrors the query: the same keys, in the same nesting, with values filled in.
  • A field of scalar type takes no selection set; a field of object type requires one, and the validator rejects either mistake before running anything.
  • The shorthand { ... } is an anonymous query; naming and typing an operation is optional for queries but recommended in real code.
  • Selecting the same field twice merges into one key in the response, and fields at the same level run independently of each other.

A query is a list of what you want

A GraphQL query reads like a JSON object with the values removed. You write the shape and the server fills it in.

query {
  book(id: "12") {
    title
    year
    author {
      name
      books { title }
    }
  }
}

Three terms cover everything here.

  • Operation. The whole thing, query { ... }. There are three kinds of operation: query, mutation, and subscription. A query only reads.
  • Field. A name you pick from the schema: book, title, author.
  • Selection set. The { ... } after a field that lists what to take from the thing it returns.

The top-level selection set picks fields from the schema’s Query type. book returns a Book, so its selection set picks from Book’s fields. author returns an Author, so it opens another selection set, and books goes back to Book. It is a walk along the schema’s links, one pair of braces per step.

Run on graphql-js 17.1.0 against a bookshelf:

{"data":{"book":{"title":"Cosmos","year":1980,"author":{"name":"Carl Sagan","books":[{"title":"Cosmos"}]}}}}

The result has the query’s own shape. If you remember one thing, make it that. It is why GraphQL responses are easy to consume: you know the shape before you send the request, because you wrote it.

Leaves and branches

Scalar and enum fields are leaves. Object, interface, and union fields are branches. The rule is simple:

  • A leaf must not have a selection set.
  • A branch must have one.

The validator enforces both:

{ book(id: "12") }
# Field "book" of type "Book" must have a selection of subfields. Did you mean "book { ... }"?

{ book(id: "12") { title { x } } }
# Field "title" must not have a selection since type "String!" has no subfields.

Neither ran a resolver. This is a safeguard against asking for an object with no instructions: “give me the book” is ambiguous, because a book has many fields, and GraphQL refuses to guess. It also means there is no SELECT *. You always name your fields.

Sibling fields and ordering

Fields at the same level are independent. Selecting books and book together works:

{ books { title } book(id: "10") { title } }

and the response contains both keys. The server may resolve them in parallel. For queries the spec lets the executor run sibling fields in any order. (Mutations differ, and are run one after another; see mutations.) The response keys come back in the order you wrote them.

Selecting the same field twice at the same level is allowed and merged. { book(id: "12") { title title year } } returned title once and year. When the same field appears with different arguments you have a conflict, which aliases resolve.

Operations, named and anonymous

The compact form { book(id: "12") { title } } is shorthand for an anonymous query. It is fine for exploring. In application code, name every operation:

query GetBook {
  book(id: "12") { title }
}

Names show up in server logs, error reports, and tracing, which is why teams enforce them. They also become required once a document holds more than one operation, and are covered with variables in variables and operation names.

Asking for what you need

The easiest GraphQL mistake is to select a lot because it is free to type. It is not free on the server. Every selected field is a resolver call, and a branch that fans out (like author { books { author { books ... } } }) multiplies work. Ask for the fields a screen actually uses. Depth and complexity limits covers how servers protect themselves when clients do not.

A good habit while exploring: build the query from the outside in. Start with the root field, add the branches, and add leaves last. GraphiQL’s autocomplete shows what each level offers.

What the video covers

This is a clip of a longer episode: My Digital Diaries’ “Anatomy of a GraphQL Query Explained” (GraphQL Fundamentals EP02, October 2026, 25 minutes). The embed spans 05:13 to 13:13, which the chapter list labels “Fields” and “Selection Sets”. Its description says the episode covers operation names, fields, selection sets, arguments, and aliases, and the next lessons use other chapters of it. I judged the clip from the chapter list and description, not a transcript. The channel’s own name says “(English)”, and the series is recent, which is why I chose it over older and shorter videos. The runner-up is Net Ninja’s “Query Basics” from 2023.

Where this fits

A selection set picks which fields. Often a field also needs input to know which data it means. Next: arguments.

Check yourself

  1. 01What is a selection set?

    The braces and the fields inside them, which say which fields of the current object to return.

  2. 02Why is { book(id: "12") } invalid?

    The book field returns an object type, so it needs a selection set naming the fields you want from it.

  3. 03What does the response look like relative to the query?

    It has the same shape, with each requested field as a key and its value in place, nested the same way.

  4. 04What happens when a query selects the same field twice at one level?

    The two selections are merged and the key appears once in the response.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark