Bird
Raised Fist0
Expressframework~10 mins

REST vs GraphQL awareness in Express - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - REST vs GraphQL awareness
Client sends request
REST API Endpoint
Server processes
Send fixed data
Client receives
Shows how client requests go to REST or GraphQL endpoints, and how servers respond with fixed or flexible data.
Execution Sample
Express
const express = require('express');
const app = express();

// REST endpoint
app.get('/users', (req, res) => {
  res.json([{ id: 1, name: 'Alice' }]);
});
A simple REST endpoint that returns a fixed list of users.
Execution Table
StepRequest TypeEndpointServer ActionResponse Sent
1GET/usersReturn full user list[{ id: 1, name: 'Alice' }]
2POST/graphqlParse query, resolve fields{ data: { user: { id: 1, name: 'Alice' } } }
3GET/users?id=1Return full user list (no filtering)[{ id: 1, name: 'Alice' }]
4POST/graphqlParse query, resolve only requested fields{ data: { user: { name: 'Alice' } } }
5N/AN/ANo more requestsEnd of trace
💡 No more requests to process, execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
requestTypeN/AGETPOSTPOSTN/A
endpointN/A/users/graphql/graphqlN/A
responseDataN/A[{ id: 1, name: 'Alice' }]{ data: { user: { id: 1, name: 'Alice' } } }{ data: { user: { name: 'Alice' } } }N/A
Key Moments - 2 Insights
Why does the REST endpoint always return the full user data even if I want only the name?
REST endpoints return fixed data shapes defined by the server, so filtering fields requires extra endpoints or query parameters. See execution_table step 1 and 3 where full user data is returned.
How does GraphQL return only the requested fields?
GraphQL parses the query to know exactly which fields the client wants, then resolves and sends only those fields. See execution_table steps 2 and 4 where different fields are returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response does the REST endpoint send at step 3?
A{ data: { user: { name: 'Alice' } } }
B{ data: { user: { id: 1, name: 'Alice' } } }
C[{ id: 1, name: 'Alice' }]
DEmpty response
💡 Hint
Check the 'Response Sent' column for step 3 in the execution_table.
At which step does GraphQL return only the user's name?
AStep 4
BStep 1
CStep 2
DStep 3
💡 Hint
Look at the 'Response Sent' column for GraphQL responses in execution_table steps 2 and 4.
If the client wants only user IDs, how would the REST response change compared to GraphQL?
AREST would send only IDs by default
BREST would still send full user data unless a new endpoint is made
CGraphQL cannot filter fields
DBoth REST and GraphQL send full data always
💡 Hint
Refer to key_moments about REST fixed data shapes and GraphQL flexible queries.
Concept Snapshot
REST vs GraphQL quick view:
- REST: Multiple endpoints, fixed data per endpoint
- GraphQL: Single endpoint, flexible queries
- REST returns full data; GraphQL returns requested fields
- REST needs new endpoints for new data shapes
- GraphQL queries specify exactly what data is needed
Full Transcript
This visual trace compares REST and GraphQL in an Express server. The client sends requests to either REST endpoints or a GraphQL endpoint. REST endpoints return fixed data shapes, so even if the client wants only part of the data, the full data is sent. GraphQL parses the query to return only requested fields, making data transfer efficient. The execution table shows requests and responses step-by-step, highlighting how REST always sends full user data while GraphQL can send partial data. Variable tracking shows request types, endpoints, and response data changing over time. Key moments clarify why REST returns fixed data and how GraphQL filters fields. The quiz tests understanding of these differences using the execution visuals.

Practice

(1/5)
1. Which statement best describes the main difference between REST and GraphQL in an Express API?
easy
A. REST uses multiple URLs and HTTP methods; GraphQL uses a single URL with flexible queries.
B. REST uses a single URL and flexible queries; GraphQL uses multiple URLs and HTTP methods.
C. REST and GraphQL both use multiple URLs but differ in data format.
D. REST and GraphQL are identical in how they handle data fetching.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    REST uses multiple URLs and HTTP methods; GraphQL uses a single URL with flexible queries. -> Option A
  4. Quick Check:

    REST vs GraphQL = multiple URLs vs single URL [OK]
