Bird
Raised Fist0
GraphQLquery~20 mins

Migration from REST to GraphQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
GraphQL Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query Result: Fetching Nested Data in GraphQL

You have a REST API endpoint /users/1 that returns user data with their posts. In GraphQL, you want to fetch the same data with one query.

Given this GraphQL query:

{ user(id: 1) { id name posts { id title } } }

What is the expected JSON output?

A{"user":{"id":1,"name":"Alice","posts":[{"id":101,"title":"Hello World"},{"id":102,"title":"GraphQL Rocks"}]}}
B{"data":{"user":{"id":1,"name":"Alice","posts":[{"id":101,"title":"Hello World"},{"id":102,"title":"GraphQL Rocks"}]}}}
C{"data":{"user":{"id":1,"name":"Alice"}}}
D{"data":{"users":[{"id":1,"name":"Alice","posts":[{"id":101,"title":"Hello World"}]}]}}
Attempts:
2 left
💡 Hint

GraphQL responses always wrap the result inside a data field matching the query shape.

🧠 Conceptual
intermediate
1:30remaining
Understanding GraphQL Query vs REST Endpoint

Which statement best describes the difference between a GraphQL query and a REST API endpoint?

AA GraphQL query allows clients to specify exactly what data they want, while a REST endpoint returns a fixed data structure.
BA REST endpoint can return nested data in one call, but GraphQL requires multiple queries.
CGraphQL queries always require multiple network requests, unlike REST endpoints.
DREST APIs do not support filtering data, but GraphQL does not support filtering either.
Attempts:
2 left
💡 Hint

Think about flexibility in data retrieval.

📝 Syntax
advanced
2:00remaining
Identify the Syntax Error in GraphQL Mutation

Consider this GraphQL mutation intended to create a new user:

mutation {
  createUser(name: "Bob", age: 30) {
    id
    name
  }
}

Which option shows the correct syntax to fix the error?

A
mutation {
  createUser(name: "Bob" age: 30) {
    id
    name
  }
}
B
mutation {
  createUser(name: "Bob", age: 30) {
    id
    name
  }
}
C
mutation {
  createUser(input: {name: "Bob", age: 30}) {
    id
    name
  }
}
D
mutation {
  createUser(name: "Bob", age: "30") {
    id
    name
  }
}
Attempts:
2 left
💡 Hint

Check how arguments are passed in mutations.

optimization
advanced
1:30remaining
Optimizing GraphQL Queries to Reduce Overfetching

You have a REST endpoint /products returning all product details. Migrating to GraphQL, you want to optimize queries to fetch only product names and prices.

Which GraphQL query best achieves this?

A{ products { name price } }
B{ products { id name price description stock } }
C{ products { name price stock } }
D{ products { id name } }
Attempts:
2 left
💡 Hint

Only request the fields you need.

🔧 Debug
expert
2:30remaining
Debugging a GraphQL Schema Migration Issue

After migrating from REST to GraphQL, a query to fetch a user's email returns null even though the REST API returned the email correctly.

Given this GraphQL schema snippet:

type User {
  id: ID!
  name: String!
  email: String
}

And resolver code snippet:

user: (parent, args, context) => {
  return database.findUserById(args.id);
}

What is the most likely cause of the null email in the response?

AThe client query does not request the email field.
BThe GraphQL schema incorrectly marks email as nullable, causing it to be null.
CThe resolver function is missing a return statement.
DThe database query does not select the email field, so it is missing in the returned user object.
Attempts:
2 left
💡 Hint

Check what data the resolver returns from the database.

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

  1. 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.
  2. Step 2: Identify the main benefit of GraphQL

    GraphQL reduces over-fetching and under-fetching by allowing precise queries in one request.
  3. Final Answer:

    You can request exactly the data you need in a single query. -> Option B
  4. 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

  1. Step 1: Recognize GraphQL query syntax

    GraphQL queries start with the keyword 'query' followed by the fields requested inside braces.
  2. Step 2: Compare options to GraphQL syntax

    query { user { name, email } } matches the correct GraphQL query format; others are REST, SQL, or invalid syntax.
  3. Final Answer:

    query { user { name, email } } -> Option C
  4. 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

  1. Step 1: Understand GraphQL nested response format

    GraphQL returns data inside a 'data' object, preserving nested structure matching the query.
  2. 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'.
  3. Final Answer:

    {"data": {"book": {"title": "Book Title", "author": {"name": "Author Name"}}}} -> Option A
  4. Quick 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
A. GraphQL does not support querying user names.
B. The REST endpoint is down.
C. The query syntax is missing curly braces.
D. The GraphQL schema does not define a 'userName' field on 'User' type.

Solution

  1. Step 1: Analyze the error message

    The error says 'userName' is not a valid field on 'User' type, indicating schema mismatch.
  2. Step 2: Understand GraphQL schema role

    GraphQL queries must match fields defined in the schema; missing fields cause errors.
  3. Final Answer:

    The GraphQL schema does not define a 'userName' field on 'User' type. -> Option D
  4. Quick 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
A. Define a GraphQL query that requests only the needed user fields like name and email.
B. Keep using the REST endpoint but add query parameters to limit fields.
C. Fetch all user data and filter unwanted fields on the client side.
D. Use multiple GraphQL queries to get each user field separately.

Solution

  1. Step 1: Understand over-fetching in REST vs GraphQL

    REST often returns full objects; GraphQL lets you specify exactly which fields to fetch.
  2. 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.
  3. Final Answer:

    Define a GraphQL query that requests only the needed user fields like name and email. -> Option A
  4. Quick 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