You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* begin with v3 tutorial
* inline versioning
* no tutorial in docs
* error handling
* filtering and pagination
* point to v3
* micro adjustments
* drop empty code block
* use native details tag
Copy file name to clipboardExpand all lines: website/src/pages/tutorial/basic/09-error-handling.mdx
+125-2Lines changed: 125 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -121,6 +121,9 @@ This is great, but now we never know why our request failed 🤔 and the error m
121
121
122
122
Let's change the implementation to expose the much more helpful message `Cannot post comment on non-existing link with id 'X'.` instead of `Unexpected error.`.
123
123
124
+
<details>
125
+
<summary>v2</summary>
126
+
124
127
For doing so you will use the `GraphQLYogaError` class that is exported from the `@graphql-yoga/node` package.
125
128
126
129
Add the following catch handler for mapping a foreign key error into a `GraphQLYogaError`:
@@ -169,6 +172,61 @@ const resolvers = {
169
172
The error code `P2003` indicates a foreign key constraint error. You can learn more about it in [the Prisma documentation](https://prisma.io/docs/reference/api-reference/error-reference#p2003).
170
173
You use that code for identifying this edge case of a missing link and then throw a `GraphQLYogaError` with a useful error message instead.
171
174
175
+
</details>
176
+
177
+
<detailsopen>
178
+
<summary>v3</summary>
179
+
180
+
For doing so you will use the `GraphQLError` class that is exported from the `graphql` package.
181
+
182
+
Add the following catch handler for mapping a foreign key error into a `GraphQLError`:
183
+
184
+
```ts
185
+
// ... other imports ...
186
+
import { GraphQLError } from'graphql'
187
+
// ... other imports ...
188
+
189
+
const resolvers = {
190
+
// ... other resolver maps ...
191
+
Mutation: {
192
+
// ... other Mutation object type field resolver functions ...
193
+
async postCommentOnLink(
194
+
parent:unknown,
195
+
args: { linkId:string; body:string },
196
+
context:GraphQLContext,
197
+
) {
198
+
const comment =awaitcontext.prisma.comment
199
+
.create({
200
+
data: {
201
+
body: args.body,
202
+
linkId: parseInt(args.linkId),
203
+
},
204
+
})
205
+
.catch((err:unknown) => {
206
+
if (
207
+
errinstanceofPrismaClientKnownRequestError&&
208
+
err.code==='P2003'
209
+
) {
210
+
returnPromise.reject(
211
+
newGraphQLError(
212
+
`Cannot post comment on non-existing link with id '${args.linkId}'.`,
213
+
),
214
+
)
215
+
}
216
+
returnPromise.reject(err)
217
+
})
218
+
219
+
returncomment
220
+
},
221
+
},
222
+
}
223
+
```
224
+
225
+
The error code `P2003` indicates a foreign key constraint error. You can learn more about it in [the Prisma documentation](https://prisma.io/docs/reference/api-reference/error-reference#p2003).
226
+
You use that code for identifying this edge case of a missing link and then throw a `GraphQLError` with a useful error message instead.
227
+
228
+
</details>
229
+
172
230
Restart the server using `npm run dev` and again execute the mutation operation using GraphiQL.
173
231
174
232
```graphql
@@ -200,9 +258,9 @@ You will receive a response with the `Cannot post comment on non-existing link w
200
258
}
201
259
```
202
260
203
-
As you might have noticed, GraphQLYoga will exclude `GraphQLYogaError` errors thrown within the resolvers from masking the error masking.
261
+
As you might have noticed, GraphQL Yoga will exclude wrapped errors thrown within the resolvers from masking the error masking.
204
262
205
-
That means every time you want to expose an error to the outside world you should throw a `GraphQLYogaError` error.
263
+
That means every time you want to expose an error to the outside world you should throw such an error.
206
264
207
265
At the same time, any other unexpected error will automatically be masked from the outside world, bringing you sensible and safe defaults for a GraphQL Yoga production deployment!
208
266
@@ -306,8 +364,12 @@ There are multiple mathematical numeral systems. The implementation tries to be
306
364
307
365
Add the following code to the `src/schema.ts` file.
`Cannot post comment on non-existing link with id '${args.linkId}'.`,
454
+
),
455
+
)
456
+
}
457
+
458
+
const comment =awaitcontext.prisma.comment
459
+
.create({
460
+
data: {
461
+
body: args.body,
462
+
linkId,
463
+
},
464
+
})
465
+
.catch((err:unknown) => {
466
+
if (errinstanceofPrismaClientKnownRequestError) {
467
+
if (err.code==='P2003') {
468
+
returnPromise.reject(
469
+
newGraphQLError(
470
+
`Cannot post comment on non-existing link with id '${args.linkId}'.`,
471
+
),
472
+
)
473
+
}
474
+
}
475
+
returnPromise.reject(err)
476
+
})
477
+
returncomment
478
+
},
479
+
},
480
+
}
481
+
```
482
+
483
+
</details>
484
+
362
485
The regex `/^(\d+)$/` simply verifies that every character within the `value` is a digit. In case the value includes a non-digit character, you simply return `null` and then reject the resolver with a `GraphQLYogaError` error.
363
486
364
487
Returning `null` instead of returning `NaN` is the better choice here, as it allows nice TypeScript checks (`linkId === null`).
0 commit comments