
tRPC Simplifies Type-Safe API Development
Learn how tRPC provides end-to-end type-safe APIs for TypeScript applications, including procedures, validation, batching, and the advantages and limitations of using tRPC.
What Is tRPC?
Full-stack TypeScript Application တွေ တည်ဆောက်တဲ့အခါ Frontend နဲ့ Backend ကြားမှာ Data Types တွေ တူညီနေဖို့က အရေးကြီးပါတယ်။
ပုံမှန် REST API တွေမှာ Backend က ပြန်ပို့ပေးတဲ့ Data Structure ကို Frontend ဘက်မှာလည်း သီးခြား Type သတ်မှတ်ပေးရတတ်ပါတယ်။ အချိန်ကြာလာတာနဲ့အမျှ Backend မှာ Data Model ပြောင်းလဲသွားပေမဲ့ Frontend Types တွေက လိုက်မပြောင်းဘဲ ကျန်ခဲ့တာမျိုး ဖြစ်နိုင်ပါတယ်။
ဒီပြဿနာကို ဖြေရှင်းဖို့ အသုံးပြုနိုင်တဲ့ နည်းပညာတစ်ခုကတော့ tRPC (TypeScript Remote Procedure Call) ဖြစ်ပါတယ်။
tRPC ဟာ Full-stack TypeScript Projects တွေအတွက် End-to-end Type Safety ရရှိစေတဲ့ RPC Framework တစ်ခု ဖြစ်ပါတယ်။ အဓိကအားသာချက်ကတော့ API Types တွေကို Frontend နဲ့ Backend မှာ သီးခြားစီ ပြန်ရေးစရာမလိုဘဲ Server ဘက်က သတ်မှတ်ထားတဲ့ Types တွေကို Client ဘက်မှာ တိုက်ရိုက် Infer လုပ်အသုံးပြုနိုင်တာပါ။
ဒါကြောင့် သီးခြား API Code Generation လုပ်စရာမလိုဘဲ TypeScript Compiler ရဲ့ Type Checking နဲ့ Autocomplete စွမ်းဆောင်ရည်တွေကို Full-stack Application တစ်ခုလုံးမှာ အသုံးချနိုင်ပါတယ်။
The Traditional API Contract Drift Problem
ရိုးရိုး REST API တစ်ခုကို စဉ်းစားကြည့်ရအောင်။
Backend မှာ User Data ကို အောက်ပါအတိုင်း ပြန်ပေးထားတယ်ဆိုပါစို့။
// Backend Response
{
id: 'usr_101',
name: 'Alice',
role: 'ADMIN'
}
Frontend ဘက်မှာ fetch() ဒါမှမဟုတ် Axios နဲ့ ဒီ Endpoint ကို Request လုပ်တဲ့အခါ TypeScript က Response ရဲ့ Data Structure ကို အလိုအလျောက် မသိနိုင်ပါဘူး။
ဒါကြောင့် Frontend မှာလည်း Type ကို သီးခြားသတ်မှတ်ပေးရတတ်ပါတယ်။
// Frontend
type User = {
id: string
name: string
role: string
}
const response = await fetch('/api/users/101')
const user: User = await response.json()
ဒီနေရာမှာ User Type ကို Developer ကိုယ်တိုင် ရေးထားတာ ဖြစ်ပါတယ်။ Backend မှာ name ကို fullName လို့ ပြောင်းလိုက်ရင် Frontend ဘက်က Type ကိုလည်း လိုက်ပြောင်းဖို့ လိုပါတယ်။
ဒီလို Backend နဲ့ Frontend ကြား API Contract တွေ မကိုက်ညီတော့တဲ့ ပြဿနာကို API Contract Drift လို့ ခေါ်ပါတယ်။
Project ကြီးလာတာနဲ့အမျှ API အရေအတွက် များလာပြီး Types တွေကို နေရာအနှံ့မှာ ထပ်ရေးနေရတဲ့အခါ ဒီလို ပြဿနာတွေ ပိုမိုကြုံတွေ့လာနိုင်ပါတယ်။
The tRPC Architecture and Code Example
tRPC မှာတော့ API ကို URL Endpoint တွေအဖြစ်ပဲ စဉ်းစားတာထက် Typed Procedures တွေအဖြစ် တည်ဆောက်ပါတယ်။ Server ဘက်က AppRouter Type ကို Export လုပ်ပြီး Client ဘက်မှာ Type-only Import အနေနဲ့ အသုံးပြုပါတယ်။
အဓိကအားဖြင့် Procedure အမျိုးအစား ၂ မျိုးကို အသုံးပြုပါတယ်။
- Query: Data ဖတ်ယူဖို့ အသုံးပြုပါတယ်။
- Mutation: Data အသစ်ထည့်တာ၊ ပြင်ဆင်တာနဲ့ ဖျက်တာလို State ပြောင်းလဲစေတဲ့ Operations တွေအတွက် အသုံးပြုပါတယ်။
Server ဘက်မှာ Router တစ်ခု တည်ဆောက်ပြီး အဲဒီ Router ရဲ့ Type ကို Export လုပ်ပေးရုံနဲ့ Client ဘက်က Type Inference ကို အသုံးပြုနိုင်ပါတယ်။
// 1. Server: Define Router and Export Type
import { initTRPC } from '@trpc/server'
import { z } from 'zod'
const t = initTRPC.create()
export const appRouter = t.router({
getUser: t.procedure
.input(z.object({ id: z.string() }))
.query(async ({ input }) => {
return {
id: input.id,
name: 'Alice',
role: 'ADMIN' as const,
}
}),
})
export type AppRouter = typeof appRouter
Client ဘက်မှာ AppRouter Type ကို အသုံးပြုပြီး Type-safe ခေါ်ယူနိုင်ပါတယ်။
// 2. Client: Call with Full Autocomplete & Inferred Types
const user = await trpc.getUser.query({ id: 'usr_101' })
// TypeScript automatically knows user: { id: string, name: string, role: 'ADMIN' }
End-to-End Type Safety and Safe Refactoring
tRPC ရဲ့ အားသာချက်တွေထဲမှာ အရေးအကြီးဆုံးတစ်ခုကတော့ End-to-end Type Safety ဖြစ်ပါတယ်။
Server ဘက်မှာ သတ်မှတ်ထားတဲ့ Input Type, Output Type နဲ့ Procedure Structure တွေကို Client ဘက်မှာပါ TypeScript က သိရှိနိုင်ပါတယ်။
ဥပမာ Backend မှာ Input ကို String အဖြစ် သတ်မှတ်ထားတယ်ဆိုပါစို့။
.input(z.object({
id: z.string(),
}))
Client ဘက်က Number ပေးလိုက်ရင် Code ရေးနေတဲ့အချိန်မှာပဲ TypeScript က Error ပြပေးနိုင်ပါတယ်။
// TypeScript Error
trpc.getUser.query({
id: 101,
})
Refactoring လုပ်တဲ့အခါမှာလည်း အကျိုးရှိပါတယ်။
ဥပမာ Backend မှာ Response Property တစ်ခုကို name ကနေ fullName လို့ ပြောင်းလိုက်တယ်ဆိုပါစို့။ Frontend မှာ user.name လို့ အသုံးပြုထားတဲ့ နေရာတွေကို TypeScript က Error ပြပေးနိုင်ပါတယ်။
ဒါကြောင့် Deploy မလုပ်ခင် Development နဲ့ Build အဆင့်မှာတင် Type Mismatch တွေကို ကြိုတင်ရှာဖွေပြင်ဆင်နိုင်ပါတယ်။
ဒါပေမဲ့ ဒီ Type Safety ဟာ TypeScript Project တွေအတွင်းမှာ အဓိက အလုပ်လုပ်တာဖြစ်ပြီး Runtime မှာ ဝင်လာတဲ့ External Data အားလုံးကို အလိုအလျောက် မှန်ကန်ကြောင်း အာမခံပေးတာတော့ မဟုတ်ပါဘူး။
Runtime Validation with Zod
TypeScript Types တွေဟာ Compile-time မှာသာ အလုပ်လုပ်ပါတယ်။ JavaScript အဖြစ် Compile လုပ်ပြီးသွားတဲ့အခါ Type Information တွေကို Runtime မှာ အသုံးပြုလို့ မရတော့ပါဘူး။
ဒါကြောင့် Client က Server ဆီ ပို့လိုက်တဲ့ Data တွေကို Runtime မှာ စစ်ဆေးဖို့ လိုပါတယ်။
ဒီနေရာမှာ tRPC နဲ့ Zod ကို တွဲဖက်အသုံးပြုနိုင်ပါတယ်။
import { z } from 'zod'
const createUserSchema = z.object({
name: z.string().min(1),
email: z.string().email(),
age: z.number().min(18),
})
export const createUser = t.procedure
.input(createUserSchema)
.mutation(async ({ input }) => {
return await db.user.create({
data: input,
})
})
ဒီဥပမာမှာ Client က ပို့လာတဲ့ Data တွေကို Zod က Runtime မှာ စစ်ဆေးပေးပါတယ်။
nameဟာ Empty String မဖြစ်ရပါဘူး။emailဟာ Valid Email Format ဖြစ်ရပါမယ်။ageဟာ 18 နဲ့အထက် ဖြစ်ရပါမယ်။
Validation မအောင်မြင်ရင် Procedure က ဆက်လက်လုပ်ဆောင်မှာ မဟုတ်ဘဲ Validation Error ပြန်ပေးမှာ ဖြစ်ပါတယ်။
ဒီလို TypeScript ရဲ့ Compile-time Type Checking နဲ့ Zod ရဲ့ Runtime Validation ကို ပေါင်းစပ်အသုံးပြုခြင်းအားဖြင့် Development အဆင့်နဲ့ Production Runtime အဆင့် နှစ်ခုစလုံးမှာ Data Consistency ကို ပိုမိုကောင်းမွန်စွာ ထိန်းသိမ်းနိုင်ပါတယ်။
Automatic Request Batching and Data Loading
Frontend Application တွေမှာ Component တစ်ခုချင်းစီက လိုအပ်တဲ့ Data ကို သီးခြား Query လုပ်တဲ့အခါ Network Requests အများအပြား ထွက်လာနိုင်ပါတယ်။
ဥပမာ Dashboard တစ်ခုမှာ User Profile, User Settings နဲ့ Notification Count စတဲ့ Data သုံးခုကို Component သုံးခုက သီးခြားတောင်းဆိုတဲ့အခါ ပုံမှန် API Requests ဆိုရင် Network Requests ၃ ခု ထွက်လာနိုင်ပါတယ်။
tRPC မှာတော့ HTTP Request Batching ကို အသုံးပြုနိုင်ပါတယ်။ တူညီတဲ့ Batch Window အတွင်း ပို့လိုက်တဲ့ Compatible Queries တွေကို HTTP Request တစ်ခုတည်းအဖြစ် ပေါင်းစည်းပေးနိုင်ပါတယ်။
/api/trpc/getUser,getSettings,getNotifications?batch=1
ဒီလိုပေါင်းစည်းလိုက်တဲ့အတွက် HTTP Request အရေအတွက်ကို လျှော့ချနိုင်ပြီး Network Overhead ကို သက်သာစေပါတယ်။ Database Query များအတွက် Server ဘက်တွင် DataLoader Pattern ဖြင့် Batch Fetching ပြုလုပ်နိုင်ပါတယ်။
tRPC vs REST vs GraphQL
API Architecture ရွေးချယ်တဲ့အခါ tRPC, REST နဲ့ GraphQL တို့ရဲ့ ကွာခြားချက်ကိုလည်း နားလည်ထားဖို့ လိုပါတယ်။
| Dimension | tRPC | REST | GraphQL |
|---|---|---|---|
| Type Safety | Native TypeScript Inference | Manual Types / OpenAPI Codegen | Schema + Codegen |
| Client Support | TypeScript-focused | Any Language | Any Language |
| API Structure | Typed Procedures | Resource-based Endpoints | Schema-based Queries |
| Data Fetching | Procedure-based | Endpoint-based | Client-selected Fields |
| Learning Curve | Low for TypeScript Developers | Low | Moderate to High |
| Best Used In | Full-stack TS Applications | Public APIs, Multi-language Systems | Complex Data Graphs |
- tRPC: Frontend နဲ့ Backend နှစ်ခုစလုံး TypeScript အသုံးပြုထားတဲ့ Full-stack Projects တွေအတွက် အလွန်သင့်တော်ပါတယ်။
- REST: Mobile Apps, Third-party Integrations နဲ့ Programming Language မတူညီတဲ့ Client တွေကို API ထုတ်ပေးရတဲ့အခါ အသုံးဝင်ပါတယ်။
- GraphQL: Data Sources အများအပြားက Data တွေကို Client ရဲ့ လိုအပ်ချက်အလိုက် ရွေးချယ် Fetch လုပ်ဖို့ လိုအပ်တဲ့အခါ သင့်တော်ပါတယ်။
Practical Limitations to Keep in Mind
tRPC ဟာ Developer Experience နဲ့ Type Synchronization ပိုင်းမှာ အားသာချက်များစွာ ရှိပေမဲ့ အောက်ပါ အချက်တွေကို သတိထားဖို့ လိုပါတယ်။
- TypeScript Ecosystem Dependencies: Server နဲ့ Client နှစ်ခုစလုံး TypeScript အသုံးပြုထားမှသာ Native Type Inference အားသာချက်ကို ရရှိနိုင်ပါတယ်။
- Authentication & Authorization: tRPC က Type Safety ပေးနိုင်ပေမဲ့ User Permission စစ်ဆေးခြင်းကို အလိုအလျောက် မလုပ်ပေးပါဘူး။ Middleware များဖြင့် သီးခြားစီမံရပါတယ်။
- Database Performance: Query တစ်ခုကို Type-safe ရေးသားထားရုံဖြင့် Database Performance ကောင်းသည်ဟု မဆိုလိုပါဘူး။ Indexing, Query Optimization, N+1 Query Prevention များကို သီးခြားစီမံရပါတယ်။
- Public API Compatibility: Third-party Developers တွေကို API ထုတ်ပေးဖို့ လိုအပ်တဲ့အခါ REST သို့မဟုတ် GraphQL က ပိုမိုသင့်တော်ပါတယ်။
Final Thoughts
tRPC ရဲ့ အဓိကတန်ဖိုးက Frontend နဲ့ Backend ကြားမှာ Type Definitions တွေ ထပ်ခါတလဲလဲ ရေးနေရတဲ့ အလုပ်ကို လျှော့ချပေးပြီး API Contract Drift ကြောင့် ဖြစ်တတ်တဲ့ ပြဿနာတွေကို စောစီးစွာ ရှာဖွေနိုင်စေတာ ဖြစ်ပါတယ်။
Server ဘက်မှာ Router နဲ့ Procedures တွေကို တစ်နေရာတည်းမှာ သတ်မှတ်ထားပြီး Client ဘက်မှာ အဲဒီ Types တွေကို တိုက်ရိုက် Infer လုပ်အသုံးပြုနိုင်တာကြောင့် Development Experience ကို သိသိသာသာ ကောင်းမွန်စေပါတယ်။
Full-stack TypeScript Monorepo, Next.js Applications နဲ့ Web/Mobile Client တွေကို TypeScript ဖြင့် စီမံခန့်ခွဲတဲ့ Systems တွေအတွက်တော့ tRPC ဟာ Type Safety ကို အပြည့်အဝ အသုံးချနိုင်တဲ့ ခိုင်မာတဲ့ Architecture တစ်ခု ဖြစ်ပါတယ်။ နောက်ဆုံးမှာ API Architecture တစ်ခု ရွေးချယ်တဲ့အခါ Developer Experience တစ်ခုတည်းကို မကြည့်ဘဲ Client အမျိုးအစား၊ Integration Requirements နဲ့ Long-term Maintenance Cost တွေကိုပါ ထည့်သွင်းစဉ်းစားသင့်ပါတယ်။