Sistem Manajemen Toko Sembako - GraphQL API untuk pengelolaan produk, inventori, dan pesanan.
- Tentang Project
- Arsitektur
- Tech Stack
- Quick Start
- API Endpoints
- Schema GraphQL
- Integrasi dengan Consumer
- Deployment Railway
- Testing
Toko Sembako adalah sistem backend untuk manajemen toko sembako yang menyediakan:
- Product Service - Manajemen katalog produk
- Inventory Service - Manajemen stok barang
- Order Service - Pemrosesan pesanan dari consumer (restoran)
- Auth Service - Autentikasi pengguna
Project ini didesain sebagai Producer/Supplier API yang dapat dikonsumsi oleh sistem lain (seperti Anugerah Resto).
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β TOKO SEMBAKO API β
β (Single Server) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββ β
β β Product β β Inventory β β Order β β Auth β β
β β Service β β Service β β Service β β Service β β
β β β β β β β β β β
β β /graphql/ β β /graphql/ β β /graphql/ β β/graphql/β β
β β product β β inventory β β order β β auth β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β MySQL Database β
β (toko_sembako) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Teknologi | Kegunaan |
|---|---|
| Node.js | Runtime JavaScript |
| Apollo Server | GraphQL Server |
| Express | HTTP Framework |
| MySQL | Database |
| Docker | Containerization |
| Railway | Cloud Deployment |
- Node.js v16+
- MySQL 8.0+
- npm atau yarn
git clone https://github.com/your-username/toko-sembako.git
cd toko-sembakonpm installBuat database MySQL:
CREATE DATABASE toko_sembako;Buat file .env (opsional, ada default values):
PORT=8080
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_NAME=toko_sembakonode deploy.jsServer akan berjalan di http://localhost:8080
- Product: http://localhost:8080/graphql/product
- Inventory: http://localhost:8080/graphql/inventory
- Order: http://localhost:8080/graphql/order
- Auth: http://localhost:8080/graphql/auth
| Endpoint | Deskripsi | Port Default |
|---|---|---|
/graphql/product |
Manajemen Produk | 8080 |
/graphql/inventory |
Manajemen Stok | 8080 |
/graphql/order |
Manajemen Pesanan | 8080 |
/graphql/auth |
Autentikasi | 8080 |
type Product {
id: ID!
name: String!
price: Int!
unit: String!
}
type Query {
getProducts: [Product]
}
type Mutation {
createProduct(name: String!, price: Int!, unit: String!): Product
}Contoh Query:
# Ambil semua produk
query {
getProducts {
id
name
price
unit
}
}
# Tambah produk baru
mutation {
createProduct(name: "Beras Premium", price: 15000, unit: "kg") {
id
name
}
}type Inventory {
productId: ID!
stock: Int!
}
type Query {
getInventory(productId: ID!): Inventory
}
type Mutation {
increaseStock(productId: ID!, qty: Int!): Inventory
decreaseStock(productId: ID!, qty: Int!): Inventory
}Contoh Query:
# Cek stok produk
query {
getInventory(productId: "1") {
productId
stock
}
}
# Tambah stok
mutation {
increaseStock(productId: "1", qty: 100) {
productId
stock
}
}type OrderItem {
productId: ID!
qty: Int!
price: Int!
subtotal: Int!
}
type Order {
id: ID!
restaurantId: String!
items: [OrderItem!]!
total: Int!
status: String!
}
type Query {
getOrders: [Order]
getOrderById(orderId: ID!): Order
}
type Mutation {
createOrder(restaurantId: String!, items: [OrderItemInput!]!): Order
cancelOrder(orderId: ID!): Order
}
input OrderItemInput {
productId: ID!
qty: Int!
}Contoh Query:
# Buat pesanan dari restoran
mutation {
createOrder(
restaurantId: "anugerah-resto-001"
items: [
{ productId: "1", qty: 10 },
{ productId: "2", qty: 5 }
]
) {
id
total
status
items {
productId
qty
price
subtotal
}
}
}
# Batalkan pesanan
mutation {
cancelOrder(orderId: "1") {
id
status
}
}type User {
id: ID!
email: String!
}
type AuthPayload {
token: String!
user: User!
}
type Mutation {
register(email: String!, password: String!): AuthPayload
login(email: String!, password: String!): AuthPayload
}Toko Sembako dapat dikonsumsi oleh sistem lain (seperti Anugerah Resto) melalui GraphQL API.
βββββββββββββββββββ βββββββββββββββββββ
β Anugerah Resto β β Toko Sembako β
β (Consumer) β β (Producer) β
βββββββββββββββββββ€ βββββββββββββββββββ€
β β 1. GET Products β β
β β ββββββββββββββββββΊ β β
β β β β
β β 2. Check Stock β β
β β ββββββββββββββββββΊ β β
β β β β
β β 3. Create Order β β
β β ββββββββββββββββββΊ β β
β β β (stok berkurang)β
β β ββββββββββββββββββ β β
β (stok masuk) β 4. Order Response β β
βββββββββββββββββββ βββββββββββββββββββ
const axios = require('axios');
const TOKO_SEMBAKO_URL = 'https://your-railway-url.up.railway.app';
// Ambil produk
async function getProducts() {
const response = await axios.post(`${TOKO_SEMBAKO_URL}/graphql/product`, {
query: `{ getProducts { id name price unit } }`
});
return response.data.data.getProducts;
}
// Buat pesanan
async function createOrder(restaurantId, items) {
const response = await axios.post(`${TOKO_SEMBAKO_URL}/graphql/order`, {
query: `
mutation CreateOrder($restaurantId: String!, $items: [OrderItemInput!]!) {
createOrder(restaurantId: $restaurantId, items: $items) {
id
total
status
}
}
`,
variables: { restaurantId, items }
});
return response.data.data.createOrder;
}- Login ke Railway
- New Project β Deploy from GitHub repo
- Pilih repository Toko Sembako
- Root Directory:
/(jika repo khusus Toko Sembako) - Root Directory:
/toko-sembako-revisi(jika bagian dari monorepo)
Railway akan otomatis set:
PORT(Railway assign)- Tambahkan MySQL credentials jika pakai external DB
- Klik "New" β "Database" β "MySQL"
- Railway akan otomatis set environment variables:
MYSQL_HOSTMYSQL_PORTMYSQL_USERMYSQL_PASSWORDMYSQL_DATABASE
Railway akan otomatis deploy saat push ke GitHub.
URL Production:
https://your-app.up.railway.app/graphql/product
https://your-app.up.railway.app/graphql/inventory
https://your-app.up.railway.app/graphql/order
https://your-app.up.railway.app/graphql/auth
- Buka endpoint di browser
- Gunakan panel kiri untuk menulis query
- Klik tombol Play
βΆοΈ
# 1. Tambah Produk
mutation {
createProduct(name: "Gula Pasir", price: 14000, unit: "kg") {
id
name
}
}
# 2. Cek Stok
query {
getInventory(productId: "1") {
stock
}
}
# 3. Tambah Stok
mutation {
increaseStock(productId: "1", qty: 100) {
stock
}
}
# 4. Buat Pesanan
mutation {
createOrder(
restaurantId: "anugerah-resto"
items: [{ productId: "1", qty: 10 }]
) {
id
total
status
}
}
# 5. Cek Pesanan
query {
getOrders {
id
restaurantId
total
status
}
}toko-sembako-revisi/
βββ deploy.js # Entry point (menggabungkan semua service)
βββ package.json # Dependencies
βββ docker-compose.yml # Docker configuration
β
βββ product-service/ # Product Service
β βββ index.js
β βββ schema_product.graphql
β
βββ inventory-service/ # Inventory Service
β βββ index.js
β βββ schema_inventory.graphql
β
βββ order-service/ # Order Service
β βββ index.js
β βββ schema_order.graphql
β
βββ auth-service/ # Auth Service
β βββ index.js
β βββ schema.graphql
β
βββ db/ # Database
βββ init.js # Database initialization
βββ seed.js # Seed data
| Nama | Role |
|---|---|
| [Nama 1] | Backend Developer |
| [Nama 2] | Backend Developer |
| [Nama 3] | Database Administrator |
MIT License - Silakan gunakan untuk keperluan akademis.
- Fork repository
- Buat branch baru (
git checkout -b feature/fitur-baru) - Commit perubahan (
git commit -m 'Tambah fitur baru') - Push ke branch (
git push origin feature/fitur-baru) - Buat Pull Request
Dibuat dengan β€οΈ untuk Tugas Besar IAE