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

Queries ရေးခြင်း — Fields, Nesting, Aliases

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

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

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

GraphQL query တစ်ခုက curly braces ထဲမှာ ကိုယ်လိုချင်တဲ့ fields ကို tree ပုံစံ selection set အနေနဲ့ ရေးထားတာဖြစ်ပါတယ်။ Object type field တစ်ခုအောက် (ဥပမာ `author`) sub-selection ပါရင် nested query ဖြစ်ပါတယ်။ Query တစ်ခုတည်းထဲမှာ field တစ်ခုတည်းကို parameter မတူဘဲ နှစ်ကြိမ်ခေါ်ချင်ရင် alias (`featured: tutorials`) သုံးပြီး result key ကို ကိုယ့်ဘာသာ ပြောင်းနိုင်ပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Tutorial card component အတွက် `tutorial(id: "42")` ကို `id`, `title`, နဲ့ nested `author { name }` ဖြင့်တောင်းမယ်။ Homepage မှာ featured tutorials list နဲ့ latest tutorials list ကို field တစ်ခုတည်းဖြစ်တဲ့ `tutorials` ကို alias နှစ်ခုနဲ့ query တစ်ခုတည်းထဲခေါ်နိုင်ပါတယ်။

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

graphql
query TutorialCard {
  tutorial(id: "42") {
    id
    title
    author {
      name
    }
  }
  featured: tutorials {
    title
  }
  latest: tutorials {
    title
  }
}
You should see
Nested field selection နှင့် alias ပါသော query တစ်ခုကို ကိုယ်တိုင်ရေးနိုင်မည်။

၅ မိနစ် စမ်းကြည့်

`tutorial(id: "7")` ကို `title` နှင့် nested `lessons { title }` ဖြင့် တောင်းပြီး၊ `tutorials` field ကို `featured` နှင့် `archived` alias နှစ်ခုနဲ့ ခေါ်ပါ။

သတိလေးတစ်ချက်

Object type field တစ်ခုအောက် selection set မထည့်ဘဲ ချန်ထားရင် GraphQL validation error ဖြစ်ပါတယ်—scalar field ချည်းသာ leaf ဖြစ်နိုင်ပါတယ်။

GraphQL — Queries and MutationsGraphQL

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

  • Object type field တစ်ခုအောက် selection set မထည့်ဘဲ ချန်ထားရင် GraphQL validation error ဖြစ်ပါတယ်—scalar field ချည်းသာ leaf ဖြစ်နိုင်ပါတယ်။
  • နမူနာ query/mutation ကို production API ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test server နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

`tutorial(id: "7")` ကို `title` နှင့် nested `lessons { title }` ဖြင့် တောင်းပြီး၊ `tutorials` field ကို `featured` နှင့် `archived` alias နှစ်ခုနဲ့ ခေါ်ပါ။

You'll know it worked when: Nested field selection နှင့် alias ပါသော query တစ်ခုကို ကိုယ်တိုင်ရေးနိုင်မည်။

Queries ရေးခြင်း — Fields, Nesting, Aliases | Thuta Learning