1
1
---
2
2
title : Understanding GraphQL.js Errors
3
3
---
4
+ import { Callout , GitHubNoteIcon } from " nextra/components" ;
4
5
5
6
# Understanding GraphQL.js Errors
6
7
@@ -37,13 +38,22 @@ For example:
37
38
Each error object can include the following fields:
38
39
39
40
- ` message ` : A human-readable description of the error.
40
- - ` locations ` (optional): Where the error occurred in the operation.
41
+ - ` locations ` (optional): Where the error occurred in the operation document .
41
42
- ` path ` (optional): The path to the field that caused the error.
42
43
- ` extensions ` (optional): Additional error metadata, often used for error codes, HTTP status
43
44
codes or debugging information.
44
45
45
- The GraphQL specification only requires the ` message ` field. All others are optional, but
46
- recommended to help clients understand and react to errors.
46
+ <Callout type = " info" >
47
+
48
+ The GraphQL specification separates errors into two types: _ request_ errors, and
49
+ _ execution_ errors. Request errors indicate something went wrong that prevented
50
+ the GraphQL operation from executing, for example the document is invalid, and
51
+ only requires the ` message ` field. Execution errors indicate something went
52
+ wrong during execution, typically due to the result of calling a resolver, and
53
+ requires both the ` message ` and ` path ` fields to be present. All others fields
54
+ are optional, but recommended to help clients understand and react to errors.
55
+
56
+ </Callout >
47
57
48
58
## Creating and handling errors with ` GraphQLError `
49
59
@@ -81,12 +91,16 @@ Each option helps tie the error to specific parts of the GraphQL execution:
81
91
82
92
When a resolver throws an error:
83
93
84
- - If the thrown value is already a ` GraphQLError ` , GraphQL.js uses it as-is.
85
- - If it is another type of error (such as a built-in ` Error ` ), GraphQL.js wraps it into a
86
- ` GraphQLError ` .
94
+ - If the thrown value is a ` GraphQLError ` and contains the required information
95
+ ( ` path ` ), GraphQL.js uses it as-is.
96
+ - Otherwise, GraphQL.js wraps it into a ` GraphQLError ` .
87
97
88
98
This ensures that all errors returned to the client follow a consistent structure.
89
99
100
+ You may throw any type of error that makes sense in your application; throwing
101
+ ` Error ` is fine, you do not need to throw ` GraphQLError ` . However, ensure that
102
+ your errors do not reveal security sensitive information.
103
+
90
104
## How errors propagate during execution
91
105
92
106
Errors in GraphQL don't necessarily abort the entire operation. How an error affects the response
@@ -96,6 +110,7 @@ depends on the nullability of the field where the error occurs.
96
110
the error and sets the field's value to ` null ` in the ` data ` payload.
97
111
- ** Non-nullable fields** : If a resolver for a non-nullable field throws an error, GraphQL.js
98
112
records the error and then sets the nearest parent nullable field to ` null ` .
113
+ If no such nullable field exists, then the operation root will be set ` null ` (` "data": null ` ).
99
114
100
115
For example, consider the following schema:
101
116
0 commit comments