0
0
GraphQLquery~10 mins

Custom error extensions in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom error extensions
GraphQL Query Received
Execute Resolver
Error Occurs?
NoReturn Data
Yes
Create Error Object
Add Custom Extensions
Return Error with Extensions
When a GraphQL query runs, if an error happens, a custom extensions object can be added to the error before sending it back.
Execution Sample
GraphQL
throw new GraphQLError('Not found', {
  extensions: { code: 'NOT_FOUND', detail: 'User ID missing' }
});
This code throws a GraphQL error with a custom extensions object containing extra info.
Execution Table
StepActionError Object StateExtensions AddedResult Sent
1Query receivedNo error yetNoneNo response yet
2Resolver runsNo error yetNoneNo response yet
3Error thrownError message: 'Not found'NoneNo response yet
4Add extensionsError message: 'Not found'{ code: 'NOT_FOUND', detail: 'User ID missing' }No response yet
5Return errorError with extensions{ code: 'NOT_FOUND', detail: 'User ID missing' }Error response sent
💡 Error with custom extensions returned to client
Variable Tracker
VariableStartAfter Step 3After Step 4Final
errorundefined{ message: 'Not found' }{ message: 'Not found', extensions: { code: 'NOT_FOUND', detail: 'User ID missing' } }Returned to client with extensions
Key Moments - 2 Insights
Why do we add extensions to the error object?
Extensions provide extra structured info about the error, like error codes or details, which helps clients handle errors better. See execution_table step 4.
Does the error message change when we add extensions?
No, the main error message stays the same. Extensions add extra info without changing the message. See execution_table steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step are extensions added to the error?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Extensions Added' column in execution_table rows
According to variable_tracker, what is the state of 'error' after step 3?
A{ message: 'Not found', extensions: {...} }
Bundefined
C{ message: 'Not found' }
DReturned to client
💡 Hint
Look at the 'After Step 3' column for 'error' in variable_tracker
If we did not add extensions, what would the client receive?
AError with only message, no extensions
BError with default extensions
CError with no message
DNo error at all
💡 Hint
Refer to execution_table step 3 where error is thrown without extensions
Concept Snapshot
GraphQL errors can include a custom 'extensions' object.
Use 'extensions' to add extra info like error codes.
Throw errors with 'new GraphQLError(message, { extensions })'.
Clients receive both message and extensions.
Extensions help clients handle errors better.
Full Transcript
When a GraphQL query runs, the server executes resolvers. If an error happens, the server creates an error object with a message. We can add a custom 'extensions' object to this error to provide extra details like error codes or descriptions. This helps clients understand and handle errors better. The error with extensions is then sent back in the response. The main error message stays the same; extensions just add more info. This process ensures clear and structured error reporting in GraphQL APIs.