Custom error extensions are additional fields added to GraphQL error responses to provide more details about the error, such as error codes or extra context.
Click to reveal answer
intermediate
How do you add custom error extensions in a GraphQL resolver?
You throw an error with an 'extensions' property, for example: throw new ApolloError('Not found', 'NOT_FOUND', { resource: 'User' }) where the third argument adds custom extensions.
Click to reveal answer
beginner
Why use custom error extensions instead of just error messages?
Custom extensions allow clients to programmatically identify error types and handle them differently, improving user experience and debugging.
Click to reveal answer
beginner
What is the standard field name for custom error details in GraphQL errors?
The standard field name is 'extensions'. It is an object that can hold any extra information about the error.
Click to reveal answer
intermediate
Give an example of a custom error extension field you might add.
You might add a field like { code: 'UNAUTHENTICATED', timestamp: '2024-06-01T12:00:00Z' } to indicate the error type and when it happened.
Click to reveal answer
What is the purpose of the 'extensions' field in a GraphQL error?
ATo store the query string
BTo hold the user's authentication token
CTo provide extra structured information about the error
DTo define the GraphQL schema
✗ Incorrect
The 'extensions' field holds extra details about the error to help clients understand and handle it.
How do you include custom error extensions when throwing an error in Apollo Server?
ABy changing the HTTP status code
BBy passing an object as the third argument to ApolloError
CBy adding a new query
DBy modifying the GraphQL schema
✗ Incorrect
ApolloError accepts a third argument for extensions, allowing you to add custom error details.
Which of these is NOT a good reason to use custom error extensions?
ATo store sensitive user passwords
BTo provide user-friendly error codes
CTo help clients handle errors programmatically
DTo add timestamps or context to errors
✗ Incorrect
Sensitive data like passwords should never be included in error extensions.
What type of data can the 'extensions' field contain?
AAny JSON-serializable data
BOnly numbers
COnly strings
DOnly booleans
✗ Incorrect
The 'extensions' field can hold any data that can be converted to JSON.
If you want to indicate an error is due to authentication failure, which extension code might you use?
ASUCCESS
BNOT_FOUND
CINTERNAL_ERROR
DUNAUTHENTICATED
✗ Incorrect
'UNAUTHENTICATED' is commonly used to indicate authentication errors.
Explain how custom error extensions improve error handling in GraphQL.
Think about why just an error message might not be enough.
You got /4 concepts.
Describe how to add custom error extensions in a GraphQL resolver using Apollo Server.
Focus on the ApolloError constructor parameters.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using extensions in GraphQL error responses?
easy
A. To change the data returned by the query
B. To hide errors from the client
C. To speed up the query execution
D. To provide extra information like error codes and details
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 D
Quick Check:
Extensions add info = B [OK]
Hint: Extensions add details to errors, not data or speed [OK]
Common Mistakes:
Thinking extensions change query data
Assuming extensions hide errors
Confusing extensions with performance tools
2. Which of the following is the correct way to add a custom error extension in a GraphQL error object?
easy
A. { message: 'Error', extensions: ['BAD_REQUEST'] }
B. { message: 'Error', code: 'BAD_REQUEST' }
C. { message: 'Error', extensions: { code: 'BAD_REQUEST' } }
D. { error: 'Error', extensions: 'BAD_REQUEST' }
Solution
Step 1: Identify correct error object structure
The error object must have a message and an extensions object with code inside.
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 A
Quick Check:
Extensions type error = A [OK]
Hint: Extensions must always be an object, never a string [OK]
Common Mistakes:
Using string instead of object for extensions
Ignoring GraphQL error format rules
Assuming message content causes error
5. You want to send a GraphQL error with a custom extension that includes an error code and a retry flag set to false. Which of these error objects correctly implements this?