Moving from REST to GraphQL helps you get exactly the data you want in one request. It makes your app faster and easier to build.
Migration from REST to GraphQL
Introduction
Syntax
GraphQL
type Query {
getUser(id: ID!): User
}
type User {
id: ID!
name: String
email: String
}GraphQL uses a single endpoint for all queries instead of multiple REST endpoints.
You define types and queries that describe exactly what data can be requested.
Examples
GraphQL
# REST example GET /users/1 # GraphQL equivalent { getUser(id: "1") { id name email } }
GraphQL
# REST multiple calls GET /users/1 GET /users/1/posts # GraphQL single call { getUser(id: "1") { id name posts { id title } } }
GraphQL
# Request only needed fields
{
getUser(id: "1") {
name
}
}Sample Program
This schema defines a user and their posts. The query asks for user details and their posts in one request.
GraphQL
# GraphQL schema definition
schema {
query: Query
}
type Query {
getUser(id: ID!): User
}
type User {
id: ID!
name: String
email: String
posts: [Post]
}
type Post {
id: ID!
title: String
}
# Sample query
{
getUser(id: "1") {
id
name
email
posts {
id
title
}
}
}Important Notes
GraphQL reduces the number of requests by combining data fetching.
It requires defining a schema that describes your data and queries.
Common mistake: Trying to replicate REST endpoints exactly instead of designing flexible queries.
Summary
GraphQL lets you ask for exactly what you need in one request.
It improves efficiency by reducing over-fetching and under-fetching.
Migration means defining a schema and rewriting client queries.
Practice
1. What is a key advantage of migrating from REST to GraphQL for database queries?
easy
Solution
Step 1: Understand REST vs GraphQL data fetching
REST often requires multiple requests or returns extra data, while GraphQL lets you specify exactly what you want.Step 2: Identify the main benefit of GraphQL
GraphQL reduces over-fetching and under-fetching by allowing precise queries in one request.Final Answer:
You can request exactly the data you need in a single query. -> Option BQuick Check:
GraphQL precise data fetching = C [OK]
Hint: GraphQL = one request, exact data [OK]
Common Mistakes:
- Thinking GraphQL needs multiple requests
- Believing REST auto-optimizes data fetching
- Assuming GraphQL can't query nested data
2. Which of the following is the correct way to define a simple GraphQL query to get a user's name and email?
easy
Solution
Step 1: Recognize GraphQL query syntax
GraphQL queries start with the keyword 'query' followed by the fields requested inside braces.Step 2: Compare options to GraphQL syntax
query { user { name, email } } matches the correct GraphQL query format; others are REST, SQL, or invalid syntax.Final Answer:
query { user { name, email } } -> Option CQuick Check:
GraphQL query syntax = D [OK]
Hint: GraphQL queries start with 'query' keyword [OK]
Common Mistakes:
- Using REST or SQL syntax instead of GraphQL
- Omitting the 'query' keyword
- Incorrect braces placement
3. Given this GraphQL query:
What is the expected shape of the returned data?
query { book(id: "1") { title author { name } } }What is the expected shape of the returned data?
medium
Solution
Step 1: Understand GraphQL nested response format
GraphQL returns data inside a 'data' object, preserving nested structure matching the query.Step 2: Match query fields to response structure
The query requests 'title' and nested 'author' with 'name', so response nests 'author' as an object with 'name'.Final Answer:
{"data": {"book": {"title": "Book Title", "author": {"name": "Author Name"}}}} -> Option AQuick Check:
Nested data in 'data' key = A [OK]
Hint: GraphQL response nests data under 'data' key [OK]
Common Mistakes:
- Returning author as string instead of object
- Missing 'data' wrapper in response
- Using arrays instead of objects for fields
4. You migrated a REST endpoint to GraphQL but get an error:
Cannot query field 'userName' on type 'User'. What is the likely cause?medium
Solution
Step 1: Analyze the error message
The error says 'userName' is not a valid field on 'User' type, indicating schema mismatch.Step 2: Understand GraphQL schema role
GraphQL queries must match fields defined in the schema; missing fields cause errors.Final Answer:
The GraphQL schema does not define a 'userName' field on 'User' type. -> Option DQuick Check:
Schema field missing = A [OK]
Hint: Check schema fields match query fields [OK]
Common Mistakes:
- Blaming REST endpoint for GraphQL schema errors
- Ignoring schema definitions
- Assuming GraphQL can't query user names
5. During migration from REST to GraphQL, you want to avoid over-fetching user data. Which approach best achieves this?
hard
Solution
Step 1: Understand over-fetching in REST vs GraphQL
REST often returns full objects; GraphQL lets you specify exactly which fields to fetch.Step 2: Choose the best method to limit data fetched
Defining a GraphQL query with only needed fields avoids over-fetching efficiently in one request.Final Answer:
Define a GraphQL query that requests only the needed user fields like name and email. -> Option AQuick Check:
Selective field query = B [OK]
Hint: Request only needed fields in GraphQL query [OK]
Common Mistakes:
- Fetching all data then filtering client-side
- Using multiple queries instead of one precise query
- Relying on REST query parameters only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
