|
| 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 | +```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 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +## Security concerns |
| 42 | + |
| 43 | +Adding a schema coordinate to errors exposes information about the schema, which can be an attack |
| 44 | +vector if you rely on the fact your schema is private and secret. |
| 45 | + |
| 46 | +This is why the `coordinate` attribute is not serialized by default, and will not be exposed to |
| 47 | +clients. |
| 48 | + |
| 49 | +If you want to send this information to client, override either each `toJSON` error's method, or add |
| 50 | +a dedicated extension. |
| 51 | + |
| 52 | +```ts |
| 53 | +import { GraphQLError } from 'graphql' |
| 54 | +import { createYoga, maskError, useErrorCoordinate } from 'graphql-yoga' |
| 55 | +import { schema } from './schema' |
| 56 | + |
| 57 | +export const yoga = createYoga({ |
| 58 | + schema, |
| 59 | + plugins: [useErrorCoordinate()], |
| 60 | + maskedErrors: { |
| 61 | + isDev: process.env['NODE_ENV'] === 'development', // when `isDev` is true, errors are not masked |
| 62 | + maskError: (error, message, isDev) => { |
| 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) |
| 75 | + } |
| 76 | + } |
| 77 | +}) |
| 78 | +``` |
0 commit comments