Precise data fetching
Read a resource and only the fields needed by a screen or service.
GraphQL · Const
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.
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.
query OperationName($variable: Type) { field(argument: $variable) { nestedField } }| Name | Type | Description |
|---|---|---|
variable definitions | VariablesDefinition | Optional operation variables referenced by the query and supplied by the client. |
selection set | SelectionSet | Fields and nested selections requested from the Query root. |
A response containing the selected fields in the shape requested by the query, with leaf values supplied by the server.
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.
Read a resource and only the fields needed by a screen or service.
Fetch a resource together with related objects in one operation.
Reuse one operation shape with different runtime inputs.
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.
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.
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.
MutationSubscriptionSelection setFieldsVariablesAliasesFragmentsQuery root