Definition
Arguments
Overview
@defer is an executable directive.
Clients use it in operations, not in subgraph schemas.
It marks a fragment whose fields the client does not need in the initial response.
The router returns all other fields in the initial response.
The router delivers the deferred fragment in a later part of a multipart/mixed response.
The router provides the directive when you set engine.enable_defer to true.
You change no subgraph and no composition.
See Defer for the router configuration and the response format.
The Incremental Delivery spec draft defines @defer.
The draft is at RFC stage 2 of the GraphQL specification process.
The router follows the response format of that draft.
Supported locations
- Inline fragments without a type condition:
... @defer { ... }. - Inline fragments with a type condition:
... on Product @defer { ... }. - Fragment spreads:
...ProductReviews @defer.
Supported operations
- Queries, on root fields and on nested fields.
- Mutations, on nested fields below the root selection set.
Examples
Example schema
Deferring an inline fragment
id and name.
It announces the deferred fragment:
Deferring a fragment spread
Using Labels
pending entry:
Conditional defer
deferReviews: true, reviews arrives in an incremental response.
With deferReviews: false, reviews belongs to the initial response.
The router then sends a single JSON response.
Rules
- A
labelis a static string. The router does not accept a variable as a label. - Labels must be unique across all
@deferand@streamdirectives in the document. - The client can select the same field inside and outside a deferred fragment on the same object. The router then delivers the field in the initial response and drops the deferred copy.
- A deferred fragment without field selections has no effect.
ifaccepts a literal or a variable. The router resolves the variable value per request. The value belongs to the cache key of the query plan.
Federation behavior
The router handles@defer itself.
It plans the deferred fragment as a separate set of subgraph fetches.
Those fetches run after the router sends the initial response.
A deferred fragment can select fields of an entity that another subgraph owns.
The router then fetches the entity key in the primary phase.
It uses that key as the representation for the deferred entity fetch.
Subgraphs never receive the @defer directive.
See How the Router Executes @defer for the execution model.