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

Schema Composition — Federation Basics

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

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

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

Monolithic GraphQL server တစ်ခုတည်းက team ကြီးထွားလာလေ deploy/ownership conflict ဖြစ်လေဖြစ်ပါတယ်—team တစ်ခုချင်းစီက schema ရဲ့ own domain ကို ကိုယ်ပိုင် service အဖြစ် owned/deploy လုပ်ချင်ကြပါတယ်။ Apollo Federation ရဲ့ core idea က team တစ်ခုစီရဲ့ subgraph schema များကို `@key` directive ဖြင့် entity (ဥပမာ `Tutorial`) ကို identify ပြီး gateway (router) တစ်ခုက subgraphs များကို composed unified schema တစ်ခုအဖြစ် client ဆီ ဖော်ပြပေးတာပါ။

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

Tutorial Platform က `tutorials-service` (Tutorial entity ပိုင်) နဲ့ `authors-service` (Author entity ပိုင်) service နှစ်ခုအဖြစ် ခွဲထားမယ်ဆိုပါစို့—`tutorials-service` က `Tutorial @key(fields: "id")` ကို declare ပြီး `authors-service` က `Author @key(fields: "id")` declare ကာ `Tutorial.author` field ကို extend လုပ်ပေးပါတယ်။ Client အနေနဲ့ query တစ်ခုတည်းက gateway ကိုသာ ခေါ်ရပြီး service ခွဲထားကြောင်း လုံးဝမသိရပါ။

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

graphql
# tutorials-service subgraph
type Tutorial @key(fields: "id") {
  id: ID!
  title: String!
  authorId: ID!
}

# authors-service subgraph
type Author @key(fields: "id") {
  id: ID!
  name: String!
}

extend type Tutorial @key(fields: "id") {
  id: ID! @external
  author: Author
}
You should see
Federation ရဲ့ subgraph/gateway model ကို ရှင်းပြပြီး `@key` directive ရဲ့ role ကို နားလည်မည်။

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

`comments-service` subgraph တစ်ခု design လုပ်ပြီး `Comment` entity ကို `Tutorial` (`tutorials-service` ပိုင်) နှင့် ဘယ်လို ချိတ်ဆက်မလဲ ရေးပါ။

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

Federation ကို team တစ်ခုတည်း/service တစ်ခုတည်းရှိသေးတဲ့ project အစောပိုင်းမှာ အစောကြီး introduce လုပ်ရင် operational complexity (gateway, subgraph deploy coordination) က benefit ထက် ကျော်သွားနိုင်ပါတယ်—monolith က mature/team growth ဖြစ်မှ split လုပ်ပါ။

Apollo Federation — IntroductionGraphQL

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

  • Federation ကို team တစ်ခုတည်း/service တစ်ခုတည်းရှိသေးတဲ့ project အစောပိုင်းမှာ အစောကြီး introduce လုပ်ရင် operational complexity (gateway, subgraph deploy coordination) က benefit ထက် ကျော်သွားနိုင်ပါတယ်—monolith က mature/team growth ဖြစ်မှ split လုပ်ပါ။
  • နမူနာ query/mutation ကို production API ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test server နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

`comments-service` subgraph တစ်ခု design လုပ်ပြီး `Comment` entity ကို `Tutorial` (`tutorials-service` ပိုင်) နှင့် ဘယ်လို ချိတ်ဆက်မလဲ ရေးပါ။

You'll know it worked when: Federation ရဲ့ subgraph/gateway model ကို ရှင်းပြပြီး `@key` directive ရဲ့ role ကို နားလည်မည်။

Schema Composition — Federation Basics | Thuta Learning