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

Apollo Server စတင်ခြင်း

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

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

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

Apollo Server က GraphQL spec ကို implement လုပ်ထားတဲ့ Node.js library ဖြစ်ပြီး `typeDefs` (SDL string) နဲ့ `resolvers` (object map) နှစ်ခုတည်း ပေးရုံနဲ့ working server တစ်ခု ရနိုင်ပါတယ်။ `startStandaloneServer` helper က HTTP server setup, CORS, body parsing အားလုံးကို default configuration နဲ့ ကိုင်တွယ်ပေးလို့ getting-started အတွက် အလွယ်ဆုံး entry point ဖြစ်ပါတယ်—production မှာ Express/Fastify integration အသုံးများပါတယ်။

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

Tutorial Platform API ကို `typeDefs` (Tutorial type + tutorials query) နှင့် `resolvers` (tutorials query ကို in-memory/db repository ကနေ ဖတ်တဲ့ function) ဖြင့် ရေးပြီး port 4000 မှာ run မယ်။ `npm run dev` ပြီးနောက် Apollo Sandbox ကို browser မှာဖွင့်ပြီး query ကို live testing လုပ်နိုင်ပါတယ်။

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

typescript
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';

const typeDefs = `#graphql
  type Tutorial {
    id: ID!
    title: String!
  }

  type Query {
    tutorials: [Tutorial!]!
  }
`;

const resolvers = {
  Query: {
    tutorials: () => tutorialRepository.findAll(),
  },
};

const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server, { listen: { port: 4000 } });
console.log(`Tutorial Platform API ready at ${url}`);
You should see
Local Apollo Server တစ်ခုကို run ပြီး Apollo Sandbox ထဲက query လုပ်ကြည့်နိုင်မည်။

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

`tutorial(id: ID!): Tutorial` field တစ်ခု `Query` type ထဲ ထပ်ထည့်ပြီး ဆီလျော်သော resolver function ရေးပါ။

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

`typeDefs` ထဲက type name (`Tutorial`) နှင့် `resolvers` object ထဲက key name မကိုက်ညီရင် (typo) resolver ကို server က ဘယ်တော့မှ မခေါ်ဘဲ default resolver ကိုပဲ fallback သုံးနိုင်ပြီး silent bug ဖြစ်တတ်ပါတယ်။

Apollo Server — Getting StartedGraphQL

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

  • `typeDefs` ထဲက type name (`Tutorial`) နှင့် `resolvers` object ထဲက key name မကိုက်ညီရင် (typo) resolver ကို server က ဘယ်တော့မှ မခေါ်ဘဲ default resolver ကိုပဲ fallback သုံးနိုင်ပြီး silent bug ဖြစ်တတ်ပါတယ်။
  • နမူနာ query/mutation ကို production API ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test server နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

`tutorial(id: ID!): Tutorial` field တစ်ခု `Query` type ထဲ ထပ်ထည့်ပြီး ဆီလျော်သော resolver function ရေးပါ။

You'll know it worked when: Local Apollo Server တစ်ခုကို run ပြီး Apollo Sandbox ထဲက query လုပ်ကြည့်နိုင်မည်။

Apollo Server စတင်ခြင်း | Thuta Learning