Defining Custom Data Types
Create custom object types, scalar types, and enums in your GraphQL schema to represent your application's data.
Defining Custom Data Types is a free GraphQL APIs with Spring Boot lesson on CoddyKit — lesson 1 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.
Custom Types: Your Data's Blueprint
In GraphQL, data types are like blueprints for your application's information. They define the shape and kind of data that can be queried or modified.
While GraphQL provides some basic types, you'll often need to define your own to perfectly match your application's unique data structure.
GraphQL's Building Blocks
GraphQL has built-in scalar types like String, Int, Boolean, Float, and ID. These are fundamental, single-value types.
But real-world data is complex! This is where Object Types come in. They let you define objects with multiple fields, each having its own type.
Crafting Object Types
You define an object type using the type keyword in GraphQL's Schema Definition Language (SDL). Think of it as creating a custom class or struct.
Each field within an object type has a name and a type. Fields can be other object types, scalar types, or even lists of types.
Example: A 'Book' Object
Let's imagine we're building an API for a library. We'd need a way to represent a book. Here's how you might define a Book object type:
type Book {
id: ID!
title: String!
author: String
publicationYear: Int
}The ! after a type means the field is non-nullable – it must always have a value.
Beyond Basic Scalars
Sometimes, built-in scalars aren't specific enough. For example, a Date field could be a String, but what if you need to ensure a specific format?
Custom Scalar Types allow you to define your own scalar types with specific serialization, parsing, and validation rules. This ensures data consistency.
Declaring Custom Scalars
Defining a custom scalar in your schema is straightforward. You use the scalar keyword, followed by the name of your new scalar type.
Later, your Spring Boot application will need to provide the actual logic for handling this custom scalar's data transformations.
scalar Date
type Event {
id: ID!
name: String!
eventDate: Date!
}Enums for Fixed Values
Enum Types (short for 'enumeration') are special scalar types that represent a finite set of possible values. They're great for fields where you want to restrict choices to a predefined list.
Using enums makes your schema more self-documenting and helps prevent invalid data from being sent to your API.
Defining an Enum Type
For our library API, a book might have a specific status. We can define an enum for this:
enum BookStatus {
AVAILABLE
CHECKED_OUT
LOST
REPAIR
}Now, any field using BookStatus can only have one of these four values.
Combining Your Custom Types
Let's update our Book type to use our new BookStatus enum and a custom Date scalar.
scalar Date
enum BookStatus {
AVAILABLE
CHECKED_OUT
LOST
}
type Book {
id: ID!
title: String!
author: String
publicationYear: Int
status: BookStatus!
lastCheckedOut: Date
}This shows how custom types build a richer, more precise schema.
Check Your Understanding
Which of the following statements about GraphQL custom data types are TRUE?
Recap: Your Data's Foundation
You've learned how to define custom data types in GraphQL using SDL!
- Object Types (
type) structure complex data. - Custom Scalar Types (
scalar) handle specific data formats. - Enum Types (
enum) restrict field values to a finite set.
These tools are crucial for building a precise and robust GraphQL schema that accurately represents your application's domain.
Frequently asked questions
Is the “Defining Custom Data Types” lesson free?
Yes — the full text of “Defining Custom Data 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 “Defining Custom Data Types”?
Create custom object types, scalar types, and enums in your GraphQL schema to represent your application's data. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Custom Data 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
- Defining Custom Data Types
- Implementing Data Resolvers
- Executing Simple GraphQL Queries
- GraphQL Mutations: Changing Data