Thuta Learning
ရှာဖွေရန်
GraphQL
BasicWeb Developmentbeginner

Schema နှင့် Type System

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Schema နှင့် Type System concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ GraphQL query/code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

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) ကွာခြားချက်ကိုသတိထားပါ။

အတူတူ စမ်းရေးကြည့်မယ်

graphql
type Tutorial {
  id: ID!
  title: String!
  summary: String
  lessonCount: Int!
  isFeatured: Boolean!
}

type Query {
  tutorial(id: ID!): Tutorial
  tutorials: [Tutorial!]!
}
You should see
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 TypesGraphQL

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Field တစ်ခုကို `!` (non-null) လုပ်ပြီးမှ resolver က `null` ပြန်ပေးမိရင် GraphQL execution error ဖြစ်တတ်ပါတယ်။ Field တကယ် lawfully null ဖြစ်နိုင်ရင် nullable ထားရပါမယ်။
  • နမူနာ query/mutation ကို production API ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test server နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

`id`, `name`, `bio` (nullable), `tutorials` (list) ပါသော `Author` type တစ်ခုနှင့် `author(id: ID!): Author` field ပါသော `Query` extension ရေးပါ။

You'll know it worked when: Type, scalar, non-null/list syntax ကို ဖတ်ပြီး ရိုးရှင်းသော schema တစ်ခု ရေးနိုင်မည်။

Schema နှင့် Type System | Thuta Learning