-
Notifications
You must be signed in to change notification settings - Fork 583
feat(graphql-yoga): add support for experimental error coordinate #4288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b3d307b
c7af7e0
1961ad0
c00085d
714bf74
e5cca5a
b137c8e
5f21de0
f407246
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| --- | ||
| 'graphql-yoga': minor | ||
| --- | ||
|
|
||
| Add experimental support for | ||
| [`coordinate` error attribute proposal](https://github.com/graphql/graphql-spec/pull/1200). | ||
|
|
||
| The `coordinate` attribute indicates the coordinate in the schema of the resolver which experienced | ||
| the errors. It allows for an easier error source identification than with the `path` which can be | ||
| difficult to walk, or even lead to unsolvable ambiguities when using Union or Interface types. | ||
|
|
||
| ## Usage | ||
|
|
||
| Since this is experimental, it has to be explicitly enabled by adding the appropriate plugin to the | ||
| Yoga instance: | ||
|
|
||
| ```ts | ||
| import { createYoga, useErrorCoordinate } from 'graphql-yoga' | ||
| import { schema } from './schema' | ||
|
|
||
| export const yoga = createYoga({ | ||
| schema, | ||
| plugins: [useErrorCoordinate()] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we enable it by default, since error masking will strip it out anyways?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, since it is experimental. If the user has it's own masking function, it could leak if it is not updated ^^' |
||
| }) | ||
| ``` | ||
|
|
||
| Once enabled, located errors will gain the `coordinate` attribute: | ||
|
|
||
| ```ts | ||
| const myPlugin = { | ||
| onExecutionResult({ result }) { | ||
| if (result.errors) { | ||
| for (const error of result.errors) { | ||
| console.log('Error at', error.coordinate, ':', error.message) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Security concerns | ||
|
|
||
| Adding a schema coordinate to errors exposes information about the schema, which can be an attack | ||
| vector if you rely on the fact your schema is private and secret. | ||
|
|
||
| This is why the `coordinate` attribute is not serialized by default, and will not be exposed to | ||
| clients. | ||
|
|
||
| If you want to send this information to client, override either each `toJSON` error's method, or add | ||
| a dedicated extension. | ||
|
|
||
| ```ts | ||
| import { GraphQLError } from 'graphql' | ||
| import { createYoga, maskError, useErrorCoordinate } from 'graphql-yoga' | ||
| import { schema } from './schema' | ||
|
|
||
| export const yoga = createYoga({ | ||
| schema, | ||
| plugins: [useErrorCoordinate()], | ||
| maskedErrors: { | ||
| isDev: process.env['NODE_ENV'] === 'development', // when `isDev` is true, errors are not masked | ||
| maskError: (error, message, isDev) => { | ||
| if (error instanceof GraphQLError) { | ||
| error.toJSON = () => { | ||
| // Get default graphql serialized error representation | ||
| const json = GraphQLError.prototype.toJSON.apply(error) | ||
| // Manually add the coordinate attribute. You can also use extensions instead. | ||
| json.coordinate = error.coordinate | ||
| return json | ||
| } | ||
| } | ||
|
|
||
| // Keep the default error masking implementation | ||
| return maskError(error, message, isDev) | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { ExecutionArgs } from '@graphql-tools/executor'; | ||
| import { Plugin } from './types.js'; | ||
|
|
||
| export function useErrorCoordinate(): Plugin { | ||
| return { | ||
| onExecute({ args }) { | ||
| (args as ExecutionArgs).schemaCoordinateInErrors = true; | ||
| }, | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