0Pricing
GraphQL APIs with Spring Boot · レッスン

カスタムデータ型の定義

アプリケーションのデータを表現するため、GraphQLスキーマにカスタムオブジェクト型、スカラー型、列挙型を作成します。

「カスタムデータ型の定義」はCoddyKit上の無料GraphQL APIs with Spring Bootレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「カスタムデータ型の定義」レッスンは無料ですか?

はい。「カスタムデータ型の定義」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、GraphQL APIs with Spring Bootコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 GraphQL APIs with Spring Bootコースには全4レッスンが含まれています。

「カスタムデータ型の定義」で何を学びますか?

アプリケーションのデータを表現するため、GraphQLスキーマにカスタムオブジェクト型、スカラー型、列挙型を作成します。 ブラウザで直接実行するハンズオンコードでGraphQL APIs with Spring Bootを演習し、24時間対応の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. データResolverの実装
  3. 基本的なGraphQLクエリの実行
  4. GraphQLミューテーション:データの変更
← GraphQL APIs with Spring Bootに戻る