A GraphQL API built with Rust using Juniper, Diesel, and Actix-web. Supports full CRUD operations on accounts with optional introspection control.
| Library | Role |
|---|---|
| Juniper | GraphQL schema & resolvers |
| Diesel | ORM & migrations (PostgreSQL) |
| Actix-web | Async HTTP server |
| r2d2 | Connection pooling |
- Rust (stable)
- PostgreSQL
- Diesel CLI:
cargo install diesel_cli --no-default-features --features postgres
1. Copy environment file
cp .env.example .envEdit .env sesuai konfigurasi lokal:
HOST=127.0.0.1
PORT=8080
DATABASE_URL=postgres://username:password@localhost/graphql2. Setup database & jalankan migrations
make db-setup
make db-migrate3. Jalankan server
make runServer berjalan di http://127.0.0.1:8080.
GraphQL Playground tersedia di http://127.0.0.1:8080/playground.
make build # Build debug binary
make build-release # Build optimized release binary
make run # Run in debug mode
make run-release # Run in release mode
make test # Run unit tests
make test-verbose # Run tests with stdout output
make lint # Clippy dengan strict warnings
make fmt # Auto-format source code
make db-setup # Inisialisasi database
make db-migrate # Jalankan pending migrations
make db-rollback # Revert migrasi terakhir
make db-reset # Drop & ulang semua migrasi
make clean # Hapus build artifacts
make help # Tampilkan semua commandsGet account by ID
query {
getById(id: 1) {
id
nickname
fullname
email
phoneNum
joinedAt
}
}List accounts (dengan pagination)
query {
accounts(skip: 0, limit: 10) {
id
nickname
email
}
}Create account
mutation {
createAccount(data: {
nickname: "zmab"
fullname: "Andrie Bam"
email: "andrie@example.com"
phoneNum: "081234567890"
})
}Update account (semua field opsional)
mutation {
updateAccount(id: 1, data: {
fullname: "New Name"
email: "new@example.com"
})
}Delete account
mutation {
deleteAccount(id: 1)
}| Variable | Keterangan | Contoh |
|---|---|---|
HOST |
Bind address server | 127.0.0.1 |
PORT |
Port server | 8080 |
DATABASE_URL |
PostgreSQL connection string | postgres://user:pass@localhost/graphql |
DISABLE_INTROSPECTION |
Nonaktifkan introspection query (true/false) |
false |
src/
├── main.rs # Entry point, server setup
├── lib.rs # Modul declarations
├── context.rs # Juniper context (db pool)
├── db.rs # Connection pool setup
├── schema.rs # Diesel schema (auto-generated)
├── schema_graphql.rs # GraphQL QueryRoot & MutationRoot
├── macros.rs # Helper macro to_graph_models!
├── handlers/
│ ├── accounts.rs # AccountHandler GraphQL object & input types
│ └── graphql.rs # HTTP handler & introspection guard
└── models/
└── accounts.rs # Account model & DB query functions
migrations/
└── ... # Diesel migration files