tmg-gw
v0.0.7
Published
`tmg-gw` คือ CLI สำหรับ generate โปรเจกต์ NestJS GraphQL Gateway ที่เชื่อมต่อ backend ผ่าน gRPC
Downloads
69
Readme
tmg-gw
tmg-gw คือ CLI สำหรับ generate โปรเจกต์ NestJS GraphQL Gateway ที่เชื่อมต่อ backend ผ่าน gRPC
สิ่งที่ generate ให้
- โครงสร้างโปรเจกต์ NestJS แบบพร้อมรัน
- GraphQL endpoint ที่
/graphql - ไฟล์
.env.example - ค่าเชื่อมต่อ gRPC เริ่มต้นสำหรับ
TMG_B_GRPC_URLและMY_SERVICE_GRPC_URL
วิธีใช้งาน CLI
ติดตั้ง dependencies และ build ตัว CLI
npm i -g tmg-gwoptions ที่ใช้ได้
--outDir <path>ตำแหน่งโฟลเดอร์ปลายทาง (default: current directory)--overwriteเขียนทับโฟลเดอร์เดิมถ้ามีอยู่แล้ว-s, --service <serviceName...>ระบุรายชื่อ service ที่ต้องการ integrate (ใส่ได้หลายตัว)-sdir, --sdir, --servicesDir <path>ระบุโฟลเดอร์ services (default:services)
ตัวอย่าง:
tmg-gw g my-gateway --outDir ./apps --overwriteตัวอย่าง integrate หลาย service:
tmg-gw g my-gateway \
-s my-service users-service wallet-service \
-sdir services \
--outDir gateways \
--overwriteขั้นตอนหลัง generate
cd my-gateway
npm install
cp .env.example .env
npm run start:devเมื่อรันสำเร็จ จะเห็น log:
Gateway is running at http://localhost:<PORT>GraphQL endpoint available at http://localhost:<PORT>/graphql
ค่าใน .env
PORTพอร์ตของ gateway (default:3000)TMG_B_GRPC_URLปลายทาง gRPC service หลัก (default:localhost:50051)MY_SERVICE_GRPC_URLปลายทาง gRPC อีก service (default:localhost:50051)
Service Integration (-s, -sdir)
เมื่อระบุ -s ตอน generate ระบบจะ:
- เพิ่ม env ต่อ service ใน
.env.exampleเช่นUSERS_SERVICE_GRPC_URL=localhost:50051 - ค้นหาไฟล์ proto จาก
<servicesDir>/<service>/proto/*.proto - คัดลอกไฟล์
.protoไปไว้ใน<gateway>/proto
ตัวอย่างโครงสร้าง:
projectA/
gateways/
services/
my-service/
users-service/
wallet-service/หมายเหตุ project name
ชื่อโปรเจกต์ต้องเป็นตัวพิมพ์เล็กเท่านั้น และใช้ได้เฉพาะ:
- a-z
- 0-9
-
