GraphQL vs REST
Bukan "mana yang lebih baik" — keduanya punya trade-off. Pilih berdasarkan kebutuhan.
Perbandingan
| Aspek | REST | GraphQL |
|---|---|---|
| Endpoints | Banyak (/users, /posts) | Satu (/graphql) |
| Data fetching | Server decides shape | Client decides shape |
| Over-fetching | Sering terjadi | Tidak (query exact fields) |
| Under-fetching | Multiple requests | Nested query sekali jalan |
| Caching | HTTP caching built-in | Lebih kompleks (POST request) |
| Versioning | /api/v1, /api/v2 | Tidak perlu (evolve schema) |
| Learning curve | Rendah | Sedang |
| Tooling | Postman, curl | GraphiQL, Apollo Studio |
| File upload | Native (multipart) | Butuh spec tambahan |
| Error handling | HTTP status codes | Selalu 200, error di body |
Kapan Pilih GraphQL?
- Mobile app — bandwidth terbatas, butuh exact data
- Complex relasi — nested data (user → posts → comments → likes)
- Multiple clients — web, mobile, IoT butuh data berbeda dari sumber sama
- Rapid iteration — frontend bisa iterasi tanpa tunggu backend buat endpoint baru
Kapan Tetap REST?
- Simple CRUD — Overhead GraphQL tidak worth it
- File-heavy API — Upload/download file lebih natural di REST
- Caching critical — HTTP caching sudah mature untuk REST
- Public API — REST lebih mudah dipahami developer luar
- Tim kecil — Schema management overhead