နားလည်ထားရမယ့် အချက်
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 လုပ်နိုင်ပါတယ်။
အတူတူ စမ်းရေးကြည့်မယ်
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}`);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 Started — GraphQL