REST i GraphQL su dva najpopularnija načina dizajna API-ja u 2026. Svaki ima svoje mjesto — ali kriva odluka znači mjesece refaktoringa. Ovo je vodič kada koji koristit, što su pitfalls, i kako oba implementirati pravilno u PHP svijetu (WordPress, Craft, Laravel).
Što je REST
Representational State Transfer — arhitektura iz 2000. Svaki resurs ima URL, HTTP metode definiraju akciju:
GET /api/posts— lista postova.GET /api/posts/123— jedan post.POST /api/posts— kreiraj.PUT /api/posts/123— update.DELETE /api/posts/123— briši.
Server odlučuje shape response-a — uvijek isti za isti endpoint.
Što je GraphQL
Query language for APIs, Facebook 2015. Jedan endpoint (/graphql), klijent definira točno koje fieldove hoće:
query {
posts(limit: 10) {
id
title
author { name }
comments(limit: 3) { body }
}
}
Server vrati točno to. Bez fieldova koji se ne traže, bez extra queryja.
Glavna razlika
- REST: server-driven shape. Klijent uzima što server daje. Vise endpointa, jednostavne URL-e.
- GraphQL: client-driven shape. Klijent definira što hoće. Jedan endpoint, kompleksniji upiti.
Kada koristit REST
- Public API za third-party developere. REST je univerzalno poznat, lakše dokumentirat, svaki SDK podržava.
- CRUD-heavy app. Standardne operacije se mappiraju jedan-na-jedan.
- HTTP caching potreban. REST se cache-a kroz standardne HTTP headers (ETag, Cache-Control). GraphQL teško (sve je POST).
- Webhooks i server-to-server. Lakše definirat fixed contract.
- Statelessness striktno. REST je u srcu stateless.
Kada koristit GraphQL
- Frontend potrebuje različite podatke za različite ekrane. Mobile app traži manje fieldova nego web — GraphQL daje točno što treba.
- Multiple klijenti različitog tipa. Mobile, web, partner integracije, sve idu kroz jedan API.
- Reduce network roundtrips. Umjesto 5 REST poziva, jedan GraphQL query s nested resources.
- Rapidly evolving frontend. Backend ne mora mijenjat endpoint kad frontend hoće dodatan field.
- Type-safe contract. GraphQL schema je strict types, lakši dev experience s code-gen tools.
WordPress REST API
WP ima built-in REST API od 2016 (/wp-json/wp/v2/). Sve core resources izložene:
GET /wp-json/wp/v2/posts?per_page=10
GET /wp-json/wp/v2/posts/123
GET /wp-json/wp/v2/users
GET /wp-json/wp/v2/categories
Auth: Application Passwords (per-user, generated u Profile), JWT (plugin), OAuth (plugin).
Custom endpoints: register_rest_route() u plugin-u/tema-u.
WordPress GraphQL — WPGraphQL plugin
WPGraphQL plugin doda /graphql endpoint, exposes sve WP content kao GraphQL types.
Plus: izvrsno za headless WP (Next.js, Astro, Gatsby frontends).
Setup: instaliraj plugin, opcionalno WPGraphQL JWT Authentication za auth.
Craft CMS — GraphQL native
Craft od verzije 3.3 ima GraphQL built-in (uz REST kao alternativu kroz Element API plugin). To je glavna razlika u odnosu na WP — Craft je headless-first.
POST /api
{
"query": "{ entries(section: \"blog\", limit: 5) { title, slug, postDate } }"
}
Schema se auto-generira iz section/field definicija. Tokeni za auth (kreiraš u Craft CP). Public read-only schema bez tokena ako želiš.
Laravel — oba podržana
REST u Laravel
Built-in. Route::apiResource('posts', PostController::class) kreira sve standardne RESTful routes. API Resources za JSON transformaciju.
// Controller
public function index() {
return PostResource::collection(Post::paginate(10));
}
// Resource
return [
'id' => $this->id,
'title' => $this->title,
'author' => new UserResource($this->author),
];
GraphQL u Laravel
Najpopularniji paket: Lighthouse (od Nuwave). Schema-first pristup, definitiraš types u SDL fajlovima.
composer require nuwave/lighthouse
Alternative: rebing/graphql-laravel (kod-first).
N+1 problem — GraphQL killer
Najveća zamka u GraphQL-u. Query:
{ posts { author { name } } }
Naive implementation: 1 query za posts (vrati 10) + 10 queries za autore = 11 queries. Skala loše.
Rješenje: DataLoader pattern.
- Lighthouse:
@hasOne,@belongsTodirectives + batch loading built-in. - Craft: eager loading kroz
.with(['author']). - WPGraphQL:
WPGraphQL\Data\Connection\AbstractConnectionResolverhandle batch.
Bez tog, GraphQL je sporiji od REST-a.
Caching
REST caching
Standardno HTTP — Cache-Control: max-age=3600, ETag, conditional GET (If-None-Match). CDN handle ovo automatski.
GraphQL caching — komplicirano
Svaki query je POST request — CDN ga ne cache-a. Rješenja:
- Persisted queries — hash query string, server čuva mapping, klijent šalje samo hash. Cacheable na CDN.
- Apollo Server cache — per-field caching s direktive.
- Server-side response cache (Redis) — hash query+vars, cache response.
- Relay GET requesti — neki GraphQL serveri podržavaju GET za queries (Apollo, Hot Chocolate).
Security — REST
- Auth: API tokens, OAuth 2.0, JWT.
- Rate limiting po endpoint i metodi.
- CORS pravilno postavljen.
- Input validation (Laravel Form Requests, WP nonces).
Security — GraphQL
GraphQL ima dodatne attack vectors:
- Query complexity DoS — klijent može poslat deeply nested query ("daj mi sve posts s autorima s njihovim posts s autorima..."). Postavi max depth (5-10) i max complexity score.
- Introspection u produkciji — schema visible svima. Disable u prod ili allow samo autenticiranim.
- Field-level auth — neki fieldovi traže permission. Lighthouse
@candirective, custom resolvers. - Rate limiting po complexity — ne po endpoint, nego po cost svake query.
Versioning
- REST: URL-based (
/v1/posts,/v2/posts) ili header-based. - GraphQL: izbjegava versioning. Dodavanjem field-a ne breaka client. Brisanje koristi
@deprecateddirective, klijenti vide warning.
Dokumentacija
- REST: OpenAPI/Swagger spec. Generira interactive docs (Swagger UI, Redoc).
- GraphQL: schema je self-documenting. Alati: GraphiQL (interactive browser), Apollo Studio.
Performance benchmarks
Ne postoji "GraphQL je brži" ili "REST je brži". Ovisi o use case:
- Jednostavan single-resource fetch: REST + HTTP cache pobjeđuje.
- Komplicirana stranica sa 5 nezavisnih resursa: GraphQL jedan query vs 5 REST poziva.
- Mobile app s loš mrežom: GraphQL štedi roundtrips i bandwidth.
- Server-to-server bulk: REST s pagination je predvidljiviji.
Hybrid pristup
Mnogi modern stackovi koriste oba:
- REST za public API (third-party developers).
- GraphQL za internal frontend (mobile + web app klijent).
- REST za webhook receivers.
- REST za file uploads (multipart easier u REST-u).
WMD i API setup
Sav WMD hosting podržava i REST i GraphQL workloadove. Za headless WP klijente postavljamo WPGraphQL + caching layer + persisted queries. Za Craft klijente konfiguriramo GraphQL token authentication + public schema. Za Laravel klijente postavljamo Lighthouse ili API Resources ovisno o potrebama. Rate limiting (po IP-u i po complexity-u), CORS, JWT auth, query depth limits — sve uključeno u Maintenance Plan. Performance audit i N+1 review za postojeće API-je.
FAQ
Moram li birat samo jedno? Ne. Mnoge produkcijske aplikacije imaju i REST i GraphQL endpointe.
GraphQL je sigurniji od REST-a? Drugačiji attack surface. Ne sigurniji per se. Treba ga pravilno konfigurirati.
WPGraphQL ili WP REST API za headless? WPGraphQL — moderniji, bolji DX, manje overhead-a. WP REST je dobar fallback.
Mogu li imati GraphQL ispred REST API-ja? Da, pattern "BFF (Backend for Frontend)" — GraphQL gateway koji proxy-a REST microservices. Skuplji setup, koristan za microservices.
Cijena setup vremena? REST: minute. GraphQL: dani-tjedni za pravilan schema design. Za prototype REST je brži start.
Trebaš API setup ili migraciju? WMD postavlja REST/GraphQL za WP, Craft, Laravel s pravilnim auth, caching, rate limiting. Javi se preko kontakt forme.