📖 Scenario: You are building a simple GraphQL API for a bookstore. You want to understand how the parent (also called root) argument works in resolver functions to access data from the previous level in the query.
🎯 Goal: Learn how to use the parent argument in GraphQL resolvers to access data from the root query or from parent fields.
📋 What You'll Learn
Create a root query type with a book field that returns a book object
Add a resolver for book that returns a fixed book object
Add a title field on the book type and a resolver that uses the parent argument to return the title
Add a author field on the book type and a resolver that uses the parent argument to return the author
💡 Why This Matters
🌍 Real World
GraphQL APIs often require accessing data from parent objects to resolve nested fields. Understanding the parent argument is essential for building flexible APIs.
💼 Career
Backend developers working with GraphQL need to write resolvers that use the parent argument to fetch and return nested data correctly.
Progress0 / 4 steps
1
Define the root query with a book field
Create a GraphQL schema with a root Query type that has a book field returning a Book type. Define the Book type with title and author fields of type String.
GraphQL
Hint
Define the root Query type with a book field returning Book. Then define the Book type with title and author fields.
2
Add resolver for book returning a fixed book object
Add a resolver function for the root book field that returns an object with title set to "1984" and author set to "George Orwell".
GraphQL
Hint
Return a fixed object with title and author properties from the book resolver.
3
Add resolver for title using the parent argument
Add a resolver for the title field on the Book type that returns the title property from the parent argument.
GraphQL
Hint
Use the parent argument to access the title property and return it.
4
Add resolver for author using the parent argument
Add a resolver for the author field on the Book type that returns the author property from the parent argument.
GraphQL
Hint
Use the parent argument to access and return the author property.
Practice
(1/5)
1. What does the parent argument represent in a GraphQL resolver function?
easy
A. It is the root query object that starts the GraphQL execution.
B. It is the context object shared across all resolvers.
C. It contains the arguments passed to the current query.
D. It holds the result of the previous resolver in the resolver chain.
Solution
Step 1: Understand resolver chaining
Resolvers can be nested, so the parent argument holds the output of the previous resolver in the chain.
Step 2: Differentiate from other arguments
The context is shared globally, args are query inputs, and root is the initial query object, not the parent.
Final Answer:
It holds the result of the previous resolver in the resolver chain. -> Option D
Quick Check:
parent = previous resolver result [OK]
Hint: Remember: parent is previous resolver's output [OK]
Common Mistakes:
Confusing parent with context
Thinking parent holds query arguments
Assuming parent is always the root query
2. Which of the following is the correct way to define a resolver function using the parent argument in GraphQL?
easy
A. resolver(parent, args, context, info) { ... }
B. resolver(args, parent, context, info) { ... }
C. resolver(context, parent, args, info) { ... }
D. resolver(info, args, parent, context) { ... }
Solution
Step 1: Recall resolver argument order
The standard resolver function signature is resolver(parent, args, context, info).
Step 2: Verify argument positions
Only resolver(parent, args, context, info) { ... } matches the correct order with parent as the first argument.
Final Answer:
resolver(parent, args, context, info) { ... } -> Option A
Quick Check:
Parent is first argument in resolver [OK]
Hint: Parent is always first argument in resolver [OK]
Common Mistakes:
Swapping parent and args positions
Placing context before parent
Misordering resolver parameters
3. Given the following GraphQL schema and resolvers, what will be the output of the query { user { id name posts { title } } }?
A. The resolver should not use filter on postsData.
B. The resolver is missing the parent argument as the first parameter.
C. The resolver should use context instead of args.
D. The resolver function name should be Posts with uppercase P.
Solution
Step 1: Check resolver function parameters
The resolver function must have parent as the first argument to access parent data.
Step 2: Identify misuse of args instead of parent
The code incorrectly uses args.id instead of parent.id to filter posts.
Final Answer:
The resolver is missing the parent argument as the first parameter. -> Option B
Quick Check:
Parent argument must be first in resolver [OK]
Hint: Always include parent as first resolver argument [OK]
Common Mistakes:
Using args instead of parent for parent data
Ignoring resolver parameter order
Assuming function name case matters
5. You want to fetch a list of books with their authors' names using nested resolvers. The Book type has an authorId field, and the Author type has id and name. How should you use the parent argument in the author resolver inside Book to get the correct author name?
hard
A. Use parent.authorId to find the author by matching id.
B. Use args.authorId to find the author by matching id.
C. Use context.authorId to find the author by matching id.
D. Use info.authorId to find the author by matching id.
Solution
Step 1: Understand nested resolver data flow
The author resolver inside Book receives the book object as parent.
Step 2: Access authorId from parent to find author
Use parent.authorId to look up the author by matching id in authors data.
Final Answer:
Use parent.authorId to find the author by matching id. -> Option A
Quick Check:
Parent holds book data, use parent.authorId [OK]
Hint: Parent holds book; use parent.authorId for author lookup [OK]
Common Mistakes:
Using args instead of parent for authorId
Trying to get authorId from context or info
Ignoring parent argument in nested resolver
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.