What is the difference between REST and GraphQL in terms of over-fetching and under-fetching?
Answer
Over-fetching means receiving more data than needed. In REST, a GET /users/1 endpoint returns the full user object with all fields, even if the client only needs the name and avatar. With hundreds of fields, this wastes bandwidth. GraphQL eliminates over-fetching by letting clients specify exactly which fields to return: { user(id: "1") { name avatar } }. Under-fetching means not receiving enough data in one request, requiring additional requests. In REST, fetching a user's profile, their posts, and each post's comments might require: GET /users/1, then GET /users/1/posts, then GET /posts/1/comments, etc. — multiple round trips. GraphQL fetches all of this in a single request: { user(id: "1") { name posts { title comments { text } } } }. This is particularly valuable for mobile clients where network round trips are expensive.