初めてのGraphQLスキーマを設計する
基本的なスキーマの型とフィールドを定義し、Schema Definition Language(SDL)について学びます。
「初めてのGraphQLスキーマを設計する」はCoddyKit上の無料GraphQL APIs with Spring Bootレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはGraphQL APIs with Spring Boot学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 GraphQL APIs with Spring Bootコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What's a GraphQL Schema?
A GraphQL schema is the blueprint of your API: it defines what data clients can ask for and how it’s structured. It’s the core of every GraphQL service.
Meet SDL: Schema Language
You write schemas in the Schema Definition Language (SDL) — a simple, readable syntax for declaring object types, their fields, and how they relate.
Building Blocks: Object Types
The core building block is the object type, declared with the type keyword. Like a class, it represents a kind of object you can fetch.
type User {
# Fields will go here
}Adding Fields with Scalar Types
Fields hold data via built-in scalar types: String, Int, Float, Boolean, and ID for unique identifiers.
type User {
id: ID
name: String
age: Int
isActive: Boolean
rating: Float
}Essential Data: Non-Null Fields
Add ! after a type to make a field non-null — it must always have a value. Query it and the server must return one or it errors.
type Product {
id: ID!
name: String!
price: Float!
description: String
}Collections: Lists in GraphQL
Wrap a type in [] for a list. Stack the ! too: [String!]! means a non-null list of non-null strings.
type Book {
id: ID!
title: String!
pages: Int
}
type Author {
id: ID!
name: String!
books: [Book] # A list of books, can be empty or null
tags: [String!]! # A list of non-null strings, and the list itself is non-null
}Entry Point: The Query Type
Every schema needs the root Query type — it defines all the entry points for clients to read data. Queries always start here.
type Query {
# Query fields will go here
}Defining Query Fields
Fields on Query are the data you can fetch, and they take arguments to filter — like book(id: ID!): Book to grab one book by id.
type Author {
id: ID!
name: String!
}
type Book {
id: ID!
title: String!
}
type Query {
allAuthors: [Author!]!
bookById(id: ID!): Book
}Your First Complete Schema
Here it all comes together: Author and Book types, a Query entry point, and a Book.author field that links the two. Relationships!
type Author {
id: ID!
name: String!
books: [Book!]! # An author has a list of non-null books
}
type Book {
id: ID!
title: String!
pages: Int
author: Author # A book has one author
}
type Query {
allBooks: [Book!]!
book(id: ID!): Book
allAuthors: [Author!]!
author(id: ID!): Author
}Schema Check-up
What does ! mean, and where does Query fit? Prove it.
Schema Design Recap
You designed a real schema: object types with the type keyword, scalar fields, ! for non-null, [] for lists, and Query as the read entry point.
よくある質問
「初めてのGraphQLスキーマを設計する」レッスンは無料ですか?
はい。「初めてのGraphQLスキーマを設計する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、GraphQL APIs with Spring Bootコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 GraphQL APIs with Spring Bootコースには全4レッスンが含まれています。
「初めてのGraphQLスキーマを設計する」で何を学びますか?
基本的なスキーマの型とフィールドを定義し、Schema Definition Language(SDL)について学びます。 ブラウザで直接実行するハンズオンコードでGraphQL APIs with Spring Bootを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
GraphQL APIs with Spring Bootを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのGraphQL APIs with Spring Bootは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「初めてのGraphQLスキーマを設計する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このGraphQL APIs with Spring Bootレッスンでコードを書いて実行できますか?
はい。すべてのGraphQL APIs with Spring Bootレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- GraphQLとは
- GraphQL向けSpring Bootのセットアップ
- 初めてのGraphQLスキーマを設計する
- GraphQLとREST:適切なアプローチの選択