GraphQL vs REST
Understand when GraphQL beats REST and why.
Why GraphQL?
You already know how to ship REST APIs in PHP. GraphQL is not a replacement for HTTP or a magic bullet — it is a query language and type system that lets the client describe exactly what it needs and gets back exactly that, in one round trip.
In this lesson we contrast the two honestly: where GraphQL genuinely wins, where REST is still the right call, and what GraphQL costs you operationally.
Over-fetching and Under-fetching
The classic REST pain points:
- Over-fetching:
GET /users/1returns 40 fields when the UI needs 3. - Under-fetching: to render a user's posts and each post's comment count you call
/users/1, then/users/1/posts, then N comment endpoints.
GraphQL collapses this into a single declarative request.
query {
user(id: 1) {
name
posts {
title
commentCount
}
}
}