December 30, 2023
GraphQL は厳密に型指定された言語です。 タイプシステム GraphQL アプリケーションで使用できるさまざまなデータ型を定義します。
スカラーは単なるデータ型ではなく、単一の値を格納する原材料です。GraphQL には、デフォルトのスカラータイプが標準で用意されています。
整数
: 符号付き 32 ビット整数。フロート
: 符号付き倍精度浮動小数点値。ストリング
: UTF-8 文字シーケンス。ブール値
: 本当
または 偽
。ID
: ID
一意の識別子を意味するscalar Date
# We defined Date as a scalar type
オブジェクトは単なる概念ではなく、構造化データの設計図です。サービスからオブジェクトを取得したり、サービスに含まれる項目を取得したりできます。
type User {
id: ID!
username: String!
email: String!
posts: [Post]
}
# User type object contains various scalar type like username is of String,
# id is of ID Scalar type
他の特定のタイプへのエントリポイントタイプ。つまり、それらを使用してスキーマで定義されている他のタイプを取得できます。
type Query {
getUser(id: ID): User
posts:[Post]
}
# getUser query will be used to fetch User type based on some parameter
# posts query will return array of Post type
データ操作のエントリポイントです。通常、データの更新にはミューテーションを使用します。これらは以下と同じように機能します。 クエリ する
type Mutation{
updatePost(id: ID): Post
}
# updatePost Mutation type will update the Post object type and return the updated Pst type
とも呼ばれる 列挙型、列挙型は特定の許容値のセットに制限される特殊な種類のスカラーです。これにより、次のことが可能になります。
enum POST_STATUS{
PUBLISHED
DRAFT
ARCHIVE
}
# POST_STATUS type defined that POST_STATUS can only be one of the given items
あの インタフェース インターフェイスを実装するために型に含める必要がある特定のフィールドセットを含む抽象型です
# interface definition
interface Character {
id: ID!
name: String!
}
# implementation of interface
type User implements Character {
id: ID!
name: String!
email: Sring
}
ユニオンは2つ以上のタイプの組み合わせで、どの組み合わせアイテムタイプでも結果が得られる場合に使用されます
union SearchResult = User| Post
# union definition
# now SearchResult can either be User or Post types