-
Notifications
You must be signed in to change notification settings - Fork 588
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
24990ce
feat(graphql-yoga): add support for experimental error coordinate
EmrysMyrddin aaef213
prettier
EmrysMyrddin 39432ef
changeset
EmrysMyrddin 397c488
fix tests, and add normal error
EmrysMyrddin 9338134
remove only
EmrysMyrddin d1baeee
export useErrorCorrdinate plugin
EmrysMyrddin b0173f3
use toJSON to maske coordinates
EmrysMyrddin c4da092
coordinate is never serialized, but should apear in error instance
EmrysMyrddin 6842aaa
changeset
EmrysMyrddin 38f5958
update to latest graphql-tools
EmrysMyrddin 0620f35
chore(dependencies): updated changesets for modified dependencies
github-actions[bot] 816e187
update lockfile
EmrysMyrddin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
.changeset/@graphql-yoga_plugin-apollo-usage-report-4288-dependencies.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@graphql-yoga/plugin-apollo-usage-report": patch | ||
| --- | ||
| dependencies updates: | ||
| - Updated dependency [`@graphql-tools/utils@^10.11.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.11.0) (from `^10.9.1`, in `dependencies`) |
5 changes: 5 additions & 0 deletions
5
.changeset/@graphql-yoga_plugin-defer-stream-4288-dependencies.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@graphql-yoga/plugin-defer-stream": patch | ||
| --- | ||
| dependencies updates: | ||
| - Updated dependency [`@graphql-tools/utils@^10.11.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.11.0) (from `^10.6.1`, in `dependencies`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@graphql-yoga/plugin-sofa": patch | ||
| --- | ||
| dependencies updates: | ||
| - Updated dependency [`@graphql-tools/utils@^10.11.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.11.0) (from `^10.3.2`, in `dependencies`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()] | ||
|
n1ru4l marked this conversation as resolved.
|
||
| }) | ||
| ``` | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "graphql-yoga": patch | ||
| --- | ||
| dependencies updates: | ||
| - Updated dependency [`@graphql-tools/executor@^1.5.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor/v/1.5.0) (from `^1.4.0`, in `dependencies`) | ||
| - Updated dependency [`@graphql-tools/utils@^10.11.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.11.0) (from `^10.6.2`, in `dependencies`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
😍