နားလည်ထားရမယ့် အချက်
`enum` က field တစ်ခုအတွက် ခွင့်ပြုထားတဲ့ value အတိအကျစာရင်းကို သတ်မှတ်ပေးပါတယ်—string typo ကို compile/validation level မှာ တားနိုင်ပါတယ်။ `interface` ကတော့ object types အများကြား ဘုံ fields (ဥပမာ `id`, `title`) ကို share လုပ်ဖို့ contract တစ်ခုဖြစ်ပါတယ်—type တစ်ခုက `implements` keyword ဖြင့် interface ရဲ့ fields အားလုံးပါအောင် ကတိပေးရပါတယ်။ Client က interface field ကို query လုပ်ပြီး concrete type မသိသေးလည်း ဘုံ data ရနိုင်ပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
`Difficulty` enum ကို `BEGINNER | INTERMEDIATE | ADVANCED` ဖြင့် သတ်မှတ်ပြီး `Tutorial.difficulty` field မှာသုံးမယ်။ `Content` interface ကို `id`, `title` fields ဖြင့်ကြေညာပြီး `Tutorial` နှင့် `Exercise` type နှစ်ခုစလုံး implement လုပ်စေမယ်—homepage search result list တစ်ခုတည်းထဲ content type နှစ်မျိုးလုံး `id`/`title` ကို ဘုံစနစ်တစ်ခုတည်းနဲ့ ပြသနိုင်ပါတယ်။
အတူတူ စမ်းရေးကြည့်မယ်
enum Difficulty {
BEGINNER
INTERMEDIATE
ADVANCED
}
interface Content {
id: ID!
title: String!
}
type Tutorial implements Content {
id: ID!
title: String!
difficulty: Difficulty!
}
type Exercise implements Content {
id: ID!
title: String!
points: Int!
}Enum နှင့် interface ကို schema ထဲ ရေးပြီး type များကြား fields share လုပ်နိုင်မည်။၅ မိနစ် စမ်းကြည့်
`Status` enum (`DRAFT | PUBLISHED | ARCHIVED`) တစ်ခု ရေးပြီး `Tutorial` type ကို `status: Status!` field ထည့်ပါ။
သတိလေးတစ်ချက်
Interface implement လုပ်ထားတဲ့ type တစ်ခုက interface field တစ်ခုရဲ့ type ကို narrow/wide လုပ်ခြင်းမပြုဘဲ shape မတူအောင် ပြောင်းမိရင် schema validation error ဖြစ်ပါတယ်။
GraphQL — Schemas and Types — GraphQL