การนำอินเทอร์เฟซและชนิด Union มาใช้
ใช้อินเทอร์เฟซและชนิด Union เพื่อสร้างการออกแบบสคีมาที่มีความยืดหยุ่นและรองรับพหุสัณฐาน
การนำอินเทอร์เฟซและชนิด Union มาใช้ เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Flexible Schemas with Polymorphism
GraphQL allows you to design highly flexible APIs. Sometimes, you need a field that can return different types of objects, or you want to define a shared set of fields that multiple types must implement. This is where polymorphism comes in.
We'll explore two key concepts for achieving this: Interfaces and Union Types.
Understanding GraphQL Interfaces
Imagine you have different types of media, like a Book and a Movie. Both might have a title and a releaseYear. Instead of duplicating these fields, you can define an Interface.
An interface is a contract that specifies a set of fields that any type implementing it must include. It's like a blueprint for common functionality.
Defining an Interface in SDL
In GraphQL's Schema Definition Language (SDL), an interface is defined using the interface keyword. Here, we define a Media interface that both Book and Movie will share.
interface Media {
id: ID!
title: String!
releaseYear: Int
}Types Implementing an Interface
Now, let's make our Book and Movie types implement the Media interface. They must include all fields defined by Media, and can add their own unique fields. Notice the implements keyword.
type Book implements Media {
id: ID!
title: String!
releaseYear: Int
author: String
pages: Int
}
type Movie implements Media {
id: ID!
title: String!
releaseYear: Int
director: String
durationMinutes: Int
}Resolvers for Interfaces
When you query a field that returns an interface type, Spring GraphQL automatically resolves the concrete type based on the data. You usually create Java data classes that implement a common Java interface, and Spring GraphQL handles the rest. Try running this example:
package com.coddykit.graphql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import java.util.List;
import java.util.ArrayList;
@SpringBootApplication
public class GraphqlApp {
public static void main(String[] args) {
SpringApplication.run(GraphqlApp.class, args);
}
}
interface Media {
String getId();
String getTitle();
Integer getReleaseYear();
}
record Book(String id, String title, Integer releaseYear, String author, Integer pages) implements Media {}
record Movie(String id, String title, Integer releaseYear, String director, Integer durationMinutes) implements Media {}
@Controller
class MediaController {
@QueryMapping
public List<Media> allMedia() {
List<Media> mediaList = new ArrayList<>();
mediaList.add(new Book("b1", "The Hitchhiker's Guide", 1979, "Douglas Adams", 193));
mediaList.add(new Movie("m1", "Inception", 2010, "Christopher Nolan", 148));
return mediaList;
}
}Exploring GraphQL Union Types
While interfaces ensure types share common fields, Union Types allow a field to return one of several distinct types, without requiring them to share any common fields. Think of it as an 'either-or' situation. For example, a search result might be a Product OR a User.
Schema Definition for Unions
In SDL, union types are defined using the union keyword, followed by the types it can represent, separated by a vertical bar |. Here's a SearchResult union that can be either a Book or a Movie.
union SearchResult = Book | MovieResolvers for Union Types
When a field returns a union type, Spring GraphQL needs to know which concrete type it is. For Java, you simply return an instance of one of the types defined in the union. Spring GraphQL automatically adds the __typename field for clients to distinguish. Try running this example:
package com.coddykit.graphql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
@SpringBootApplication
public class GraphqlApp {
public static void main(String[] args) {
SpringApplication.run(GraphqlApp.class, args);
}
}
record Book(String id, String title, Integer releaseYear, String author, Integer pages) {}
record Movie(String id, String title, Integer releaseYear, String director, Integer durationMinutes) {}
@Controller
class SearchController {
@QueryMapping
public Object search(@Argument String query) {
if (query.equalsIgnoreCase("inception")) {
return new Movie("m1", "Inception", 2010, "Christopher Nolan", 148);
} else if (query.equalsIgnoreCase("hitchhiker")) {
return new Book("b1", "The Hitchhiker's Guide", 1979, "Douglas Adams", 193);
}
return null;
}
}Choosing Between Interfaces and Unions
Both interfaces and unions offer polymorphism, but they serve different purposes:
- Interfaces: Use when multiple types share a common set of fields and behavior. They enforce a contract.
- Union Types: Use when a field can return one of several distinct types that do not necessarily share any common fields. It's about 'either A or B or C'.
Interfaces vs. Unions Quiz
You are designing a GraphQL API. You need a field that can return either a Photo object or a Video object. These two types have completely different fields and do not share any common properties. Which GraphQL schema feature is best suited for this scenario?
Recap: Flexible Schemas
You've learned how GraphQL Interfaces and Union Types enable flexible and polymorphic schema designs. Interfaces define a contract for common fields, while Union Types allow a field to return one of several distinct types. These tools are crucial for building robust and adaptable GraphQL APIs.
Next, you'll explore Input Types to streamline mutation arguments.
เรียนรู้ GraphQL APIs with Spring Boot ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การนำอินเทอร์เฟซและชนิด Union มาใช้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การนำอินเทอร์เฟซและชนิด Union มาใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การนำอินเทอร์เฟซและชนิด Union มาใช้”
ใช้อินเทอร์เฟซและชนิด Union เพื่อสร้างการออกแบบสคีมาที่มีความยืดหยุ่นและรองรับพหุสัณฐาน คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การนำอินเทอร์เฟซและชนิด Union มาใช้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม
ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจำลองออบเจ็กต์ซ้อนกันและความสัมพันธ์
- การนำอินเทอร์เฟซและชนิด Union มาใช้
- การใช้ชนิดข้อมูลนำเข้าสำหรับมิวเทชัน
- อีนัมและชนิดสเกลาร์แบบกำหนดเอง