Query execution
Run a read operation against a GraphQL schema and return the requested fields.
GraphQL · Function
GraphQL execution runs a parsed and validated operation against a schema. It resolves requested fields, completes their values, and returns data shaped like the client’s operation, potentially with errors.
Execution runs a validated GraphQL operation against an existing schema. It starts at the root Query, Mutation, or Subscription fields, invokes resolvers for requested fields, completes nested selections or scalar values, and produces a response that mirrors the query shape; resolvers can perform asynchronous work.
execute(schema, document, operationName, variableValues, initialValue)A GraphQL response whose data mirrors the requested operation; it may also include errors and extensions.
const result = await graphql({
schema,
source: '{ hello }',
rootValue: { hello: 'world' }
});
console.log(result.data);A server executes the parsed and validated operation against its schema and returns data shaped like the request.
Run a read operation against a GraphQL schema and return the requested fields.
Apply server-side state changes through a mutation's root fields.
Resolve event-driven subscription operations and produce results for events.
Assemble nested response objects whose keys match fields or aliases in the operation.
Execution begins with the operation's root type: Query, Mutation, or Subscription. The executor resolves requested fields, recursively completes object selections, and coerces leaf values to the schema's expected types.
Follow-up: How can a response contain both data and errors? A field resolver may fail after other fields have resolved, allowing partial data while the failure is reported in errors.
Each requested field is backed by a resolver. The resolver receives the relevant parent value and produces the next value; that value is then completed either by running a nested selection set or by coercing a scalar.
Follow-up: What happens for an asynchronous resolver? Execution can await asynchronous resolver results before completing the field and assembling the response.
The result is assembled as a key-value map while fields resolve. A field name, or its alias when one is present, becomes the key, so the returned structure mirrors the client's requested shape.
Follow-up: Why are aliases significant? They let multiple selections use distinct response keys even when they refer to fields with the same underlying name.
QueriesMutationsSubscriptionsResolversResponse