Thuta Learning
GraphQL
BasicWeb Developmentbeginner

Writing Queries — Fields, Nesting, and Aliases

What you'll walk away with

  • Explain the core ideas behind Writing Queries — Fields, Nesting, and Aliases
  • Run the sample GraphQL query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

A GraphQL query is a tree-shaped selection set inside curly braces, listing exactly the fields you want. When an object-type field (such as `author`) includes a sub-selection, that becomes a nested query. If you need to call the same field twice with different arguments in one query, an alias (`featured: tutorials`) renames the result key so both can coexist.

Connect it to a real scenario

For the tutorial card component, request `tutorial(id: "42")` with `id`, `title`, and a nested `author { name }`. On the homepage, fetch a featured list and a latest list from the same `tutorials` field in one query using two aliases.

Try the working example

graphql
query TutorialCard {
  tutorial(id: "42") {
    id
    title
    author {
      name
    }
  }
  featured: tutorials {
    title
  }
  latest: tutorials {
    title
  }
}
You should see
You can write a query that uses nested field selection and aliases.

5-minute try-it

Query `tutorial(id: "7")` for `title` and nested `lessons { title }`, then call `tutorials` twice under `featured` and `archived` aliases.

One important caution

Leaving off the selection set under an object-type field causes a validation error — only scalar fields can be leaves.

GraphQL — Queries and MutationsGraphQL

Easy traps

  • Leaving off the selection set under an object-type field causes a validation error — only scalar fields can be leaves.
  • Validate sample queries and mutations on a local or test server with recoverable data before applying them to production.

Exercise

Query `tutorial(id: "7")` for `title` and nested `lessons { title }`, then call `tutorials` twice under `featured` and `archived` aliases.

You'll know it worked when: You can write a query that uses nested field selection and aliases.

Writing Queries — Fields, Nesting, and Aliases | Thuta Learning