Apollo Federation @key directive: missing key selection priority constraint
0 reputation · 18 Jan 2022, 13:14 UTC
0 reputation · 18 Jan 2022, 13:14 UTC
Apollo Federation allows entities to define multiple @key directives, each declaring a different set of fields that can uniquely identify an entity across subgraphs. The gateway's query planner chooses which key to use based on the fields requested in the incoming operation, but the selection algorithm lacks a documented priority order or configurable constraint.
Without a guaranteed selection order, the same query can produce different query plans across gateway versions or even between planning cycles. This non-determinism complicates performance tuning, as subgraph authors cannot enforce the use of a more efficient key—such as one backed by a database index—over a less optimal alternative. Workarounds like restricting entities to a single key or adding @provides/@requires directives increase schema complexity without addressing the root constraint gap.
Can the Federation specification define a deterministic key selection priority? Is there a mechanism for subgraph authors to declare a preferred key for a given query pattern? Will future gateway releases expose a configuration option to control this behavior?
35775 reputation · 18 Jan 2022, 15:20 UTC
No. The Federation 2 specification does not define a deterministic priority order among multiple @key directives on one entity. The Apollo Router planner picks a key based on the fields requested in the operation and which subgraphs can resolve the entity, but that choice is an internal implementation detail, not a published contract. Subgraph authors cannot declare a preferred key for a query pattern, and no documented router configuration forces a specific key.
Confirmed: Federation permits multiple @key directives per entity. The planner may choose any key that satisfies the requested fields, and behavior can shift between router releases even with an unchanged schema.
Likely explanation: The planner targets plan validity and field coverage, not index-backed lookup speed. Because the algorithm is internal, it is not guaranteed to prefer a database-indexed key over a less efficient alternative.
apollo router --version and the composed schema's federation target._entities representation to see which key fields go to which subgraph.@key directives from the entity, redeploy, and compare the plan and latency.@requires and @provides to shape resolution, or move the indexed lookup into the entity resolver so the planner does not control the lookup path.Apollo has not announced a public key-priority configuration toggle. Future router releases may add planning knobs, but none are documented in current stable releases. Review router release notes before upgrading.
Whether you use Apollo Router or legacy Apollo Gateway changes the applicable behavior. Confirm your gateway implementation and Federation version before applying these steps.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.