0Pricing
GraphQL APIs with Spring Boot · Lesson

Entity References and the @key Directive

Master the heart of Apollo Federation: how subgraphs share entities using the @key directive and resolve references from other subgraphs in Spring Boot.

Entity References and the @key Directive is a free GraphQL APIs with Spring Boot lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the GraphQL APIs with Spring Boot learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Entities Span Subgraphs

In federation, a single type like Product can be owned and extended by several subgraphs. The mechanism that lets them agree on the same object is the entity.

What Makes an Entity

An entity is a type with a @key directive. The key names the field(s) that uniquely identify an instance across subgraphs, like a primary key in a database.

type Product @key(fields: "id") {
  id: ID!
  name: String!
}

The Owning Subgraph

The subgraph that defines a type's core fields is its owner. It is responsible for fetching a full entity given just its key, so the gateway can stitch data together.

Extending an Entity Elsewhere

Another subgraph can add fields to the same entity. It declares the type with the matching @key and marks borrowed fields as external context.

type Product @key(fields: "id") {
  id: ID!
  reviews: [Review!]!
}

The Reference Resolver

When the gateway needs an entity from a subgraph, it calls a special reference resolver, passing only the key fields. The subgraph returns the matching object.

Resolving References in Spring

Spring for GraphQL Federation provides @EntityMapping to implement the reference resolver. The key fields arrive as arguments.

@EntityMapping
public Product product(@Argument String id) {
    return productService.findById(id);
}

Adding the Federation Library

Enable federation by adding the Apollo federation JVM support, which generates the _entities and _service fields the gateway requires.

// build.gradle
implementation 'com.apollographql.federation:federation-graphql-java-support'

Compound Keys

An entity can be identified by multiple fields. List them space-separated in the @key, useful when no single field is unique.

type OrderItem @key(fields: "orderId sku") {
  orderId: ID!
  sku: String!
}

Multiple Keys

A type can declare several @key directives, letting different subgraphs reference it by whichever identifier they hold.

type User @key(fields: "id") @key(fields: "email") {
  id: ID!
  email: String!
}

How the Gateway Stitches Data

The gateway queries the owning subgraph, then sends the entity's keys to other subgraphs via _entities to fetch their extra fields, merging everything into one response.

Best Practices

Design entities carefully:

  • Pick stable, unique key fields
  • One subgraph owns the core fields
  • Keep reference resolvers fast (consider DataLoaders)
  • Use compound keys only when necessary

Quick Check

Test your federation entity knowledge.

Recap

You learned federated entities:

  • An entity is a type with a @key
  • One subgraph owns it; others extend it
  • Reference resolvers (@EntityMapping) fetch by key
  • Compound and multiple keys handle complex identity

Entities and @key are how federation stitches a unified graph from many subgraphs.

Frequently asked questions

Is the “Entity References and the @key Directive” lesson free?

Yes — the full text of “Entity References and the @key Directive” is free to read here on the web, and the GraphQL APIs with Spring Boot course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the GraphQL APIs with Spring Boot course, upgrade to CoddyKit PRO.

What will I learn in “Entity References and the @key Directive”?

Master the heart of Apollo Federation: how subgraphs share entities using the @key directive and resolve references from other subgraphs in Spring Boot. You practise GraphQL APIs with Spring Boot with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start GraphQL APIs with Spring Boot?

No prior experience is required. GraphQL APIs with Spring Boot on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Entity References and the @key Directive” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this GraphQL APIs with Spring Boot lesson?

Yes. Every GraphQL APIs with Spring Boot lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Apollo Federation
  2. Building Federated Subgraphs
  3. Gateway Setup and Management
  4. Entity References and the @key Directive
← Back to GraphQL APIs with Spring Boot