Federation and Relay support
Strawberry supports two GraphQL conventions that shape how clients and services use a schema. Federation lets several GraphQL services act as one composed graph. Relay adds stable object identity, refetch support, and predictable pagination for clients.
Federated GraphQL services
Federation turns each service into a subgraph that can be composed by an Apollo Gateway or Router. A federated schema publishes its service description and entity entry points so the gateway can plan a request across subgraphs and join the results.
The federation model separates two jobs:
- A subgraph owns fields and returns the data it can provide.
- The gateway uses entity keys to pass an object reference to another subgraph.
Strawberry targets Apollo Federation 2. It supports federation versions 2.0 through 2.11 and uses version 2.11 by default. The selected version controls which federation directives are valid in the schema. See Federation support for the schema features and directive surface. The related schema and type system page explains how these schema definitions fit the wider GraphQL model.
Custom schema directives can also take part in composition. A directive marked for composition is made known to the composed graph, and an import URL can identify the directive source when the graph uses it across subgraphs. The schema collects directives used in the schema before it builds the served schema. It then adds the needed link and composition declarations once, so the declarations match the schema that clients and gateways receive.
Federation also supplies schema types for field sets and link imports. These values let federation directives describe field selections and imported specifications without changing the meaning of normal application fields.
The main request path is shown below. The gateway composes service descriptions before it routes an operation.
flowchart LR
A[Subgraph schemas] --> B[Schema composition]
B --> C[Composed graph]
D[Client operation] --> E[Gateway or Router]
C --> E
E --> F[Service field requests]
F --> G[Entity references]
G --> H[Joined client result]
References: strawberry/…/schema.py• L46-121 strawberry/…/schema.py• L266-400 strawberry/…/types.py• L7-30 strawberry/…/schema_directive.py• L13-59 strawberry/…/versions.py• L1-29 docs/…/federation.md• L14-20
Federated entities
An entity is a type that a gateway can identify and request again from another subgraph. A federation key acts as the shared identity for that type, while an entity reference resolver turns the key data into the local object needed to resolve fields.
Federated schemas add gateway-facing fields for these tasks:
_service { sdl }exposes the subgraph schema description for composition._entitiesaccepts representations from the gateway when the schema has keyed entity types.- Federation directives describe keys, field dependencies, shared fields, visibility, and service ownership.
The schema adds _entities only when it finds keyed object types reachable from the root operations or additional schema types. The discovery walks the reachable type graph and builds a stable entity union from those types. This keeps entity entry points tied to the services that actually expose entities. The _Any representation scalar carries the gateway input used by this entry point.
Federation-aware type decorators can mark object and interface types with keys and other type-level rules. An interface object can represent an interface in a subgraph so the gateway can use the same entity model for that interface.
The schema adds federation fields without replacing matching fields already defined by the application query type. It also preserves the query type's metadata when it extends that type. This lets a service keep its own schema contract while exposing the federation entry points it needs.
The entity flow is therefore driven by the composed graph rather than by a direct client call to a single service. A request can begin with a field in one subgraph, carry the entity key to another subgraph, and return the additional fields as one result.
References: strawberry/…/schema.py• L123-264 strawberry/…/schema.py• L402-471 strawberry/…/object_type.py• L23-56 strawberry/…/object_type.py• L195-260 strawberry/…/params.py• L111-162 docs/…/federation.md• L107-123
Relay GraphQL conventions
Relay support gives a schema a common contract for object identity and list navigation. A Relay node has a globally unique id, and a connection presents a list through edges plus page information.
These conventions help a client use different object types through the same patterns:
- A
Nodecan be fetched again from its global ID. - A
Connectiondescribes a page of objects. - An
Edgecarries one object and its cursor. PageInforeports the cursors and whether more items exist.
See Relay nodes and connections for the node, global ID, connection, and field contracts. The GraphQL execution page explains how these fields participate in request execution.
References: strawberry/…/types.py• L71-143 strawberry/…/types.py• L354-379 strawberry/…/types.py• L629-694 strawberry/…/__init__.py• L1-33 docs/…/relay.md• L14-24
Relay node identity
A Relay global ID combines the GraphQL type name with the application-level node ID and encodes the pair as a string. A NodeID marks the value used to create that ID, while node resolution maps the decoded value back to one object or to a list of objects.
The identity contract has two parts:
- The schema exposes a stable
id: ID!for each node. - The application resolves that ID to the correct type and object when a client uses the node field or a mutation input.
The shared node field can return one node, an optional node, or a list. Lists can contain different node types, which lets a client refetch objects through one entry point.
References: strawberry/…/types.py• L96-253 strawberry/…/types.py• L321-342 strawberry/…/types.py• L354-626 strawberry/…/utils.py• L15-70 strawberry/…/fields.py• L57-216 docs/…/relay.md• L37-89
Relay connections and pagination
A Relay connection turns an application result into a page with edges and pageInfo. The connection field adds the standard before, after, first, and last arguments, then applies the selected pagination policy to the returned iterable.
Strawberry provides a list-based connection for common cases and a base connection for application-specific pagination. A service can return a list, iterator, generator, or their asynchronous forms. The default result limit comes from the schema configuration and can be changed for an individual connection field. The list connection also checks the query selection before resolving page data, so it can avoid work when the client does not request connection edges or page information.
Pagination has a clear contract between the field and the client:
- Cursors identify positions in the connection.
firstandaftermove forward through a result set.lastandbeforemove backward through a result set.PageInfostates whether another page is available.
When data access has its own pagination method, a custom connection can keep that policy while preserving the same GraphQL shape. See Relay nodes and connections for the supported field and pagination choices.
References: strawberry/…/fields.py• L219-372 strawberry/…/fields.py• L396-513 strawberry/…/types.py• L658-953 strawberry/…/utils.py• L73-183 docs/…/relay.md• L91-147
Federation and Relay boundaries
Federation and Relay solve different coordination problems. Federation coordinates data between services through composition and entity references. Relay coordinates client access to objects and lists through stable IDs and pagination.
They can describe different parts of the same GraphQL API, but they do not replace each other. Choose federation when service ownership and cross-service queries matter. Choose Relay conventions when clients need a common way to refetch objects or move through large result sets.
References: strawberry/…/schema.py• L123-164 strawberry/…/types.py• L528-557 docs/…/federation.md• L186-207 docs/…/relay.md• L145-147
Change checklist
When these files or sections are changed, remember to consider these:
- When the federation version list or directive rules change, check the minimum-version validation and the generated
@linkversion used by composed schemas. - When an entity key changes, check every subgraph that sends or resolves that representation. The key fields must remain sufficient for reference resolution.
- When federation fields or directives change, check access controls around
_serviceand_entities. These fields serve gateways but can expose schema or entity data to other callers. - When a custom schema directive is marked for composition, check that its composed name and import source match the contract expected by the gateway.
- When federation field-set or link types change, check the directive arguments and the generated scalar or enum names used in the federation SDL.
- When query fields use names that match federation fields, check both the GraphQL names and the Python names. A collision must not hide an application field or a federation entry point.
- When schema directive collection changes, check directives on nested input and argument types. They must be included before schema construction so link and composition declarations stay complete.
- When a Relay type name or node ID rule changes, check stored IDs and every client that can refetch the type. The encoded type name is part of the public ID.
- When connection cursor or limit rules change, check forward and backward page behavior together. Clients depend on cursor prefixes and
PageInfovalues to continue a query. - When connection selection checks change, check queries that request only
pageInfoor only nested connection selections. The result must still expose the requested pagination fields without resolving unrequested edge data.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A federation directive requires a newer version than the schema selects | Schema creation fails with a version compatibility error | Select a supported Federation 2 version that includes the directive |
| A composed custom directive has the wrong name or import source | Composition cannot preserve or recognize the directive across subgraphs | Keep the directive name and import URL aligned with the composition contract |
| An entity representation has a missing or invalid key value | The gateway cannot resolve the entity, so fields from that subgraph fail | Keep key fields aligned across subgraphs and validate reference resolution |
An untrusted caller can query _service { sdl } | The caller can read the federated service SDL even when standard introspection is disabled | Protect federation fields with authentication, authorization, or network controls |
| An application query field collides with a federation field by GraphQL or Python name | The wrong field can be used or schema construction can produce an unexpected query shape | Keep application names distinct or verify the schema's collision handling |
A Node type has no unique NodeID field or has more than one | Node schema setup raises a node ID annotation error | Mark exactly one stable application ID with relay.NodeID |
| A global ID contains an unknown type or malformed encoded value | Node lookup fails before the object can be resolved | Treat global IDs as schema-bound values and preserve the type name and ID format |
| A connection receives an invalid cursor or a page size above its limit | The request fails instead of returning a page | Use cursors from the same connection and keep requested sizes within the configured limit |
Key files
| File | Why to read it |
|---|---|
| strawberry/…/schema.py | Shows how a federated schema exposes service metadata, discovers reachable entity types, resolves gateway representations, and prepares versioned composition directives before the schema is built. |
| strawberry/…/types.py | Defines the federation schema values used for field sets, imported link values, and link purposes. |
| strawberry/…/object_type.py | Shows how federation-aware type decorators add keys, extensions, and interface-object behavior to normal GraphQL types. |
| strawberry/…/field.py | Shows how federation field options become schema directives without changing normal resolver behavior. |
| strawberry/…/schema_directive.py | Shows how custom schema directives declare their locations and opt into composition. |
| strawberry/…/schema_directives.py | Defines the federation directive vocabulary and the Federation 2 versions that support it. |
| strawberry/…/types.py | Defines global IDs, nodes, edges, page information, and the connection model used in the GraphQL schema. |
| strawberry/…/fields.py | Connects node and connection fields to object lookup and pagination behavior. |
| strawberry/…/utils.py | Defines global ID encoding, cursor validation, query-selection checks, and the cursor slice rules used by list connections. |
| docs/…/federation.md | Gives the end-to-end subgraph, composition, and entity example. |
| docs/…/relay.md | Gives the end-to-end node, refetch, and connection example. |