Performance: REST vs GraphQL awareness
This affects how fast data loads on the page and how much data the browser processes, impacting load speed and interaction responsiveness.
Jump into concepts and practice - no test required
GraphQL query: { user(id: "123") { name, posts { title }, friends { name } } }REST endpoints: GET /user/123, GET /user/123/posts, GET /user/123/friends
| Pattern | Network Requests | Payload Size | Rendering Delay | Verdict |
|---|---|---|---|---|
| Multiple REST calls | Multiple HTTP requests | Larger due to over-fetching | Higher due to waiting on all requests | [X] Bad |
| GraphQL single query | Single HTTP request | Smaller, only requested fields | Lower, data ready sooner | [OK] Good |
{ 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 }));name field of the user with id "1".app.get('/users/:id', (req, res) => {
const user = users.find(u => u.id === req.params.id);
if (user) {
res.json(user);
}
res.end();
});