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
Recall & Review
beginner
What is REST in web development?
REST (Representational State Transfer) is a way to design web APIs using standard HTTP methods like GET, POST, PUT, and DELETE to manage resources identified by URLs.
Click to reveal answer
beginner
What is GraphQL and how does it differ from REST?
GraphQL is a query language for APIs that lets clients ask for exactly the data they need. Unlike REST, it uses a single endpoint and flexible queries instead of multiple fixed URLs.
Click to reveal answer
beginner
Name one advantage of using REST APIs.
REST APIs are simple and use standard HTTP methods, making them easy to understand and cache for better performance.
Click to reveal answer
beginner
Name one advantage of using GraphQL APIs.
GraphQL allows clients to get exactly the data they want in one request, reducing over-fetching and under-fetching of data.
Click to reveal answer
beginner
In Express, how would you typically set up a REST API endpoint?
You use app.get(), app.post(), app.put(), or app.delete() with a URL path to handle HTTP requests for resources.
Click to reveal answer
Which HTTP method is commonly used to update a resource in REST?
APUT
BGET
CPOST
DDELETE
✗ Incorrect
PUT is used to update or replace a resource in REST APIs.
What is a key feature of GraphQL compared to REST?
AMultiple endpoints for different data
BNo need for a server
COnly supports GET requests
DSingle endpoint with flexible queries
✗ Incorrect
GraphQL uses a single endpoint where clients send queries specifying exactly what data they want.
In Express, which method would you use to create a new resource in a REST API?
Aapp.put()
Bapp.get()
Capp.post()
Dapp.delete()
✗ Incorrect
POST is used to create new resources in REST APIs.
Which problem does GraphQL help solve compared to REST?
AToo many HTTP methods
BOver-fetching or under-fetching data
CLack of security
DNo caching support
✗ Incorrect
GraphQL lets clients request exactly the data they need, avoiding over-fetching or under-fetching.
Which of these is NOT a REST principle?
ASingle endpoint for all queries
BMultiple endpoints for resources
CStateless communication
DUse of standard HTTP methods
✗ Incorrect
REST uses multiple endpoints for different resources, not a single endpoint.
Explain the main differences between REST and GraphQL APIs.
Think about how clients ask for data and how many URLs they use.
You got /4 concepts.
Describe how you would set up a simple REST API endpoint in Express.
Remember Express methods match HTTP verbs.
You got /4 concepts.
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
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 A
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
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 C
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
Step 1: Understand the GraphQL query
The query requests only the name field 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 B
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
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 D
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
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 A
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