0Pricing
GraphQL APIs with Spring Boot · 课时

定义自定义数据类型

在 GraphQL 模式中创建自定义对象类型、标量类型和枚举,以表示应用的数据。

定义自定义数据类型 是 CoddyKit 上的免费 GraphQL APIs with Spring Boot 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 GraphQL APIs with Spring Boot 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 GraphQL APIs with Spring Boot 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「定义自定义数据类型」课时是免费的吗?

是的 — 「定义自定义数据类型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 GraphQL APIs with Spring Boot 课程的其余内容,请升级到 CoddyKit PRO。 GraphQL APIs with Spring Boot 课程共包含 4 节课。

「定义自定义数据类型」这节课中我会学到什么?

在 GraphQL 模式中创建自定义对象类型、标量类型和枚举,以表示应用的数据。 你通过在浏览器中直接运行的动手代码来练习 GraphQL APIs with Spring Boot,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 GraphQL APIs with Spring Boot 需要有经验吗?

无需任何先前经验。CoddyKit 上的 GraphQL APIs with Spring Boot 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「定义自定义数据类型」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 GraphQL APIs with Spring Boot 课中编写并运行代码吗?

能。每节 GraphQL APIs with Spring Boot 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 定义自定义数据类型
  2. 实现数据解析器
  3. 执行简单的 GraphQL 查询
  4. GraphQL 变更:修改数据
← 返回 GraphQL APIs with Spring Boot