How a GraphQL Request Works

A GraphQL request goes through several conceptual stages before the client receives a response.

Suppose the client sends:

query GetProduct($id: ID!) {
  product(id: $id) {
    id
    name
    price
  }
}

with variables:

{
  "id": "p100"
}

The server conceptually performs the following steps.

1. Receive the operation

The server receives the GraphQL document and any variables.

2. Parse

The GraphQL document is converted into an internal representation called an AST, or Abstract Syntax Tree.

3. Validate

The operation is checked against the schema. The server verifies that fields exist, arguments have valid types, required values are present, and selections are valid.

4. Execute

If validation succeeds, GraphQL executes the operation. Resolver functions are invoked to obtain the requested values.

5. Complete values

GraphQL processes returned values according to the field types. This includes handling nested objects, lists, scalar serialization, and nullability rules.

6. Return the response

The result normally contains a data property and may also contain an errors property.

For example:

{
  "data": {
    "product": {
      "id": "p100",
      "name": "Keyboard",
      "price": 2499
    }
  }
}

Understanding this lifecycle becomes extremely important later when debugging validation errors, resolver failures, null propagation, authorization behavior, and performance issues.

GraphQL Request Lifecycle
Key Takeaways
  • GraphQL parses the incoming document before execution.
  • Validation occurs against the schema.
  • Resolvers participate during execution.
  • The response can contain both data and errors.
  • Understanding the lifecycle is essential for debugging.
Knowledge Check

What happens if a GraphQL query requests a field that does not exist on the corresponding schema type?