Custom error extensions in GraphQL - Time & Space Complexity
When using custom error extensions in GraphQL, it's important to understand how the process of adding extra error details affects performance.
We want to know how the time to handle errors grows as the number of errors or the size of error details increases.
Analyze the time complexity of this GraphQL error handling snippet.
type Query {
getUser(id: ID!): User
}
type User {
id: ID!
name: String
}
// Resolver example
function getUserResolver(parent, args) {
try {
// fetch user logic
} catch (error) {
throw new GraphQLError("User not found", {
extensions: { code: "USER_NOT_FOUND", timestamp: Date.now() }
});
}
}
This code adds custom extensions to errors thrown during query resolution.
Look for repeated actions that affect time.
- Primary operation: Adding custom extensions to each error object.
- How many times: Once per error thrown during query execution.
As the number of errors increases, the time to add extensions grows linearly.
| Input Size (number of errors) | Approx. Operations |
|---|---|
| 1 | 1 operation to add extensions |
| 10 | 10 operations to add extensions |
| 100 | 100 operations to add extensions |
Pattern observation: The work grows directly with the number of errors.
Time Complexity: O(n)
This means the time to add custom error extensions grows in a straight line with the number of errors.
[X] Wrong: "Adding custom extensions to errors is instant and does not affect performance."
[OK] Correct: Each error requires extra processing to add extensions, so more errors mean more work and longer time.
Understanding how error handling scales helps you write efficient GraphQL APIs and shows you can think about performance beyond just query logic.
What if we added nested objects or large data inside the error extensions? How would the time complexity change?
Practice
extensions in GraphQL error responses?Solution
Step 1: Understand the role of error extensions
Extensions add extra info to errors, such as codes or metadata.Step 2: Compare with other options
Extensions do not change data, speed, or hide errors; they enrich error info.Final Answer:
To provide extra information like error codes and details -> Option DQuick Check:
Extensions add info = B [OK]
- Thinking extensions change query data
- Assuming extensions hide errors
- Confusing extensions with performance tools
Solution
Step 1: Identify correct error object structure
The error object must have a message and an extensions object with code inside.Step 2: Check each option's syntax
{ message: 'Error', extensions: { code: 'BAD_REQUEST' } } correctly nests code inside extensions; others misuse or misplace fields.Final Answer:
{ message: 'Error', extensions: { code: 'BAD_REQUEST' } } -> Option CQuick Check:
Extensions is an object with code = D [OK]
- Putting code outside extensions
- Using extensions as a string or array
- Misnaming fields like error instead of message
{"errors":[{"message":"Not found","extensions":{"code":"NOT_FOUND","timestamp":"2024-06-01T12:00:00Z"}}]}What will the client see in the
extensions field?Solution
Step 1: Locate the extensions field in the error
The extensions field contains code and timestamp as key-value pairs.Step 2: Understand what client receives
The client sees the extensions object exactly as given, with code and timestamp.Final Answer:
{"code":"NOT_FOUND","timestamp":"2024-06-01T12:00:00Z"} -> Option AQuick Check:
Extensions content = A [OK]
- Confusing message with extensions
- Expecting extensions to be a string
- Ignoring extra fields like timestamp
{ message: 'Unauthorized', extensions: 'AUTH_ERROR' }Why does this cause a problem?
Solution
Step 1: Check the type of extensions field
Extensions must be an object with key-value pairs, not a string.Step 2: Identify the error cause
Using a string for extensions causes syntax or runtime errors in GraphQL.Final Answer:
Extensions must be an object, not a string -> Option AQuick Check:
Extensions type error = A [OK]
- Using string instead of object for extensions
- Ignoring GraphQL error format rules
- Assuming message content causes error
Solution
Step 1: Check the data types for extensions fields
The retry flag should be a boolean false, not string or null or number.Step 2: Validate each option's retry value
{ message: 'Timeout', extensions: { code: 'TIMEOUT', retry: false } } uses boolean false correctly; others use wrong types.Final Answer:
{ message: 'Timeout', extensions: { code: 'TIMEOUT', retry: false } } -> Option BQuick Check:
Boolean retry flag = C [OK]
- Using string 'no' instead of boolean false
- Using null or 0 for boolean flags
- Ignoring correct data types in extensions
Ready to prove it?
Pass the test and this pattern moves unknown
