Definition
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
fields | FieldSet! | — | A selection set (as a string) of one or more fields that together uniquely identify an instance of the entity. |
resolvable | Boolean | true | When false, indicates that this subgraph does not implement a reference resolver for the entity. The router will not route queries to this subgraph solely to resolve entity fields. |
Overview
The@key directive is the foundation of GraphQL Federation entities. Applying @key to an object type declares it an entity: a type whose fields can be distributed across multiple subgraphs and resolved independently by each. The key fields uniquely identify an instance of the entity so the router can correlate data from different subgraphs.
Any subgraph that resolves or references an entity must declare at least one @key. Different subgraphs may use different key fields as long as the router can traverse between them — either directly (both subgraphs share a common key) or through an intermediary subgraph that declares both keys. See When Multiple Keys Are Needed for an example. The router uses these keys to construct representations when fetching entity data from subgraphs that contribute additional fields.
Single Key
The most common case is identifying an entity by a single scalar field. Here both the scheduling and analytics subgraphs contribute fields to theEvent entity:
Event as an entity with the id key. The router can now combine fields from both subgraphs into a single Event response.
Multiple Keys
A type can have more than one@key directive. Each additional key gives the query planner another set of fields it can use to jump between subgraphs when resolving the entity.
When Multiple Keys Are Needed
Multiple keys become necessary when different subgraphs can only resolve an entity by different identifiers. Consider aUser entity with a public id and an internalId. Subgraph A can only resolve users by id, subgraph B can only resolve users by internalId, and subgraph C defines both keys:
Compound Keys
A compound key spans multiple fields, including nested object fields. All fields in the compound key’s selection set must be present on the type:id and the nested event.id to uniquely identify the Session.
Non-Resolvable Keys (resolvable: false)
A subgraph sometimes needs to return a reference to an entity it does not resolve. For example, a ticketing subgraph stores which event a ticket belongs to, but the event’s details (name, date, venue) live in a separate scheduling subgraph. Setting resolvable: false tells the router to never send entity resolution requests to this subgraph for the Event type:
Event references (e.g. Ticket.event), but the router will resolve Event fields through subgraphs that declare a resolvable key for it. If you need to use the id field in @requires or @provides within this subgraph, mark it as @external.
In Federation v1, each entity had a single “origin” subgraph (the type definition). All other subgraphs used type extensions and had to mark key fields as
@external. Federation v2 removed the origin concept entirely. Entities can now be defined as regular types across multiple subgraphs, and key fields are implicitly shareable. See Understanding Federation Versions for more details.Keys on Interfaces
Applying@key to an interface creates an entity interface, allowing a subgraph to contribute fields to every object type that implements the interface across subgraphs.
Rules
- The subgraph that defines the entity interface must also define every entity type that implements it. Other subgraphs may also define these entities.
- Every implementing entity must include all
@keydirectives from the interface. Additional keys are permitted but generally discouraged (see Multiple Keys). - Key field values must be unique across all implementing types. No instance of
Concertcan have the same key value as any instance ofWorkshop. - The interface must have at least one
@keydirective.
See Also
@external declares that a field is not independently resolvable by the current subgraph, making it available for use in @requires or @provides. @requires declares that an entity field depends on external fields that must be fetched from another subgraph first. @provides declares that a subgraph can resolve certain external entity fields at a specific query path.