Frontend System Design · Lesson 49 of 86 · 4 min
REST and GraphQL for Frontend Engineers
This is not about memorizing definitions; it's about articulating the trade-offs when an interviewer asks, "Which would you use here, and why?"
How REST Structures Data Access
REST (Representational State Transfer) isn't a protocol; it's a set of conventions for building APIs. Think of it as an agreed-upon way to model your backend as a collection of resources, accessed via URLs.
For you as a frontend developer, this means predictable patterns. You fetch a list of users from /users. You fetch a specific user from /users/123. You create a user by sending a POST request to /users. You use standard HTTP verbs (GET, POST, PUT, DELETE) to signal your intent.
The key benefit is simplicity and leveraging the web's existing infrastructure. HTTP caching works out of the box with GET requests, which can dramatically improve performance without any client-side logic.
// A typical RESTful GET request
async function fetchUsers(): Promise<User[]> {
const response = await fetch('https://api.example.com/v1/users');
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const users = await response.json();
return users;
}
The trap: Candidates often just say REST uses HTTP methods. Go deeper. Mention that REST's stateless nature and use of GET for reads makes it perfectly suited for caching at multiple layers (browser, CDN, proxy), which is a significant performance win.
The Problem GraphQL Solves
The rigidity of REST endpoints creates two common problems: over-fetching and under-fetching.
Over-fetching is when you get more data than you need. The /users/123 endpoint might return a 50-field user object, but your UI only needs the user's name and avatar. The other 48 fields are wasted bandwidth.
Under-fetching is when you don't get enough data, forcing you to make more requests. To build a user's profile page, you might need to call GET /users/123, then GET /users/123/posts, and then GET /users/123/followers. This "chatty" interaction adds latency.
These problems are especially painful on mobile clients with limited network bandwidth and higher latency.
How GraphQL Gives the Frontend Control
GraphQL is a query language for your API. Instead of hitting multiple, fixed endpoints, you send a single query to a single endpoint (usually /graphql) that describes exactly the data you need. The server responds with a JSON object that mirrors the shape of your query.
This gives the frontend control. If you need the user's name and the titles of their last three posts, you ask for just that.
query GetUserWithPosts($id: ID!) {
user(id: $id) {
id
name
posts(last: 3) {
id
title
}
}
}
Under the hood, this is still just an HTTP request. You send a POST request with the query and any variables in the body.
// A typical GraphQL request using fetch
async function fetchUserWithPosts(userId: string) {
const response = await fetch('https://api.example.com/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
query GetUserWithPosts($id: ID!) {
user(id: $id) {
id
name
posts(last: 3) {
id
title
}
}
}
`,
variables: { id: userId },
}),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const { data } = await response.json();
return data.user;
}
The GraphQL server parses the query and fetches the data from various sources—databases, microservices, other APIs—before assembling the final response.
Articulating the Trade-offs
This is the core of the interview question. Your ability to discuss trade-offs shows seniority.
In an interview, show you can weigh these factors. Start your answer with "It depends on the project's needs..." then explain what those needs might be. For a simple CRUD app with a well-defined resource model, REST is often the right call due to its simplicity. For a complex dashboard aggregating data from three microservices, GraphQL is a strong contender.
Choose REST when:
- You have a simple, resource-centric API (e.g., a basic blog).
- You want to heavily leverage HTTP caching.
- The project is small, and the overhead of setting up a GraphQL server is not justified.
- You are building a public API and want the lowest possible barrier to entry for consumers.
Choose GraphQL when:
- You have complex UIs with varied and nested data requirements.
- You need to minimize network requests (e.g., mobile apps).
- You are building a frontend on top of multiple existing services (an "API gateway").
- The frontend team is evolving rapidly and wants to avoid waiting for backend changes to add or remove fields.
Be aware of GraphQL's challenges:
- Caching is harder. Since most queries are
POSTrequests to a single endpoint, you can't rely on standard HTTP caching. Caching logic moves to the client (e.g., Apollo Client, urql). - Performance pitfalls. A poorly-implemented GraphQL server can suffer from the "N+1 problem," where a query for a list of N items results in N+1 database queries. This is solved on the backend with tools like
dataloader, but you should know the problem exists. - Complexity. It introduces a new language, a type system (the schema), and more complex server-side logic (resolvers). This can be overkill for simple projects.
Try it
Take 15 minutes. For a component that displays a blog post with its author's name and a list of comments, write down: 1. The sequence of REST API calls you might need. 2. The single GraphQL query that would get the same data.
Next: Batching, Throttling and Debouncing API Calls.
Lock it in — apply this lesson under interview conditions.