枚举与自定义标量类型
通过定义表示固定值集合的枚举,以及表示日期和 URL 等领域类型的自定义标量,在 Spring Boot 中扩展 GraphQL 模式,超越内置类型的限制。
枚举与自定义标量类型 是 CoddyKit 上的免费 GraphQL APIs with Spring Boot 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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
Coercinglogic 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
Coercingfor your own - Register scalars via
RuntimeWiringConfigurer
Richer types make your API safer and more expressive.
常见问题解答
「枚举与自定义标量类型」课时是免费的吗?
是的 — 「枚举与自定义标量类型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 GraphQL APIs with Spring Boot 课程的其余内容,请升级到 CoddyKit PRO。 GraphQL APIs with Spring Boot 课程共包含 4 节课。
「枚举与自定义标量类型」这节课中我会学到什么?
通过定义表示固定值集合的枚举,以及表示日期和 URL 等领域类型的自定义标量,在 Spring Boot 中扩展 GraphQL 模式,超越内置类型的限制。 你通过在浏览器中直接运行的动手代码来练习 GraphQL APIs with Spring Boot,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 GraphQL APIs with Spring Boot 需要有经验吗?
无需任何先前经验。CoddyKit 上的 GraphQL APIs with Spring Boot 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「枚举与自定义标量类型」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 GraphQL APIs with Spring Boot 课中编写并运行代码吗?
能。每节 GraphQL APIs with Spring Boot 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 建模嵌套对象与关系
- 实现接口与联合类型
- 利用输入类型实现变更
- 枚举与自定义标量类型