Reuse a document for several reads
A client fetches different resources through one persisted or bundled document.
GraphQL · Function
Separate operations by defining multiple named GraphQL operations in one document, then selecting the desired operation at request time. This supports reusable documents and lets clients choose among queries, mutations, or subscriptions.
A GraphQL document may define multiple named operations. The client chooses the operation to execute by supplying its name separately from the document, while variables are supplied in a transport-specific dictionary.
query OperationName($variable: Type) { field }| Name | Type | Description |
|---|---|---|
document | GraphQL document | The GraphQL document containing one or more operations. |
operationName | string | The unique name of the operation to execute when the document contains multiple operations. |
variables | JSON object | Values for variables declared by the selected operation, supplied separately from the document. |
The response produced by executing the selected GraphQL operation.
query GetUser($id: ID!) {
user(id: $id) {
id
name
}
}
query GetPost($id: ID!) {
post(id: $id) {
id
title
}
}A document can contain multiple named operations; the client selects which one to execute.
A client fetches different resources through one persisted or bundled document.
A client needs several related mutations but executes only one per request.
A development or tooling workflow wants named operations for tracing and debugging.
A GraphQL document can contain several operations, such as queries or mutations. Each operation needs a unique name, and the request identifies which named operation the server should execute.
Follow-up: What happens if the client omits the operation name? The server cannot unambiguously select an operation when the document contains multiple operations.
The operation type indicates whether the operation is a query, mutation, or subscription. When an operation has a name, the type keyword must also be present.
Follow-up: Can the type keyword be omitted? Only for the shorthand form of a single query operation; mutations and subscriptions always require it.
Variables are declared on a named operation and supplied separately from the GraphQL document, usually in a JSON variables object. This keeps dynamic values out of the query text.
Follow-up: Why must the operation be named when variables are used? The explicit operation definition provides the place where the variable declarations belong.
querymutationsubscriptionoperation namevariablesaliases