📖 Scenario: You are building a GraphQL API for a bookstore. You want to provide clear error messages with extra details when something goes wrong, so clients can understand and handle errors better.
🎯 Goal: Create a GraphQL schema with a query that can return custom errors including additional information in the extensions field.
📋 What You'll Learn
Define a simple GraphQL schema with a book query that takes an id argument.
Create a custom error class that adds an extensions field with extra error details.
Throw the custom error when a book with the given id is not found.
Ensure the error response includes the extensions field with the custom data.
💡 Why This Matters
🌍 Real World
Custom error extensions help API clients understand exactly what went wrong and how to handle errors gracefully.
💼 Career
Many companies use GraphQL APIs and expect developers to provide clear, structured error information for better client-side error handling.
Progress0 / 4 steps
1
Define the GraphQL schema with a book query
Create a GraphQL schema string called typeDefs with a Book type having fields id (ID!), title (String!), and a Query type with a book field that takes an id argument of type ID! and returns a Book.
GraphQL
Hint
Use backticks to create a multi-line string for the schema.
2
Create a custom error class with extensions
Define a class called BookNotFoundError that extends Error. In its constructor, accept a message and set this.extensions to an object with a code property set to BOOK_NOT_FOUND and a timestamp property set to the current ISO date string.
GraphQL
Hint
Remember to call super(message) in the constructor.
3
Implement the resolver to throw the custom error
Create a resolvers object with a Query field. Inside Query, define a book function that takes parent, args, and context. Use a hardcoded array books with one book: { id: '1', title: '1984' }. Find the book by args.id. If not found, throw a BookNotFoundError with the message Book with id {args.id} not found. Otherwise, return the book.
GraphQL
Hint
Use Array.find to locate the book by id.
4
Export the schema and resolvers for server setup
Export the typeDefs and resolvers using module.exports as an object with keys typeDefs and resolvers.
GraphQL
Hint
Use module.exports = { typeDefs, resolvers } to export both.
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?