Challenge - 5 Problems
GraphQL Parent Argument Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Understanding the root parent argument in GraphQL resolver
Given the following GraphQL resolver snippet, what will be the value of the
parent argument when resolving the user field in the root query?GraphQL
const resolvers = { Query: { user(parent, args, context, info) { return { id: args.id, name: "Alice" }; } } };
Attempts:
2 left
💡 Hint
Think about what the parent argument represents at the root level of a GraphQL query.
✗ Incorrect
At the root level, the parent argument is undefined because there is no parent object above the root query.
❓ query_result
intermediate2:00remaining
Parent argument value in nested GraphQL resolvers
Consider this GraphQL schema and resolver snippet. What will be the value of the
parent argument when resolving the posts field inside the User type?GraphQL
const resolvers = { Query: { user(parent, args) { return { id: 1, name: "Bob" }; } }, User: { posts(parent, args) { return [{ id: 101, title: "Hello" }]; } } };
Attempts:
2 left
💡 Hint
The parent argument in nested resolvers is the object returned by the parent field resolver.
✗ Incorrect
When resolving nested fields, the parent argument is the object returned by the previous resolver, here the user object.
📝 Syntax
advanced2:00remaining
Identify the syntax error in resolver using parent argument
Which option contains a syntax error in the way the
parent argument is used in this resolver function?GraphQL
const resolvers = { User: { posts(parent, args) { return parent.posts; } } };
Attempts:
2 left
💡 Hint
Check the function parameter list syntax carefully.
✗ Incorrect
Option A is missing a comma between parameters, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing resolver usage of parent argument to avoid redundant database calls
Given a resolver for
User.posts that fetches posts from the database, which option best uses the parent argument to avoid fetching the user again inside the posts resolver?GraphQL
const resolvers = { Query: { user(parent, args) { return db.getUserById(args.id); } }, User: { posts(parent, args) { // Which option below is best? } } };
Attempts:
2 left
💡 Hint
Use the parent argument to get the user id directly.
✗ Incorrect
Option C uses the parent object passed from the user resolver, avoiding an extra database call.
🧠 Conceptual
expert2:00remaining
Why is the parent argument null at the root level in GraphQL?
In GraphQL, the
parent argument in resolvers is undefined at the root query level. Why is this the case?Attempts:
2 left
💡 Hint
Think about the structure of a GraphQL query and how resolvers are called.
✗ Incorrect
The root query is the top-level entry point, so it has no parent object; hence parent is undefined.