Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

GraphQL · Const

Query

A GraphQL query is a read operation that starts at the Query root and selects fields through nested objects to scalar or enum leaves. Its selection set lets clients request precisely the data they need, optionally using names, variables, aliases, and fragments.

How it works

A GraphQL query is a read operation that begins at the schema's Query root and follows a selection set through fields until it reaches scalar or enum leaf values. Clients may name the operation, pass variables, use aliases, and traverse related objects, while requesting only the fields needed.

Syntax

query OperationName($variable: Type) { field(argument: $variable) { nestedField } }

Parameters

NameTypeDescription
variable definitionsVariablesDefinitionOptional operation variables referenced by the query and supplied by the client.
selection setSelectionSetFields and nested selections requested from the Query root.

Returns

A response containing the selected fields in the shape requested by the query, with leaf values supplied by the server.

Key points

  • GraphQL defines queries, mutations, and subscriptions as its three primary operation types.
  • A query describes the fields the client wants rather than requesting an opaque response.
  • Every operation begins from a root operation type, with Query serving as the entry point for reads.
  • The selection set expresses the requested fields and can continue through nested object relationships.
  • Nested traversal lets one query retrieve related data without several separate round trips.
  • The operation declaration uses one of the three operation keywords to state its intent.
  • Selections must eventually resolve to scalar or enum leaf fields.

Common mistakes

  • The query keyword may be omitted only for shorthand query syntax; include it when naming an operation or declaring variables.
  • A named query must use the explicit query operation keyword rather than shorthand syntax.

Example: A named query with variables and nested fields

query GetUser($id: ID!) {
  user(id: $id) {
    id
    name
    posts {
      title
    }
  }
}

This named query reads a user and selected nested fields. The variable supplies the user ID, while the selection set controls exactly which scalar and nested values are returned.

When to use it

Precise data fetching

Read a resource and only the fields needed by a screen or service.

Nested resource retrieval

Fetch a resource together with related objects in one operation.

Parameterized reads

Reuse one operation shape with different runtime inputs.

Interview questions

QWhat is a GraphQL query?

A query is a read operation. It starts at the schema's Query root and traverses selected fields until scalar or enum leaf values are produced.

Follow-up: Why can a query contain nested selections? Because object-valued fields lead to related objects, whose fields must be selected before execution reaches leaves.

QWhen is the query keyword required?

The operation type identifies the intended operation, such as query, mutation, or subscription. It is optional only for eligible shorthand queries and is required when naming an operation.

Follow-up: What must be added when a document contains multiple named operations? The client must identify which operation to execute, commonly through the transport's operation-name mechanism.

QHow does a selection set control the result?

A selection set lists the fields the client wants, including nested fields for object values. GraphQL can therefore fetch related data in one request while returning the requested shape.

Follow-up: How does this differ from requesting an entire resource representation? The client specifies the fields and nested paths instead of accepting a fixed server response shape.

  • Mutation
  • Subscription
  • Selection set
  • Fields
  • Variables
  • Aliases
  • Fragments
  • Query root

Source: Queries. Checked 2026-10-15.