Hint: REST = many URLs; GraphQL = one URL with queries [OK]
Common Mistakes:
  • Thinking GraphQL uses multiple URLs like REST
  • Confusing HTTP methods usage between REST and GraphQL
  • Believing REST and GraphQL are the same
2. Which Express route setup correctly represents a REST API endpoint for getting a user by ID?
easy
A. app.put('/user', (req, res) => { /* fetch user */ });
B. app.post('/user/:id', (req, res) => { /* fetch user */ });
C. app.get('/user/:id', (req, res) => { /* fetch user */ });
D. app.get('/graphql', (req, res) => { /* fetch user */ });

Solution

  1. Step 1: Identify correct HTTP method for fetching data

    GET method is used to retrieve data in REST APIs.
  2. Step 2: Check URL pattern for resource identification

    /user/:id correctly uses a URL parameter to specify which user to fetch.
  3. Final Answer:

    app.get('/user/:id', (req, res) => { /* fetch user */ }); -> Option C
  4. Quick Check:

    GET + /user/:id = fetch user [OK]
Hint: GET method + URL with :id fetches resource [OK]
Common Mistakes:
  • Using POST instead of GET for fetching data
  • Using GraphQL endpoint for REST question
  • Using PUT method which is for updates
3. Given this Express GraphQL setup, what will the client receive when querying for { 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 }));
medium
A. {"data":{"user":{"id":"1","name":"Alice","email":"alice@example.com"}}}
B. {"data":{"user":{"name":"Alice"}}}
C. {"error":"Field 'user' not found"}
D. {"data":{"user":null}}

Solution

  1. Step 1: Understand the GraphQL query

    The query requests only the name field of the user with id "1".
  2. Step 2: Check resolver returns full user object

    The resolver returns id, name, and email, but GraphQL returns only requested fields.
  3. Final Answer:

    {"data":{"user":{"name":"Alice"}}} -> Option B
  4. Quick Check:

    GraphQL returns only requested fields [OK]
Hint: GraphQL returns only requested fields, not full object [OK]
Common Mistakes:
  • Expecting all fields returned regardless of query
  • Confusing error responses with valid data
  • Assuming REST style full object return
4. Identify the error in this Express REST route that causes it to always return an empty response:
app.get('/users/:id', (req, res) => {
  const user = users.find(u => u.id === req.params.id);
  if (user) {
    res.json(user);
  }
  res.end();
});
medium
A. The route path should be '/user/:id' not '/users/:id'.
B. The route should use POST instead of GET.
C. The user lookup uses incorrect comparison operator.
D. res.end() is called even after sending a response, causing empty output.

Solution

  1. 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.
  2. Step 2: Understand Express response behavior

    Calling res.end() after res.json() can cause the response to be overwritten or cause errors.
  3. Final Answer:

    res.end() is called even after sending a response, causing empty output. -> Option D
  4. Quick Check:

    Only send one response per request [OK]
Hint: Send only one response; avoid res.end() after res.json() [OK]
Common Mistakes:
  • Using wrong HTTP method for fetching
  • Assuming path name causes empty response
  • Ignoring that res.end() can overwrite response
5. You want to build an Express API that allows clients to fetch user data with flexible fields and avoid multiple endpoints. Which approach is best and why?
hard
A. Use GraphQL with a single endpoint letting clients specify exactly which fields they want.
B. Use REST but return all fields for every resource to avoid multiple endpoints.
C. Use REST with multiple endpoints for each resource and HTTP methods for actions.
D. Use GraphQL but create multiple endpoints for each query type.

Solution

  1. Step 1: Identify requirement for flexible fields and fewer endpoints

    Clients want to specify fields and avoid many URLs.
  2. Step 2: Match approach to requirement

    GraphQL uses one endpoint and allows clients to request exactly needed fields, fitting the requirement.
  3. Final Answer:

    Use GraphQL with a single endpoint letting clients specify exactly which fields they want. -> Option A
  4. Quick Check:

    Flexible fields + single endpoint = GraphQL [OK]
Hint: Flexible data + one URL = GraphQL [OK]
Common Mistakes:
  • Choosing REST and returning all fields wastes bandwidth
  • Using GraphQL with multiple endpoints defeats its purpose
  • Confusing REST flexibility with GraphQL's query power