Entitätsreferenzen und die @key-Direktive
Meistern Sie das Herzstück von Apollo Federation: Erfahren Sie, wie Subgraphs mithilfe der @key-Direktive Entitäten gemeinsam nutzen und Referenzen aus anderen Subgraphs in Spring Boot auflösen.
Entitätsreferenzen und die @key-Direktive ist eine kostenlose GraphQL APIs with Spring Boot-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des GraphQL APIs with Spring Boot-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der GraphQL APIs with Spring Boot-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Entitätsreferenzen und die @key-Direktive“ kostenlos?
Ja — der vollständige Text von „Entitätsreferenzen und die @key-Direktive“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des GraphQL APIs with Spring Boot-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der GraphQL APIs with Spring Boot-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Entitätsreferenzen und die @key-Direktive“?
Meistern Sie das Herzstück von Apollo Federation: Erfahren Sie, wie Subgraphs mithilfe der @key-Direktive Entitäten gemeinsam nutzen und Referenzen aus anderen Subgraphs in Spring Boot auflösen. Du übst GraphQL APIs with Spring Boot mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um GraphQL APIs with Spring Boot zu starten?
Keine Vorkenntnisse erforderlich. GraphQL APIs with Spring Boot auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Entitätsreferenzen und die @key-Direktive“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser GraphQL APIs with Spring Boot-Lektion Code schreiben und ausführen?
Ja. Jede GraphQL APIs with Spring Boot-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einführung in Apollo Federation
- Federierte Subgraphs erstellen
- Gateway einrichten und verwalten
- Entitätsreferenzen und die @key-Direktive