နားလည်ထားရမယ့် အချက်
GraphQL schema က API ထဲမှာရှိနိုင်တဲ့ data shape အားလုံးကို type အနေနဲ့ ကြေညာထားတဲ့ contract ဖြစ်ပါတယ်။ Built-in scalar types မှာ `String`, `Int`, `Float`, `Boolean`, `ID` ပါဝင်ပြီး `!` က non-null ကို၊ `[Type!]!` လို syntax က non-null list of non-null items ကို ဆိုလိုပါတယ်။ `Query` က root type တစ်ခုဖြစ်ပြီး client ဖတ်နိုင်တဲ့ entry point fields အားလုံးကို ကြေညာထားပါတယ်။ ဒီ type language ကို Schema Definition Language (SDL) လို့ခေါ်ပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Tutorial Platform ရဲ့ core `Tutorial` type ကို `id`, `title`, `summary`, `lessonCount`, `isFeatured` fields နဲ့ ကြေညာမယ်။ `id` ကို `ID!` (non-null) လုပ်ထားတာက tutorial တိုင်းမှာ id အမြဲရှိရမယ်ဆိုတဲ့ guarantee ကို schema level မှာတင်ထားတာပါ။ `Query` type ထဲမှာ `tutorial(id: ID!): Tutorial` (single item, nullable) နဲ့ `tutorials: [Tutorial!]!` (list, non-null) ကွာခြားချက်ကိုသတိထားပါ။
အတူတူ စမ်းရေးကြည့်မယ်
type Tutorial {
id: ID!
title: String!
summary: String
lessonCount: Int!
isFeatured: Boolean!
}
type Query {
tutorial(id: ID!): Tutorial
tutorials: [Tutorial!]!
}Type, scalar, non-null/list syntax ကို ဖတ်ပြီး ရိုးရှင်းသော schema တစ်ခု ရေးနိုင်မည်။၅ မိနစ် စမ်းကြည့်
`id`, `name`, `bio` (nullable), `tutorials` (list) ပါသော `Author` type တစ်ခုနှင့် `author(id: ID!): Author` field ပါသော `Query` extension ရေးပါ။
သတိလေးတစ်ချက်
Field တစ်ခုကို `!` (non-null) လုပ်ပြီးမှ resolver က `null` ပြန်ပေးမိရင် GraphQL execution error ဖြစ်တတ်ပါတယ်။ Field တကယ် lawfully null ဖြစ်နိုင်ရင် nullable ထားရပါမယ်။
GraphQL — Schemas and Types — GraphQL