Enums and Custom Scalar Types
Extend your GraphQL schema beyond the built-in types by defining enums for fixed value sets and custom scalars for domain types like dates and URLs in Spring Boot.
Enums and Custom Scalar Types is a free GraphQL APIs with Spring Boot lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the GraphQL APIs with Spring Boot learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Enums and Custom Scalar Types” lesson free?
Yes — the full text of “Enums and Custom Scalar Types” is free to read here on the web, and the GraphQL APIs with Spring Boot course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the GraphQL APIs with Spring Boot course, upgrade to CoddyKit PRO.
What will I learn in “Enums and Custom Scalar Types”?
Extend your GraphQL schema beyond the built-in types by defining enums for fixed value sets and custom scalars for domain types like dates and URLs in Spring Boot. You practise GraphQL APIs with Spring Boot with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start GraphQL APIs with Spring Boot?
No prior experience is required. GraphQL APIs with Spring Boot on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enums and Custom Scalar Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this GraphQL APIs with Spring Boot lesson?
Yes. Every GraphQL APIs with Spring Boot lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Modeling Nested Objects & Relationships
- Implementing Interfaces and Union Types
- Leveraging Input Types for Mutations
- Enums and Custom Scalar Types