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

Enums နှင့် Interfaces

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

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

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

`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` ကို ဘုံစနစ်တစ်ခုတည်းနဲ့ ပြသနိုင်ပါတယ်။

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

graphql
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!
}
You should see
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 TypesGraphQL

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

  • Interface implement လုပ်ထားတဲ့ type တစ်ခုက interface field တစ်ခုရဲ့ type ကို narrow/wide လုပ်ခြင်းမပြုဘဲ shape မတူအောင် ပြောင်းမိရင် schema validation error ဖြစ်ပါတယ်။
  • နမူနာ query/mutation ကို production API ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test server နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

`Status` enum (`DRAFT | PUBLISHED | ARCHIVED`) တစ်ခု ရေးပြီး `Tutorial` type ကို `status: Status!` field ထည့်ပါ။

You'll know it worked when: Enum နှင့် interface ကို schema ထဲ ရေးပြီး type များကြား fields share လုပ်နိုင်မည်။

Enums နှင့် Interfaces | Thuta Learning