0
0
GraphQLquery~20 mins

Migration from REST to GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.