API スキーマ¶
はじめに¶
本サイトにつきまして、【IT技術の知見】はじめに - 俺の技術ノート をご認識のほど宜しくお願いいたします。
01. API スキーマ¶
API スキーマとは¶
GraphQL-API が受信する CRUD のデータ型や必須データを定義したもの。受信したデータのバリデーションに使用する。
02. 文法¶
enum¶
enum 型のデータを設定する。
enum GenderType {
UNKNOWN
MAN
WOMAN
}
type User {
id: ID!
gender: GenderType
}
input¶
Mutation 処理の引数として受信するデータ構造を設定する。
input UserCreateInput {
name: String!
email: String!
}
type Mutation {
createUser(data: UserCreateInput!): User!
}
interface¶
複数の type で共通するフィールドを設定する。
interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
}
scalar¶
フィールドのデータ型のうち、それ以上分解できない型である。
組み込みの型として Int、Float、String、Boolean、ID がある。また、独自の型も定義できる。
scalar DateTime
type User {
id: ID!
createdAt: DateTime!
}
type¶
▼ type とは¶
レスポンスで返信するデータ構造を設定する。
フィールドの型に ! をつけると必須 (null 不可) になり、[] で囲むと配列になる。
type User {
id: ID!
name: String!
# 必須の User型の配列
friends: [User!]!
}
▼ ルート型¶
クライアントがリクエストする処理の入口となる type である。
Query (データ読出)、Mutation (データ作成/更新/削除)、Subscription (データ変更の通知) がある。
type Query {
user(id: ID!): User
}
type Mutation {
createUser(data: UserCreateInput!): User!
}
type Subscription {
userCreated: User!
}
union¶
複数の type のいずれかを返信するデータ型を設定する。
union SearchResult = User | Post
type Query {
search(text: String!): [SearchResult!]!
}