Riferimenti alle entità e direttiva @key
Padroneggi il cuore di Apollo Federation: scopra come i sottografi condividono le entità usando la direttiva @key e risolvono i riferimenti provenienti da altri sottografi in Spring Boot.
Riferimenti alle entità e direttiva @key è una lezione GraphQL APIs with Spring Boot gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento GraphQL APIs with Spring Boot, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso GraphQL APIs with Spring Boot include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Riferimenti alle entità e direttiva @key» è gratuita?
Sì — il testo completo di «Riferimenti alle entità e direttiva @key» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso GraphQL APIs with Spring Boot, passa a CoddyKit PRO. Il corso GraphQL APIs with Spring Boot include 4 lezioni in totale.
Cosa imparerò in «Riferimenti alle entità e direttiva @key»?
Padroneggi il cuore di Apollo Federation: scopra come i sottografi condividono le entità usando la direttiva @key e risolvono i riferimenti provenienti da altri sottografi in Spring Boot. Eserciti GraphQL APIs with Spring Boot con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare GraphQL APIs with Spring Boot?
Non è richiesta alcuna esperienza precedente. GraphQL APIs with Spring Boot su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Riferimenti alle entità e direttiva @key»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione GraphQL APIs with Spring Boot?
Sì. Ogni lezione GraphQL APIs with Spring Boot include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Introduzione ad Apollo Federation
- Costruire subgraph federati
- Configurazione e gestione del Gateway
- Riferimenti alle entità e direttiva @key