Why resolvers connect schema to data in GraphQL - Performance Analysis
When using GraphQL, resolvers are the parts that fetch data for each field in the schema.
We want to understand how the time to get data grows as the amount of data or requests grows.
Analyze the time complexity of the following resolver code snippet.
type Query {
books: [Book]
}
type Book {
title: String
author: Author
}
type Author {
name: String
}
const resolvers = {
Query: {
books: () => fetchAllBooks()
},
Book: {
author: (book) => fetchAuthorById(book.authorId)
}
}
This code fetches a list of books, then for each book fetches its author data.
Look for repeated data fetching steps.
- Primary operation: Fetching authors for each book.
- How many times: Once for each book in the list.
As the number of books grows, the number of author fetches grows too.
| Input Size (n books) | Approx. Operations |
|---|---|
| 10 | 10 author fetches + 1 books fetch |
| 100 | 100 author fetches + 1 books fetch |
| 1000 | 1000 author fetches + 1 books fetch |
Pattern observation: The number of author fetches grows directly with the number of books.
Time Complexity: O(n)
This means the time to get all data grows in a straight line as the number of books increases.
[X] Wrong: "Fetching authors for each book happens all at once and does not add extra time."
[OK] Correct: Each author fetch is a separate operation, so more books mean more fetches and more time.
Understanding how resolvers fetch data step-by-step helps you explain how your code handles growing data smoothly.
What if the author data was included inside each book record already? How would the time complexity change?
Practice
Solution
Step 1: Understand the purpose of resolvers
Resolvers are functions that run when a field in the schema is requested to provide the actual data.Step 2: Differentiate from schema definition
The schema defines the shape of data, but resolvers connect those fields to real data sources or computations.Final Answer:
To fetch or compute the data for a specific field in the schema -> Option AQuick Check:
Resolvers fetch data = A [OK]
- Confusing resolvers with schema definitions
- Thinking resolvers handle authentication
- Believing resolvers format queries
Solution
Step 1: Recall JavaScript arrow function syntax
Arrow functions use parentheses for parameters separated by commas, e.g., (a, b, c) => result.Step 2: Check each option's parameter syntax
const resolver = (parent, args, context) => data; uses commas correctly; A uses semicolons instead of commas; B uses braces instead of parentheses; D misses commas.Final Answer:
const resolver = (parent, args, context) => data; -> Option CQuick Check:
Arrow function params need commas = C [OK]
- Using braces {} instead of parentheses () for parameters
- Separating parameters with semicolons
- Omitting commas between parameters
userName?
const resolvers = {
Query: {
userName: () => "Alice"
}
};Solution
Step 1: Identify the resolver function for userName
The resolver for userName returns the string "Alice" directly.Step 2: Understand the query result
When the userName field is queried, the resolver returns "Alice", so the output is "Alice".Final Answer:
"Alice" -> Option AQuick Check:
Resolver returns "Alice" = B [OK]
- Confusing field name with returned value
- Expecting null if no arguments
- Assuming error without resolver
const resolvers = {
Query: {
userAge: (parent, args) => {
return args.age
}
}
};
// Query: { userAge }
// Variables: {}Solution
Step 1: Check resolver parameters and usage
The resolver expects an argument named 'age' from args to return.Step 2: Analyze the query and variables
The query requests userAge but provides no variables, so args.age is undefined.Final Answer:
Resolver expects argument 'age' but none provided in query -> Option BQuick Check:
Missing argument in query = D [OK]
- Assuming context is always required
- Ignoring missing query variables
- Thinking missing semicolon causes error here
userProfile field?Solution
Step 1: Understand resolver role in combining data
Resolvers can run code to fetch and combine data from multiple sources before returning.Step 2: Evaluate options for fetching and merging data
Write a resolver that calls the database and API, then merges results before returning correctly describes writing a resolver that calls both sources and merges results. Other options either ignore data sources or misuse resolvers.Final Answer:
Write a resolver that calls the database and API, then merges results before returning -> Option DQuick Check:
Resolvers combine data sources = A [OK]
- Expecting schema alone to fetch data
- Trying multiple resolvers for one field
- Ignoring one data source
Ready to prove it?
Pass the test and this pattern moves unknown
