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
Migration from REST to GraphQL
📖 Scenario: You work for a company that currently uses REST APIs to fetch user data. The company wants to switch to GraphQL to make data fetching more flexible and efficient.You will build a simple GraphQL schema and resolver setup that mimics the existing REST data structure.
🎯 Goal: Create a GraphQL schema and resolver that fetch user information similar to the existing REST API response.Learn how to define types, queries, and resolve data in GraphQL.
📋 What You'll Learn
Define a GraphQL type User with fields id, name, and email
Create a query type Query with a field users that returns a list of User
Set up a resolver for users that returns the exact user data
Add a configuration variable maxUsers to limit the number of users returned
💡 Why This Matters
🌍 Real World
Many companies migrate from REST APIs to GraphQL to improve data fetching efficiency and flexibility in their applications.
💼 Career
Understanding how to define GraphQL schemas and resolvers is essential for backend developers working with modern API technologies.
Progress0 / 4 steps
1
Define the User type and sample data
Create a GraphQL type called User with fields id (ID!), name (String!), and email (String!). Also, create a constant userData as an array of three user objects with these exact values: {id: "1", name: "Alice", email: "alice@example.com"}, {id: "2", name: "Bob", email: "bob@example.com"}, {id: "3", name: "Charlie", email: "charlie@example.com"}.
GraphQL
Hint
Use gql`...` to define the GraphQL type. Create userData as an array of objects with exact fields and values.
2
Add Query type and maxUsers configuration
Add a Query type to the schema with a field users that returns a list of User. Also, create a constant maxUsers and set it to 2 to limit the number of users returned.
GraphQL
Hint
Add the Query type inside the gql template. Define maxUsers as a constant number.
3
Create resolver for users query with limit
Create a resolvers object with a Query field. Inside Query, define a users function that returns the first maxUsers entries from userData using slice(0, maxUsers).
GraphQL
Hint
Define resolvers as an object with a Query field. The users function returns a slice of userData.
4
Export schema and resolvers for server setup
Export typeDefs and resolvers using module.exports so they can be used in the GraphQL server setup.
GraphQL
Hint
Use module.exports to export both typeDefs and resolvers.
Practice
(1/5)
1. What is a key advantage of migrating from REST to GraphQL for database queries?
easy
A. REST automatically optimizes data fetching without changes.
B. You can request exactly the data you need in a single query.
C. GraphQL requires multiple requests to get all data.
D. GraphQL does not support querying nested data.
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 B
Quick 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
A. { user: { name, email } }
B. GET /user { name, email }
C. query { user { name, email } }
D. SELECT name, email FROM user
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 C
Quick 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: query { book(id: "1") { title author { name } } } What is the expected shape of the returned data?
medium
A. {"data": {"book": {"title": "Book Title", "author": {"name": "Author Name"}}}}
B. {"book": {"title": "Book Title", "author": "Author Name"}}
C. {"data": {"book": {"title": "Book Title", "author": "Author Name"}}}
D. {"data": {"book": ["title", "author"]}}
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'.