GraphQL APIs with Spring Boot · บทเรียน

การอ้างอิงเอนทิตีและคำสั่ง @key

เชี่ยวชาญหัวใจของ Apollo Federation โดยเรียนรู้วิธีที่กราฟย่อยใช้ @key เพื่อแบ่งปันเอนทิตี และแก้ไขการอ้างอิงจากกราฟย่อยอื่นใน Spring Boot

บทเรียน 4 จาก 413 ขั้นตอน

การอ้างอิงเอนทิตีและคำสั่ง @key เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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.

เริ่มต้นได้ฟรี

เรียนรู้ GraphQL APIs with Spring Boot ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การอ้างอิงเอนทิตีและคำสั่ง @key” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การอ้างอิงเอนทิตีและคำสั่ง @key” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การอ้างอิงเอนทิตีและคำสั่ง @key”

เชี่ยวชาญหัวใจของ Apollo Federation โดยเรียนรู้วิธีที่กราฟย่อยใช้ @key เพื่อแบ่งปันเอนทิตี และแก้ไขการอ้างอิงจากกราฟย่อยอื่นใน Spring Boot คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การอ้างอิงเอนทิตีและคำสั่ง @key” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม

ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนะนำ Apollo Federation
  2. การสร้างซับกราฟแบบรวมศูนย์
  3. การตั้งค่าและจัดการ Gateway
  4. การอ้างอิงเอนทิตีและคำสั่ง @key
← กลับไปที่ GraphQL APIs with Spring Boot