Discover how one simple change can make your app faster and your code cleaner!
REST vs GraphQL awareness in Express - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a website where users want to see their profile, posts, and comments. You write separate requests for each piece of data, and every time the user clicks, your app asks the server multiple times.
Making many separate requests slows down the app and wastes data. Sometimes you get too much data you don't need, or not enough, so you have to ask again. Managing all these requests by hand is confusing and error-prone.
REST organizes data into fixed endpoints, but GraphQL lets you ask for exactly what you want in one request. This means faster loading, less wasted data, and simpler code to manage your data needs.
GET /users/123/profile GET /users/123/posts GET /users/123/comments
POST /graphql
{
user(id: "123") {
profile {
name
age
}
posts {
title
}
comments {
text
}
}
}It enables building faster, smarter apps that get just the data they need with fewer requests.
Think of ordering food: REST is like ordering each dish separately, while GraphQL is like telling the chef your whole meal in one order, so everything arrives together and fresh.
REST uses fixed URLs for each data type, which can cause many requests.
GraphQL lets you request exactly what you want in one go.
This makes apps faster, simpler, and more efficient.
Practice
Solution
Step 1: Understand REST API structure
REST APIs use different URLs (endpoints) for different resources and HTTP methods like GET, POST, PUT, DELETE to perform actions.Step 2: Understand GraphQL API structure
GraphQL uses a single endpoint URL and clients specify exactly what data they want in the query, making it flexible.Final Answer:
REST uses multiple URLs and HTTP methods; GraphQL uses a single URL with flexible queries. -> Option AQuick Check:
REST vs GraphQL = multiple URLs vs single URL [OK]
- Thinking GraphQL uses multiple URLs like REST
- Confusing HTTP methods usage between REST and GraphQL
- Believing REST and GraphQL are the same
Solution
Step 1: Identify correct HTTP method for fetching data
GET method is used to retrieve data in REST APIs.Step 2: Check URL pattern for resource identification
/user/:id correctly uses a URL parameter to specify which user to fetch.Final Answer:
app.get('/user/:id', (req, res) => { /* fetch user */ }); -> Option CQuick Check:
GET + /user/:id = fetch user [OK]
- Using POST instead of GET for fetching data
- Using GraphQL endpoint for REST question
- Using PUT method which is for updates
{ user(id: "1") { name } }?
const { graphqlHTTP } = require('express-graphql');
const schema = buildSchema(`
type Query {
user(id: ID!): User
}
type User {
id: ID
name: String
email: String
}
`);
const root = {
user: ({ id }) => ({ id, name: 'Alice', email: 'alice@example.com' })
};
app.use('/graphql', graphqlHTTP({ schema, rootValue: root, graphiql: true }));Solution
Step 1: Understand the GraphQL query
The query requests only thenamefield of the user with id "1".Step 2: Check resolver returns full user object
The resolver returns id, name, and email, but GraphQL returns only requested fields.Final Answer:
{"data":{"user":{"name":"Alice"}}} -> Option BQuick Check:
GraphQL returns only requested fields [OK]
- Expecting all fields returned regardless of query
- Confusing error responses with valid data
- Assuming REST style full object return
app.get('/users/:id', (req, res) => {
const user = users.find(u => u.id === req.params.id);
if (user) {
res.json(user);
}
res.end();
});Solution
Step 1: Analyze response flow
If user is found, res.json(user) sends response, but code continues to res.end() which sends empty response again.Step 2: Understand Express response behavior
Calling res.end() after res.json() can cause the response to be overwritten or cause errors.Final Answer:
res.end() is called even after sending a response, causing empty output. -> Option DQuick Check:
Only send one response per request [OK]
- Using wrong HTTP method for fetching
- Assuming path name causes empty response
- Ignoring that res.end() can overwrite response
Solution
Step 1: Identify requirement for flexible fields and fewer endpoints
Clients want to specify fields and avoid many URLs.Step 2: Match approach to requirement
GraphQL uses one endpoint and allows clients to request exactly needed fields, fitting the requirement.Final Answer:
Use GraphQL with a single endpoint letting clients specify exactly which fields they want. -> Option AQuick Check:
Flexible fields + single endpoint = GraphQL [OK]
- Choosing REST and returning all fields wastes bandwidth
- Using GraphQL with multiple endpoints defeats its purpose
- Confusing REST flexibility with GraphQL's query power
