Referensi Entitas dan Direktif @key
Kuasai inti Apollo Federation: cara subgraf berbagi entitas menggunakan direktif @key dan menyelesaikan referensi dari subgraf lain di Spring Boot.
Referensi Entitas dan Direktif @key adalah pelajaran GraphQL APIs with Spring Boot gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar GraphQL APIs with Spring Boot, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus GraphQL APIs with Spring Boot mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Referensi Entitas dan Direktif @key” gratis?
Ya — teks lengkap “Referensi Entitas dan Direktif @key” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus GraphQL APIs with Spring Boot, upgrade ke CoddyKit PRO. Kursus GraphQL APIs with Spring Boot mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Referensi Entitas dan Direktif @key”?
Kuasai inti Apollo Federation: cara subgraf berbagi entitas menggunakan direktif @key dan menyelesaikan referensi dari subgraf lain di Spring Boot. Kamu berlatih GraphQL APIs with Spring Boot dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai GraphQL APIs with Spring Boot?
Tidak diperlukan pengalaman sebelumnya. GraphQL APIs with Spring Boot di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Referensi Entitas dan Direktif @key” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran GraphQL APIs with Spring Boot ini?
Ya. Setiap pelajaran GraphQL APIs with Spring Boot menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pengantar Apollo Federation
- Membangun Subgraf Federasi
- Penyiapan dan Pengelolaan Gerbang
- Referensi Entitas dan Direktif @key