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

อีนัมและชนิดสเกลาร์แบบกำหนดเอง

ขยายสคีมา GraphQL ให้เกินกว่าชนิดข้อมูลในตัว โดยกำหนดอีนัมสำหรับชุดค่าที่ตายตัวและสเกลาร์แบบกำหนดเองสำหรับชนิดข้อมูลในโดเมน เช่น วันที่และ URL ใน Spring Boot

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

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

Beyond Built-in Types

GraphQL ships with scalars like Int, String, and Boolean. But real domains need more: a fixed set of statuses, a proper date type, or a validated email.

Enums and custom scalars let your schema express these precisely.

What Is a GraphQL Enum?

An enum restricts a field to a fixed list of named values. Clients can only send or receive one of those values, and tooling can offer autocompletion.

enum OrderStatus {
  PENDING
  SHIPPED
  DELIVERED
  CANCELLED
}

Using an Enum in a Type

Reference the enum like any other type. The field is now guaranteed to hold a valid status.

type Order {
  id: ID!
  status: OrderStatus!
}

Mapping Enums to Java

Spring for GraphQL maps a schema enum to a Java enum automatically when the names match exactly.

public enum OrderStatus {
    PENDING, SHIPPED, DELIVERED, CANCELLED
}

Why Custom Scalars?

The built-in scalars are limited. Representing a date as a String loses meaning and validation. A custom scalar defines how a domain value is serialized, deserialized, and validated.

Declaring a Scalar in the Schema

First declare the scalar name in your SDL, then use it on fields.

scalar DateTime

type Event {
  id: ID!
  startsAt: DateTime!
}

Using a Pre-built Scalar Library

The graphql-java-extended-scalars library provides ready-made scalars for DateTime, Date, URL, and more, so you do not write them by hand.

// build.gradle
implementation 'com.graphql-java:graphql-java-extended-scalars:21.0'

Registering a Scalar in Spring

Wire the scalar into the runtime with a RuntimeWiringConfigurer bean so the schema knows how to handle it.

@Bean
public RuntimeWiringConfigurer scalars() {
    return wiring -> wiring.scalar(ExtendedScalars.DateTime);
}

Writing Your Own Scalar

For a domain type you can define a GraphQLScalarType with a custom Coercing implementation controlling parse and serialize logic.

GraphQLScalarType.newScalar()
    .name("Email")
    .coercing(new EmailCoercing())
    .build();

Validation Inside Coercing

A scalar's Coercing can reject invalid input by throwing a CoercingParseValueException, giving you type-level validation for free.

if (!value.toString().contains("@")) {
    throw new CoercingParseValueException("Invalid email");
}

Best Practices

Use these types wisely:

  • Prefer enums over free-form strings for fixed sets
  • Reuse library scalars before writing your own
  • Keep Coercing logic pure and predictable
  • Document each custom scalar's expected format

Quick Check

Test your knowledge of enums and scalars.

Recap

You extended your schema's type system:

  • Enums restrict fields to a fixed value set
  • They map automatically to Java enums by name
  • Custom scalars model domain types like dates and emails
  • Use library scalars or write a Coercing for your own
  • Register scalars via RuntimeWiringConfigurer

Richer types make your API safer and more expressive.

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

บทเรียน “อีนัมและชนิดสเกลาร์แบบกำหนดเอง” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “อีนัมและชนิดสเกลาร์แบบกำหนดเอง”

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

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

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

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

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

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

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

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

  1. การจำลองออบเจ็กต์ซ้อนกันและความสัมพันธ์
  2. การนำอินเทอร์เฟซและชนิด Union มาใช้
  3. การใช้ชนิดข้อมูลนำเข้าสำหรับมิวเทชัน
  4. อีนัมและชนิดสเกลาร์แบบกำหนดเอง
← กลับไปที่ GraphQL APIs with Spring Boot