A default resolver is the built-in function GraphQL uses to fetch the value of a field when no custom resolver is provided. It simply returns the property of the source object that matches the field name.
Click to reveal answer
beginner
How does GraphQL's default resolver find the value for a field?
It looks for a property on the parent object with the same name as the field and returns its value directly.
Click to reveal answer
intermediate
When would you need to write a custom resolver instead of relying on the default resolver?
When the data needs transformation, fetching from another source, or combining multiple fields, you write a custom resolver. The default resolver only returns existing properties as-is.
Click to reveal answer
beginner
True or False: The default resolver can handle nested objects automatically.
True. If the field's value is an object, the default resolver returns it, and GraphQL continues resolving fields on that nested object.
Click to reveal answer
beginner
What happens if the default resolver does not find a matching property on the source object?
It returns null or undefined, which means the field will have no value in the query result unless a custom resolver handles it.
Click to reveal answer
What does the default resolver in GraphQL do when resolving a field?
AReturns the property of the source object matching the field name
BAlways returns null
CFetches data from a database automatically
DTransforms the data before returning
✗ Incorrect
The default resolver returns the property on the source object that matches the field name.
When is a custom resolver necessary instead of using the default resolver?
AWhen data needs to be fetched from another source or transformed
BWhen the field name matches the source object property
CWhen the query is simple
DWhen the default resolver returns the correct value
✗ Incorrect
Custom resolvers are needed when data requires fetching from other sources or transformation.
If a field's value is an object, how does the default resolver handle it?
AIt throws an error
BIt returns null
CIt returns the object and GraphQL resolves nested fields automatically
DIt converts the object to a string
✗ Incorrect
The default resolver returns the object, allowing GraphQL to resolve nested fields.
What will the default resolver return if the source object does not have the requested field?
AAn error message
BNull or undefined
CThe entire source object
DA default string value
✗ Incorrect
If the property is missing, the default resolver returns null or undefined.
Which of the following is NOT true about default resolvers?
AThey handle nested objects by returning them
BThey return the property matching the field name
CThey are used when no custom resolver is defined
DThey automatically fetch data from databases
✗ Incorrect
Default resolvers do not fetch data from databases automatically; they just return existing properties.
Explain how GraphQL's default resolver works when resolving a field in a query.
Think about how GraphQL finds the value without custom code.
You got /4 concepts.
Describe a situation where you would need to write a custom resolver instead of relying on the default resolver.
When the data is not directly available as a simple property.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of default resolvers in GraphQL?
easy
A. To write custom logic for every field in the schema
B. To automatically return data by matching schema fields to data keys
C. To validate user input before querying the database
D. To handle errors during query execution
Solution
Step 1: Understand default resolver behavior
Default resolvers automatically map schema fields to matching data keys without extra code.
Step 2: Compare with other options
Options B, C, and D describe custom logic, validation, and error handling, which are not default resolver roles.
Final Answer:
To automatically return data by matching schema fields to data keys -> Option B
Hint: Default resolvers match schema fields to data keys automatically [OK]
Common Mistakes:
Thinking default resolvers handle validation
Confusing default resolvers with custom resolvers
Assuming default resolvers manage errors
2. Which of the following is the correct way to define a GraphQL schema field that uses a default resolver?
easy
A. type Query { user: User() }
B. type Query { user: User @customResolver }
C. type Query { user: User }
D. type Query { user: User -> resolver }
Solution
Step 1: Identify correct schema syntax
GraphQL schema fields are defined as fieldName: Type without extra syntax for default resolvers.
Step 2: Analyze options
type Query { user: User } uses correct syntax. Options B, C, and D include invalid or non-standard syntax.
Final Answer:
type Query { user: User } -> Option C
Quick Check:
Default resolver needs simple field: Type syntax [OK]
Hint: Default resolvers need simple field: Type syntax in schema [OK]
Common Mistakes:
Adding parentheses after type name
Using custom directives without definition
Using arrow or other symbols in schema
3. Given the schema type Query { book: Book } and data { book: { title: "1984", author: "Orwell" } }, what will the default resolver return for the field book?
medium
A. { title: "1984", author: "Orwell" }
B. "1984"
C. null
D. Error: Resolver not found
Solution
Step 1: Understand default resolver behavior
The default resolver returns the data object matching the field name if it exists.
Step 2: Match field to data
The field book matches the data key book which contains the object with title and author.
Final Answer:
{ title: "1984", author: "Orwell" } -> Option A
Quick Check:
Field book returns matching data object [OK]
Hint: Default resolver returns matching data object for field name [OK]
Common Mistakes:
Returning only one property instead of whole object
Expecting null if no custom resolver is defined
Assuming error if resolver is not explicitly written
4. You wrote a GraphQL schema with type Query { user: User } but your query returns null for user. What is a likely cause related to default resolvers?
medium
A. The data source does not have a matching key named 'user'
B. You forgot to write a custom resolver for 'user'
C. The schema syntax is invalid
D. GraphQL does not support default resolvers
Solution
Step 1: Recall default resolver behavior
Default resolvers return data matching the field name from the data source.
Step 2: Identify cause of null result
If the data source lacks a key named 'user', the default resolver returns null.
Final Answer:
The data source does not have a matching key named 'user' -> Option A
Quick Check:
Missing data key causes null from default resolver [OK]
Hint: Check data keys match schema fields for default resolver to work [OK]
Common Mistakes:
Assuming custom resolver is always required
Blaming schema syntax without checking data
Thinking default resolvers are unsupported
5. You have a schema type Query { product: Product } and data { product: { id: 1, name: "Pen", price: 2.5 } }. You want to return the price with a 10% discount but keep default resolvers for other fields. How should you implement this?
hard
A. Use default resolvers only; custom resolvers cannot be mixed
B. Write a custom resolver for the entire 'product' field in Query
C. Modify the data source to store discounted price
D. Write a custom resolver only for the 'price' field inside Product type
Solution
Step 1: Understand resolver layering
Default resolvers work per field; you can override specific fields with custom resolvers.
Step 2: Apply custom resolver to 'price' only
Write a custom resolver for 'price' to apply discount, leaving other fields to default resolvers.