Federation support
Strawberry federation support lets a GraphQL service publish a subgraph that a federation gateway can join with other subgraphs. It adds entity identity, reference lookup, federation metadata, version checks, and gateway-facing schema fields while keeping normal Strawberry type definitions available. This page focuses on federation; see Federation and Relay support for the parent overview, Relay nodes and connections for the related Relay model, Schema definition for the base schema, and Schema conversion and printing for the schema-to-SDL path.
Federation schema
The federation schema is the entry point for building a composable subgraph. It extends the normal schema with the federation contract that describes the service and lets a gateway request entities by their representations.
Gateway schema surface
The strawberry.federation.Schema in strawberry/…/schema.py adds _service to every federation schema. _service.sdl exposes the subgraph SDL that a gateway uses to learn the service's types and federation metadata. When the schema contains at least one keyed object type, it also adds _entities and the _Entity union.
The schema registers the _Any scalar used for entity representations. It discovers federation directives while the base schema collector walks the same definitions used to build the GraphQL schema. This includes directives on fields, arguments, and nested input types. The discovery step lets the schema add @link and composed custom-directive metadata before the final GraphQL schema is built. The federation-specific scalar mapping is combined with any caller-provided scalar overrides, so federation support does not replace the application's scalar choices.
The following diagram shows how a subgraph definition becomes the gateway-facing schema surface.
flowchart LR
A[Python types and federation metadata] --> B[Federation schema]
B --> C[Subgraph SDL through _service]
B --> D[Entity representation through _entities]
D --> E[Reference lookup]
E --> F[Entity fields returned to gateway]
References: strawberry/…/schema.py• L47-120 strawberry/…/schema.py• L123-213 strawberry/…/schema.py• L266-400 strawberry/…/types.py• L7-29 tests/…/test_schema.py• L66-128
Entity reference flow
An entity is a GraphQL object that has a federation key. The gateway sends a representation with __typename and the key fields to _entities. Strawberry maps the type name to the entity type and then resolves the representation through the type's resolve_reference method when one is supplied.
When no custom reference method is present, Strawberry builds the entity from the representation when the data matches the type shape. A reference method can be synchronous or asynchronous and can accept Info for request context. The result list keeps the same order as the input representations, so each gateway request remains aligned with its requested entities.
Reference lookup can return None when the entity does not exist. Invalid key data produces an entity resolution error, while an error raised by a reference method keeps its message and GraphQL error metadata.
The entity flow has these stages:
- The gateway sends entity representations to
_entities. - The schema selects the entity type from
__typename. - Strawberry calls the custom reference resolver or constructs the type from the representation.
- GraphQL resolves the requested fields on each returned entity.
References: strawberry/…/schema.py• L215-264 tests/…/test_entities.py• L7-43 tests/…/test_entities.py• L95-175 tests/…/test_entities.py• L177-217 tests/…/test_entities.py• L419-497
Federation decorators and fields
Federation decorators keep the Python model close to the subgraph contract. They add federation metadata to objects, interfaces, inputs, enums, scalars, unions, fields, and arguments without changing the main purpose of those GraphQL types.
Type decorators
The federation type decorators are exposed from strawberry/…/__init__.py. type marks an object as a federation type and accepts keys, extension status, shareability, access rules, and other type metadata. interface adds federation metadata to an interface. interface_object describes an object that represents an interface in a subgraph. input supports input objects with federation visibility and tag metadata.
A string key is converted to a federation field selection. A type may have more than one key, which lets the gateway identify it through different representations. extend=True marks a type as an extension of a type owned by another subgraph. The type decorator also accepts explicit directive instances when the shorthand options do not cover the required metadata.
The decorator metadata tells static type checkers that these decorators create dataclass-like types without enabling generated ordering methods by default. This keeps type-checking assumptions aligned with the federation type contract.
References: strawberry/…/__init__.py• L1-24 strawberry/…/object_type.py• L23-56 strawberry/…/object_type.py• L65-104 strawberry/…/object_type.py• L107-161 strawberry/…/object_type.py• L164-260 strawberry/…/params.py• L29-41
Field and argument metadata
strawberry.federation.field has the normal Strawberry field behavior plus federation options. These options express how a field participates in composition: a field can be external, require fields from another subgraph, provide fields to another type, be shareable, be hidden from the composed API, carry access rules, or replace a field from another subgraph.
Field selections are supplied as lists in the Python API and become federation field-set values in the schema. The same field metadata can be supplied as explicit directive objects when a caller needs full control. Federation arguments add the visibility and tag options that apply to argument definitions. These helpers attach metadata; they do not provide the data source for a field.
References: strawberry/…/field.py• L93-129 strawberry/…/params.py• L16-27 strawberry/…/params.py• L43-108 strawberry/…/argument.py• L7-32 federation-compatibility/schema.py• L122-149 federation-compatibility/schema.py• L247-256
Federation type values
Federation versions of enum, enum_value, scalar, and union add federation visibility and access metadata to the corresponding GraphQL type. They support the @inaccessible and @tag patterns where those locations are valid, as well as security metadata on types that support it.
FieldSet represents a selection of fields used by @key, @requires, and @provides. LinkImport represents an imported value for @link, while LinkPurpose identifies whether a link serves security or execution concerns. These values give the schema a typed way to describe federation metadata instead of treating all directive input as ordinary application data.
References: strawberry/…/enum.py• L18-41 strawberry/…/enum.py• L44-122 strawberry/…/scalar.py• L24-76 strawberry/…/union.py• L7-54 strawberry/…/types.py• L7-29
Federation directives
Federation directives describe ownership, field use, access rules, and composition behavior. Strawberry models these directives as schema metadata so the generated SDL can carry the contract that a federation gateway and composition tool expect.
Composition directives
The core composition directives cover the common relationships between subgraphs:
| Directive | Purpose |
|---|---|
@key | Defines the field set that identifies an entity. |
@external | Marks a field supplied by another subgraph. |
@requires | States which external fields a field needs to resolve. |
@provides | States which fields a returned entity can supply to another subgraph. |
@shareable | Allows a field or object to be resolved by more than one subgraph. |
@override | Moves field ownership from one subgraph to another. |
@inaccessible | Keeps schema metadata out of the composed API. |
@tag | Adds a named label to schema elements. |
The federation field and type decorators provide shorthand for many of these directives. The directive classes remain available for explicit use and for metadata that needs directive-specific values such as a key field set or an override source.
References: strawberry/…/schema_directives.py• L28-90 strawberry/…/schema_directives.py• L132-172 strawberry/…/params.py• L43-108
Access and policy directives
Federation support also models directives that describe access requirements and query cost. @authenticated marks a schema element as requiring authentication. @requiresScopes expresses required scope sets. @policy expresses policy sets. @cost and @listSize describe cost and list-size information for composition or query planning.
These directives are available on the locations defined by the federation contract. The shorthand options on federation types, fields, enums, and scalars turn common Python sequences into the nested directive values expected in SDL.
References: strawberry/…/schema_directives.py• L196-249 strawberry/…/schema_directives.py• L278-312 strawberry/…/params.py• L47-55 strawberry/…/params.py• L111-162
Context and custom directives
Federation versions that support request context can use @context on an object, interface, or union and @fromContext on an argument. These directives let a composed field declare that an argument comes from federation context rather than from client input.
Applications can define a custom schema directive with strawberry.federation.schema_directive. A directive marked for composition is included in @composeDirective, and its import is added to the matching @link entry. If no import URL is given, Strawberry supplies a stable Strawberry URL for the generated link. This allows a custom directive to travel through composition as part of the subgraph contract.
The federation schema uses the directive list collected while the base schema traverses the definitions. As a result, directives used on nested inputs or arguments can contribute their imports and composition declarations before GraphQL builds the final schema.
References: strawberry/…/schema_directives.py• L252-275 strawberry/…/schema_directive.py• L13-59 strawberry/…/schema.py• L266-400 federation-compatibility/schema.py• L110-119
Federation version handling
The schema accepts a supported Federation 2 version from 2.0 through 2.11. It uses the selected version for the Apollo federation link in the SDL and checks that every federation directive used by the schema is valid for that version.
Each federation directive declares a minimum version. The schema compares those requirements with the configured version before it finishes setup. If several directives are present, the schema must meet the highest requirement among them. The default is the latest supported version, currently 2.11.
The version flow is:
- The schema parses the configured version.
- The schema gathers federation directives used by types and fields.
- The schema rejects a directive whose minimum version is higher than the configured version.
- The schema adds imports for federation directives to the selected Apollo specification URL and adds imports for composed custom directives to their configured or generated URLs.
- The schema adds
@composeDirectiveentries for composed custom directives before the final schema is built.
| Minimum version | Directives introduced in this support layer |
|---|---|
2.0 | Core entity, ownership, visibility, override, and tag directives. |
2.1 | @composeDirective. |
2.3 | @interfaceObject. |
2.5 | @authenticated and @requiresScopes. |
2.6 | @policy. |
2.8 | @context and @fromContext. |
2.9 | @cost and @listSize. |
References: strawberry/…/schema.py• L64-84 strawberry/…/schema.py• L266-400 strawberry/…/versions.py• L1-29 strawberry/…/schema_directives.py• L28-32 strawberry/…/schema_directives.py• L175-312 tests/…/test_version_validation.py• L7-18 tests/…/test_version_validation.py• L107-170
Federation compatibility fixture
The federation-compatibility/schema.py fixture is a small Apollo-style products subgraph used to exercise composition-facing behavior. It combines entity keys, type extensions, external and required fields, provided fields, overrides, inaccessible fields, tags, custom composed directives, interface objects, and reference resolvers in one service.
The fixture models several forms of entity identity. A product can resolve by an ID or by alternate compound keys. Product research uses a nested key selection, while the extended user type resolves from an email key. This makes the fixture useful for checking that a gateway can send different representation shapes and still receive the right entity.
The fixture is packaged as a service on port 4001 by federation-compatibility/docker-compose.yml and federation-compatibility/Dockerfile. Its schema runs with Federation 2.7, which makes the fixture a compatibility target for features available at or below that version. It also enables Apollo federation tracing so the service can be checked in a gateway-style environment.
References: federation-compatibility/schema.py• L72-107 federation-compatibility/schema.py• L110-149 federation-compatibility/schema.py• L152-227 federation-compatibility/schema.py• L230-301 federation-compatibility/schema.py• L303-319 federation-compatibility/docker-compose.yml• L1-7 federation-compatibility/Dockerfile• L17-28
Change checklist
When these files or sections are changed, remember to consider these:
- A new federation directive needs a minimum version, valid schema locations, an Apollo import name, and a matching version-validation path.
- A change to entity keys must stay aligned across the type metadata, the representation accepted by
resolve_reference, and the_Entityunion discovered by the schema. - A change to a shorthand field or type option must preserve the equivalent explicit directive output in the subgraph SDL.
- A change to composed custom directives must keep the
@composeDirectivedeclaration and its@linkimport in sync. - A change to directive traversal must include directives on arguments and nested input definitions so their links are not lost.
- A change to the compatibility fixture must preserve at least one example for each representation shape that the fixture is meant to check.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A schema uses a directive above its configured Federation version. | Schema creation raises a version compatibility error. | Set federation_version to a version that meets every directive requirement. |
| An entity representation omits or changes a key field. | _entities cannot build the entity and returns an entity resolution error. | Keep gateway representations aligned with each @key field set and reference resolver. |
A schema uses federation directives with strawberry.Schema instead of strawberry.federation.Schema. | Strawberry warns that the federation schema entry point is required. | Construct the schema with the federation-specific Schema. |
No reachable object type has a @key directive. | The schema has _service but no _Entity union or _entities field. | Add a key to each type that must be fetched by the gateway and make it reachable from the schema types. |
| A custom directive is marked for composition without a usable import link. | Composition cannot import the directive under the expected name. | Keep the compose setting and import URL or generated link together. |
Key files
| File | Why to read it |
|---|---|
| strawberry/…/schema.py | Defines the federation schema contract, gateway fields, recursive entity discovery, reference lookup, SDL links, directive collection, and version checks. |
| strawberry/…/object_type.py | Shows how federation type decorators add keys, extension status, and type-level metadata. |
| strawberry/…/field.py | Shows the federation field entry point and its relationship to normal Strawberry fields. |
| strawberry/…/params.py | Maps decorator options to federation directive metadata. |
| strawberry/…/schema_directives.py | Defines the supported federation directives and their minimum versions. |
| strawberry/…/schema_directive.py | Defines custom federation schema directives and composition options. |
| strawberry/…/types.py | Defines the typed schema values used by federation links and field selections. |
| strawberry/…/versions.py | Defines supported version names and Apollo link formatting. |
| federation-compatibility/schema.py | Provides an end-to-end subgraph fixture with varied entity and directive use. |
| tests/…/test_schema.py | States the expected gateway fields, entity union behavior, and service SDL. |
| tests/…/test_entities.py | States the reference resolution, async behavior, representation, and error contracts. |
| tests/…/test_version_validation.py | States the minimum-version and default-version guarantees. |