|
| 1 | +--- |
| 2 | +'graphql-yoga': minor |
| 3 | +--- |
| 4 | + |
| 5 | +Add experimental support for |
| 6 | +[`coordinate` error attribute proposal](https://github.com/graphql/graphql-spec/pull/1200). |
| 7 | + |
| 8 | +The `coordinate` attribute indicates the coordinate in the schema of the resolver which experienced |
| 9 | +the errors. It allows for an easier error source identification than with the `path` which can be |
| 10 | +difficult to walk, or even lead to unsolvable ambiguities when using Union or Interface types. |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +Since this is experimental, it has to be explicitly enabled by adding the appropriate plugin to the |
| 15 | +Yoga instance: |
| 16 | + |
| 17 | +```ts |
| 18 | +import { createYoga, useErrorCoordinate } from 'graphql-yoga' |
| 19 | +import { schema } from './schema' |
| 20 | + |
| 21 | +export const yoga = createYoga({ |
| 22 | + schema, |
| 23 | + plugins: [useErrorCoordinate()] |
| 24 | +}) |
| 25 | +``` |
| 26 | + |
| 27 | +Once enabled, located errors will gain the `coordinate` attribute: |
| 28 | + |
| 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" |
| 38 | + } |
| 39 | + ] |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Security concerns |
| 44 | + |
| 45 | +Adding a schema coordinate to errors exposes information about the schema, which can be an attack |
| 46 | +vector if you rely on the fact your schema is private and secret. |
| 47 | + |
| 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. |
| 51 | + |
| 52 | +You can customize error masking by providing the `errorMasking` option: |
| 53 | + |
| 54 | +```ts |
| 55 | +import { createYoga, useErrorCoordinate } from 'graphql-yoga' |
| 56 | +import { schema } from './schema' |
| 57 | + |
| 58 | +export const yoga = createYoga({ |
| 59 | + schema, |
| 60 | + plugins: [useErrorCoordinate()], |
| 61 | + maskedErrors: { |
| 62 | + isDev: process.env['NODE_ENV'] === 'development', // when `isDev` is true, errors are not masked |
| 63 | + maskError: (error, message, isDev) => { |
| 64 | + //... you can provide your own masking logic, to always expose `coordinate` for example. |
| 65 | + } |
| 66 | + } |
| 67 | +}) |
| 68 | +``` |
0 commit comments