How does error handling work in GraphQL?
Answer
GraphQL's error model is unique: the response can contain both data and errors simultaneously. The response is always HTTP 200 with a JSON body containing data and/or errors arrays. Partial success is common — if one resolver fails, GraphQL returns the error for that field but still resolves all other fields. Each error in the errors array has a message, locations, path, and optional extensions for machine-readable codes. For client-facing errors (validation, auth), throw domain-specific errors in resolvers with an extensions.code. Unexpected server errors should be masked with a generic message to avoid leaking implementation details.
Previous
What is the difference between Input types and Object types in GraphQL mutations?
Next
What is the difference between union types and interface types in GraphQL?
More GraphQL Questions
View all →- Intermediate What is the N+1 problem in GraphQL and how does DataLoader solve it?
- Intermediate What are persisted queries in GraphQL?
- Intermediate What is the difference between schema-first and code-first GraphQL approaches?
- Intermediate What are the pagination strategies in GraphQL?
- Intermediate How is authentication handled in GraphQL?