Default resolvers in GraphQL - Time & Space Complexity
When using default resolvers in GraphQL, it's important to understand how the time it takes to get data changes as the data size grows.
We want to know how the work done by default resolvers increases when we ask for more items.
Analyze the time complexity of the following code snippet.
query {
books {
title
author {
name
}
}
}
This query asks for a list of books and for each book, it fetches the title and the author's name using default resolvers.
Look for repeated actions in the query resolution.
- Primary operation: The default resolver fetches the list of books once and then fetches the author for each book.
- How many times: Once for the books list, and once for each book (for its author).
As the number of books grows, the number of fetches grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 books fetch + 10 author fetches ≈ 11 operations |
| 100 | 1 books fetch + 100 author fetches ≈ 101 operations |
| 1000 | 1 books fetch + 1000 author fetches ≈ 1001 operations |
Pattern observation: The total work grows linearly with the number of books.
Time Complexity: O(n)
This means the time to get all books and their authors grows in a straight line as the number of books increases.
[X] Wrong: "Fetching nested fields with default resolvers is constant time regardless of list size."
[OK] Correct: Each book and its author are fetched separately, so more books mean more work, not the same amount.
Understanding how default resolvers scale helps you explain data fetching costs clearly and shows you know how GraphQL handles nested data.
"What if the author field was a list of authors instead of one? How would the time complexity change?"
Practice
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 BQuick Check:
Default resolver = automatic field-to-key mapping [OK]
- Thinking default resolvers handle validation
- Confusing default resolvers with custom resolvers
- Assuming default resolvers manage errors
Solution
Step 1: Identify correct schema syntax
GraphQL schema fields are defined asfieldName: Typewithout 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 CQuick Check:
Default resolver needs simple field: Type syntax [OK]
- Adding parentheses after type name
- Using custom directives without definition
- Using arrow or other symbols in schema
type Query { book: Book } and data { book: { title: "1984", author: "Orwell" } }, what will the default resolver return for the field book?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 fieldbookmatches the data keybookwhich contains the object with title and author.Final Answer:
{ title: "1984", author: "Orwell" } -> Option AQuick Check:
Field book returns matching data object [OK]
- Returning only one property instead of whole object
- Expecting null if no custom resolver is defined
- Assuming error if resolver is not explicitly written
type Query { user: User } but your query returns null for user. What is a likely cause related to 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 AQuick Check:
Missing data key causes null from default resolver [OK]
- Assuming custom resolver is always required
- Blaming schema syntax without checking data
- Thinking default resolvers are unsupported
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?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.Step 3: Avoid overriding entire 'product' resolver
Overriding entire 'product' would require manually returning all fields, losing default resolver benefits.Final Answer:
Write a custom resolver only for the 'price' field inside Product type -> Option DQuick Check:
Custom resolver per field + default resolvers for others [OK]
- Overriding entire parent field unnecessarily
- Trying to mix default and custom resolvers at root level only
- Modifying data source instead of resolver logic
Ready to prove it?
Pass the test and this pattern moves unknown
