0
0
GraphQLquery~5 mins

Custom error extensions in GraphQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are custom error extensions in GraphQL?
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: <br>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
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
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
What type of data can the 'extensions' field contain?
AAny JSON-serializable data
BOnly numbers
COnly strings
DOnly booleans
If you want to indicate an error is due to authentication failure, which extension code might you use?
ASUCCESS
BNOT_FOUND
CINTERNAL_ERROR
DUNAUTHENTICATED
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.