The rules a Graphitron subgraph applies to Apollo Federation’s @key. @key is federation’s directive, imported through the schema’s @link, so it has no page among Graphitron’s directives; this page states what Graphitron does with it. How-to: Federation keys walks through the shapes with examples.

How an entity is resolved

An entity is resolved by selecting its row from its table: _entities matches the values a representation carries against that table’s columns, one batched statement per key alternative. Each @key on a type is one alternative. A representation is matched by the most specific resolvable alternative whose fields it carries, ties broken by declaration order. A key with resolvable: false is declared for reference only and is never matched. A key value that is no value of its column matches no row, and its representation answers null without reaching the database: "abc" or a number outside the column’s range on a numeric column, or a value too large to read.

A value too large to read is refused on its size, before anything parses it: on a numeric column, a number with more than 1000 fraction digits, or text longer than 2016 characters. The same bound holds for a tenant value, where a refused value is a client error (How-to: Tenant scoping).

A key over the node’s own id is decoded rather than matched: the representation’s id is a global node ID, decoded into the node’s key columns as Query.node decodes it. The node’s own id is an id field on a node type with no @field; an id carrying @field(name:) reads a column like any other field.

Synthesised keys

In a federation-linked graph, every resolvable node type is given @key(fields: "id") after the keys its author wrote, unless an authored key already selects id alone. Federation knows keys, not Node.id, so an authored @key(fields: "id") is the type’s id key whatever its id reads, and how it resolves follows from that: decoded where id is the node’s own, matched against the column where id carries @field(name:). A node this subgraph does not resolve, @node(resolvable: false), is given none.

A node whose id carries @field(name:) encodes no node id, so neither Query.node nor a representation’s id is decoded for it; it is resolved only through the keys its author writes.

A type marked @issue is given the voucher field internalGraphitronVoucher: String, marked @inaccessible, and @key(fields: "internalGraphitronVoucher", resolvable: false) after the keys its author wrote: the type is keyed by its voucher and not resolved here, so a router reaches the other subgraph’s entity from this one only through the voucher.

A type marked @accept is given the same voucher field, marked @inaccessible, and @key(fields: "internalGraphitronVoucher"), resolvable, after the keys its author wrote and the node key: a router hands this subgraph the voucher it fetched from the issuing one, and the voucher decodes to the authored key @accept(key:) names. The authored keys are untouched.

What a key may select

Each refusal below is reported at the @key that breaks it.

Code Rule

KEY_TYPE_NOT_TABLE_BOUND

A resolvable @key needs a type bound to a table with @table, since the entity is selected from it. A type whose every key is resolvable: false is another subgraph’s to resolve and needs no table.

KEY_ON_INTERFACE

A table-bound interface carries no @key: an entity is resolved by one table, so declare the key on each implementing type.

KEY_SELECTS_NOTHING

A key’s fields: must select at least one field.

KEY_FIELD_MISSING

Every field a key selects is a field its type declares, whatever the type is bound to, a key with resolvable: false included. A name the type does not declare is refused, naming it.

KEY_FIELD_NOT_A_COLUMN

Every field a key selects reads a column of the entity’s own table. A nested selection, a field read through a @reference path, a computed field, or a @nodeId carrying another node’s ID is refused, naming the field. The one exception is a key whose only field is the node’s own id. A compound key including the node’s own id is refused, since an encoded ID is not a column value.