This recipe covers the federation key surface: how @key declares an entity, how graphitron wires entity resolution from @key and @node, what compound and multi-alternative keys do, and the resolvable: false flag for keys owned by another subgraph. It walks the path from "what @key does at all" through to declaring a reference-only entity that lives on another subgraph.

The framework-level federation surface (the @link opt-in, the runtime entity dispatcher, custom fetchers, the <schemaInput tag> flag) lives in How-to: Apollo Federation transport; this recipe assumes that wiring is in place.

The rules themselves, what a key may select and the codes a key is refused with, are stated in the Federation reference.

What @key does

A federation @key on a type declares "this type is an entity, and a representation carrying these fields uniquely identifies one row". The federation gateway routes _entities lookups to your subgraph; graphitron wires the resolution per-type so each @key becomes a SELECT against the type’s @table.

extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.10", import: ["@key"])

type Customer @table(name: "customer") @key(fields: "customerId") {
    customerId: Int! @field(name: "CUSTOMER_ID")
    firstName:  String! @field(name: "FIRST_NAME")
    lastName:   String! @field(name: "LAST_NAME")
}

The classifier reads @key(fields: "customerId"), walks customerId’s field-level `@field to find the column (customer.customer_id), and registers an entity-resolution alternative on the type. At runtime, an _entities representation { "__typename": "Customer", "customerId": 42 } becomes the SELECT:

SELECT customer.first_name, customer.last_name, customer.customer_id
  FROM customer
 WHERE customer.customer_id = 42

The dispatcher batches across all _entities representations for the same alternative on the same type into a single VALUES-derived-table query, so a request asking for fifty customers fires one statement, not fifty.

A resolvable @key on a type without @table is refused with KEY_TYPE_NOT_TABLE_BOUND at the @key. The reference dispatcher SELECTs from a table; without one, there’s no source.

Compound keys

fields: accepts a space-separated list. Every column on the list contributes to the key:

type FilmActor @table(name: "film_actor") @key(fields: "filmId actorId") {
    filmId:  Int! @field(name: "film_id")
    actorId: Int! @field(name: "actor_id")
}

The dispatcher emits a multi-column VALUES join: each representation must carry both filmId and actorId, and the SELECT WHERE clause is (film_id, actor_id) IN ((?, ?), (?, ?), …​). The arity of the VALUES-derived table matches the key’s column count.

The fields named in fields: must each be a field the type declares, on any type, or the key is refused with KEY_FIELD_MISSING. On a table-bound type each must also read a column of the type’s own table. A nested selection, a field read through a @reference path, an @externalField or computed scalar, or an @nodeId field carrying another node’s encoded id can’t participate in a key: each is refused with KEY_FIELD_NOT_A_COLUMN at the @key, naming the field. A fields: that selects nothing is KEY_SELECTS_NOTHING.

Multiple @key alternatives

A type can declare multiple @key directives. Each is an alternative:

type Film @table(name: "film")
        @key(fields: "filmId")
        @key(fields: "filmId title") {
    filmId: Int!    @field(name: "FILM_ID")
    title:  String! @field(name: "TITLE")
}

The runtime dispatcher walks the alternatives in most-specific-first order: when a representation supplies both filmId and title, the compound key wins; when only filmId is supplied, the single-key alternative matches. Ties (two alternatives of the same size whose required fields are both subsets of the representation’s keys) are broken by SDL declaration order.

This matters for cross-subgraph contracts: a federation gateway may route a representation to your subgraph that supplies more fields than the narrowest alternative needs. The most-specific-wins rule means the dispatcher uses the richest information available, even if a less-specific alternative would also match. The query plan is the same SELECT shape; only the WHERE clause changes.

@key on @node types

Graphitron’s @node directive synthesises a @key(fields: "id", resolvable: true) per type in a federation-linked graph. The opaque id: is decoded through the same NodeIdEncoder the Relay-side Query.node uses, and the resulting column values feed the same dispatcher as a @key(fields: "…​") would. Point being: @node is enough on its own; you don’t need to add @key(fields: "id") for the synthesised path.

An authored @key(fields: "id") always takes the synthesised key’s place: federation looks entities up by their keys, and one key spelling id is enough. How it resolves follows from what id reads. The node’s own id, with no @field, carries the encoded node id and is decoded; an id carrying @field(name:) reads a column like any other field, so the key matches that column, and the type is not resolved by a decoded node id at all. A compound key that includes the node’s own id is refused with KEY_FIELD_NOT_A_COLUMN, since the encoded id is not a column value.

A @node type with an additional explicit @key gets both alternatives:

type Film implements Node @table(name: "film") @node
        @key(fields: "filmId")
        @key(fields: "filmId title") {
    id:     ID!     @nodeId
    filmId: Int!    @field(name: "FILM_ID")
    title:  String! @field(name: "TITLE")
}

The synthesised NODE_ID alternative (decode id: through NodeIdEncoder) is recorded, plus the two consumer-declared DIRECT alternatives (filmId, then filmId title). At dispatch time, a representation with id: lands the NODE_ID arm; a representation with filmId and title lands the compound alternative; a representation with just filmId lands the single-column alternative. A representation that supplies all three fields lands the compound alternative (most-specific tie-break).

This layered shape is the right pattern when:

  • The Relay path (Query.node(id:)) and the federation path (_entities) both need to work, and the Relay path must use opaque IDs (no leaking column values to clients).

  • External subgraphs may reference the entity by either form (gateway-supplied opaque IDs from a previous Relay traversal, or column values from a fresh subgraph join).

The synthesised alternative is always added; explicit @key(fields: "id") on a @node type is folded into the same NODE_ID alternative, with the resolvable: value coming from the user’s directive (so @key(fields: "id", resolvable: false) on a @node carries resolvable: false through to the synthesised alternative).

resolvable: false: keys for reference

A federation entity can declare a key that this subgraph does not resolve. The federation spec calls this a non-resolvable key, and graphitron honours it: the dispatcher records the alternative but skips it at match time, so it never fires a SELECT for it. When the entity has another, resolvable key, a representation matching only the non-resolvable one yields null. When every key of the entity is non-resolvable, as below, this subgraph resolves no representation of the type at all, and one that arrives anyway gets null and an error (see What _entities answers).

type Language @table(name: "language") @key(fields: "languageId", resolvable: false) {
    languageId: Int!    @field(name: "LANGUAGE_ID")
    name:       String! @field(name: "NAME")
}

The schema lists Language as a reference-only entity: the gateway sees the key, knows the type exists, but routes resolution to whichever subgraph declared a resolvable key for the same type. This is the federation-level "I know about this thing, but it’s not mine to fetch".

Two distinct uses:

  • This subgraph has the table but the entity is owned elsewhere. The local table is incidental (cached lookup data, denormalised columns); the canonical resolution lives on another subgraph.

  • This subgraph references the entity by FK but does not store it. The non-resolvable key lets a relationship typecheck without forcing the local subgraph to expose a SELECT.

resolvable: false is also legal on @node types via the synthesised path: @node @key(fields: "id", resolvable: false) declares the type as a reference-only entity even though @node would normally make it resolvable.

A type this subgraph only decodes ids of goes further: @node(resolvable: false) makes it a node like any other, implementing Node, so @nodeId(typeName:) can name it, while Query.node answers its id with null and no @key(fields: "id") is synthesised for it.

What _entities answers

Each representation gets one slot in the answer, in the order sent.

  • A representation this subgraph resolves, naming a row that exists, gets the row.

  • A representation whose typename this subgraph does not resolve gets null and an error at its slot: Cannot resolve entity with typename 'Foo'. That covers a type this schema does not declare, and an entity whose every @key is resolvable: false. A representation with no __typename gets null and an error the same way. The router only sends a subgraph types it declared resolvable, so either error means the supergraph and this subgraph disagree, usually a subgraph deployed without recomposing.

  • A representation of a type this subgraph resolves, but whose key names no row, does not decode, or names a tenant outside the request’s set, gets null with no error, exactly as a missing row does.

The error is a client error, so it reaches the response as written; reword it through FetcherErrorResponse.clientErrorMessage like any other.

For a request carrying an unknown type between two it resolves:

{ "data": { "_entities": [ { "__typename": "Customer" }, null, { "__typename": "Film" } ] },
  "errors": [ { "message": "Cannot resolve entity with __typename 'DoesNotExist'.",
                "path": [ "_entities", 1 ] } ] }

Keys without the tenant column

In a database-per-tenant build (<tenantColumn>), _entities reads each representation’s tenant out of its key, so a resolvable @key on a tenant-scoped type must include the tenant column. A request names no tenant of its own, so a key that cannot carry the column is a build error: add a field mapped to the tenant column to the key, or mark that key resolvable: false. The @defaultTenant marker that once resolved such keys is no longer supported.

Three wiring concerns surface once @key is in play. How-to: Apollo Federation transport covers them in full; the operational summary:

  • Opt in via @link. Add an extend schema @link(url: "…​", import: […​]) to one of your .graphqls files. The plugin detects the link and wraps the assembled schema with the federation builder. Without @link, the schema is non-federation; @key fails graphql-java parsing because the directive is not declared.

  • Manual directive declarations. Alternative to @link: declare each directive you use (directive @key(fields: String!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE). The build still wraps with federation when the catalog finds any federation directive applied. Mixing the two on the same directive (importing via @link and redeclaring it locally) fails validation.

  • <schemaInput tag> is federation-specific. Setting <schemaInput><tag>…​</tag></schemaInput> in the plugin config emits @tag(name: "…​") directives on schema elements. These are only meaningful to a federation gateway; the plugin treats this as an implicit Federation 2 opt-in, and synthesises a minimal @link import for @tag if you haven’t already declared one.

  • Custom entity fetcher. The default dispatcher handles @node and @key types graphitron classifies; for hand-rolled entity types or entities pulled from a non-graphitron source, supply your own fetcher via the two-arg Graphitron.buildSchema(b, fed → fed.fetchEntities(myFetcher)). Custom fetchers must return entities the default resolveEntityType can recognise (jOOQ Record`s with a `__typename column).

Pitfalls

  • @key requires @table. The dispatcher SELECTs from a table; a resolvable @key on a non-table-bound type is refused (KEY_TYPE_NOT_TABLE_BOUND). The exception is @node types (which always carry an entity entry for the Query.node path, regardless of @key).

  • @key is refused on table-bound interface types in graphitron (KEY_ON_INTERFACE). The federation spec allows it, but graphitron’s dispatcher does not yet support @key on interfaces; declare @key on the implementing types instead.

  • Most-specific-wins applies across keys, not within. Within a single @key, all required fields are mandatory; the alternative matches only when every required field is supplied. Across alternatives, the dispatcher picks the largest matching one.

  • resolvable: false is honoured but advisory. The dispatcher skips the alternative; if another, resolvable alternative exists but none matches, the result is null. If the entity has no resolvable key at all, a representation of it gets null and an error, as for a type this subgraph does not declare. A non-resolvable key with no resolvable counterpart on any subgraph is a federation-graph composition error, not a graphitron build error; it surfaces at the gateway, not at build time.

  • Keys must list columns of the type’s own table only. A field that is @externalField, read through a @reference path, or otherwise not one of the table’s columns can’t participate in a @key; KEY_FIELD_NOT_A_COLUMN names the offending field. The fix: use a column-backed field, or restructure the key to avoid the non-column field.

See also

  • @node is the Relay/Query.node directive whose synthesised key feeds the federation _entities path.

  • @nodeId declares the encoded id: field that NODE_ID-shape alternatives use.

  • @field is the per-field column-mapping axis for entity key fields.

  • How-to: Apollo Federation transport covers the framework-level wiring (@link, <schemaInput tag>, custom fetchers) this recipe assumes is in place.

  • How-to: Global object IDs covers the Relay-side Query.node dispatch and keyColumns migrations that share infrastructure with @key.