列挙型とカスタムスカラー型
Spring Bootで、固定値の集合には列挙型を、日付やURLなどのドメイン型にはカスタムスカラーを定義し、組み込み型を超えてGraphQLスキーマを拡張します。
「列挙型とカスタムスカラー型」はCoddyKit上の無料GraphQL APIs with Spring Bootレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「列挙型とカスタムスカラー型」レッスンは無料ですか?
はい。「列挙型とカスタムスカラー型」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、GraphQL APIs with Spring Bootコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 GraphQL APIs with Spring Bootコースには全4レッスンが含まれています。
「列挙型とカスタムスカラー型」で何を学びますか?
Spring Bootで、固定値の集合には列挙型を、日付やURLなどのドメイン型にはカスタムスカラーを定義し、組み込み型を超えてGraphQLスキーマを拡張します。 ブラウザで直接実行するハンズオンコードでGraphQL APIs with Spring Bootを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。