Références d’entités et directive @key
Maîtrisez le cœur d’Apollo Federation : découvrez comment les sous-graphes partagent des entités avec la directive @key et résolvent les références provenant d’autres sous-graphes dans Spring Boot.
Références d’entités et directive @key est une leçon GraphQL APIs with Spring Boot gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage GraphQL APIs with Spring Boot, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours GraphQL APIs with Spring Boot comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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.
Questions Fréquemment Posées
La leçon « Références d’entités et directive @key » est-elle gratuite ?
Oui — le texte complet de « Références d’entités et directive @key » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours GraphQL APIs with Spring Boot, passe à CoddyKit PRO. Le cours GraphQL APIs with Spring Boot comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Références d’entités et directive @key » ?
Maîtrisez le cœur d’Apollo Federation : découvrez comment les sous-graphes partagent des entités avec la directive @key et résolvent les références provenant d’autres sous-graphes dans Spring Boot. Tu pratiques GraphQL APIs with Spring Boot avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer GraphQL APIs with Spring Boot ?
Aucune expérience préalable n'est requise. GraphQL APIs with Spring Boot sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.
Combien de temps prend la leçon « Références d’entités et directive @key » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon GraphQL APIs with Spring Boot ?
Oui. Chaque leçon GraphQL APIs with Spring Boot inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Introduction à la fédération Apollo
- Construire des sous-graphes fédérés
- Configurer et gérer la passerelle
- Références d’entités et directive @key