Adapters customize the behavior of your GraphQL system by providing helpers, metadata configuration, and document transformation capabilities.
Use defineAdapter to create a typed adapter:
Helpers are custom functions injected into the gql() callback context. They provide a centralized place for reusable logic.
Helpers are available in the gql() callback:
| Benefit | Description |
|---|---|
| Centralized Logic | Define once, use everywhere |
| Type Safety | Full TypeScript inference |
| Testability | Helper functions can be unit tested |
| Consistency | Ensure consistent patterns across operations |
Document transformation allows you to modify the GraphQL AST at build time. Common use cases include adding directives, modifying field selections, or injecting metadata.
| Level | Receives | When to Use |
|---|---|---|
| Adapter | schemaLevel, fragmentMetadata | Schema-wide transforms (e.g., add auth directive to all queries) |
| Operation | Typed metadata | Per-operation transforms based on operation metadata |
Operation-level transforms run first, then adapter-level transforms are applied to the result.
Runs for all operations. Access schema-level configuration and aggregated fragment metadata:
Runs per-operation with access to typed operation metadata:
Even for operation-level transforms, define the transform logic in helpers to keep operations clean and reusable:
This approach:
Schema-level values are fixed configuration available to all operation metadata builders:
Access in metadata builders:
When operations spread fragments, their metadata is aggregated using aggregateFragmentMetadata:
The aggregated metadata is available in:
fragmentMetadata parametertransformDocument via fragmentMetadata argument