Skip to content

Commit f407246

Browse files
committed
changeset
1 parent 5f21de0 commit f407246

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

.changeset/fluffy-fans-feel.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ export const yoga = createYoga({
2626

2727
Once enabled, located errors will gain the `coordinate` attribute:
2828

29-
```json
30-
{
31-
"data": null,
32-
"errors": [
33-
{
34-
"locations": [{ "column": 3, "line": 1 }],
35-
"message": "An Error Occured",
36-
"path": ["a"],
37-
"coordinate": "Query.a"
29+
```ts
30+
const myPlugin = {
31+
onExecutionResult({ result }) {
32+
if (result.errors) {
33+
for (const error of result.errors) {
34+
console.log('Error at', error.coordinate, ':', error.message)
35+
}
3836
}
39-
]
37+
}
4038
}
4139
```
4240

@@ -45,14 +43,15 @@ Once enabled, located errors will gain the `coordinate` attribute:
4543
Adding a schema coordinate to errors exposes information about the schema, which can be an attack
4644
vector if you rely on the fact your schema is private and secret.
4745

48-
This is why the `coordinate` attribute is masked from error by default when running in production
49-
mode (`NODE_ENV != 'development'`). The `coordinate` attribute is still part of the error object,
50-
but is hidden at serialization time so that it is not exposed to the client.
46+
This is why the `coordinate` attribute is not serialized by default, and will not be exposed to
47+
clients.
5148

52-
You can customize error masking by providing the `errorMasking` option:
49+
If you want to send this information to client, override either each `toJSON` error's method, or add
50+
a dedicated extension.
5351

5452
```ts
55-
import { createYoga, useErrorCoordinate } from 'graphql-yoga'
53+
import { GraphQLError } from 'graphql'
54+
import { createYoga, maskError, useErrorCoordinate } from 'graphql-yoga'
5655
import { schema } from './schema'
5756

5857
export const yoga = createYoga({
@@ -61,7 +60,18 @@ export const yoga = createYoga({
6160
maskedErrors: {
6261
isDev: process.env['NODE_ENV'] === 'development', // when `isDev` is true, errors are not masked
6362
maskError: (error, message, isDev) => {
64-
//... you can provide your own masking logic, to always expose `coordinate` for example.
63+
if (error instanceof GraphQLError) {
64+
error.toJSON = () => {
65+
// Get default graphql serialized error representation
66+
const json = GraphQLError.prototype.toJSON.apply(error)
67+
// Manually add the coordinate attribute. You can also use extensions instead.
68+
json.coordinate = error.coordinate
69+
return json
70+
}
71+
}
72+
73+
// Keep the default error masking implementation
74+
return maskError(error, message, isDev)
6575
}
6676
}
6777
})

0 commit comments

Comments
 (0)