Skip to content

Commit 2118a80

Browse files
EmrysMyrddingithub-actions[bot]dependabot[bot]renovate[bot]theguild-bot
authored
feat(executor): add schema coordinate to graphql errors (#7588)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: TheGuildBot <[email protected]>
1 parent 4638f7b commit 2118a80

14 files changed

+391
-64
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-tools/graphql-file-loader": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`@graphql-tools/[email protected]` ↗︎](https://www.npmjs.com/package/@graphql-tools/import/v/7.1.7) (from `7.1.6`, in `dependencies`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-tools/import": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`@theguild/federation-composition@^0.21.0` ↗︎](https://www.npmjs.com/package/@theguild/federation-composition/v/0.21.0) (from `^0.20.2`, in `dependencies`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-tools/node-require": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`@graphql-tools/[email protected]` ↗︎](https://www.npmjs.com/package/@graphql-tools/graphql-file-loader/v/8.1.7) (from `8.1.6`, in `dependencies`)

.changeset/floppy-women-poke.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
'@graphql-tools/executor': minor
3+
'@graphql-tools/utils': minor
4+
---
5+
6+
Add optional schema coordinate in error extensions. This extension allows to precisely identify the
7+
source of the error by automated tools like tracing or monitoring.
8+
9+
This new feature is opt-in, you have to enable it using `schemaCoordinateInErrors` executor option.
10+
11+
**Caution:** This feature, when enabled, will expose information about your schema. If you need to
12+
keep your schema private and secret, you should strip this attribute at serialization time before
13+
sending errors to the client.
14+
15+
```ts
16+
import { parse } from 'graphql'
17+
import { normalizedExecutor } from '@graphql-tools/executor'
18+
import { getSchemaCoordinate } from '@graphql-tools/utils'
19+
import schema from './schema'
20+
21+
const result = await normalizedExecutor({
22+
schema,
23+
document: parse(`...`),
24+
schemaCoordinateInErrors: true // enable adding schema coordinate to graphql errors
25+
})
26+
27+
if (result.errors) {
28+
for (const error of result.errors) {
29+
console.log('Error in resolver ', error.coordinate, ':', error.message)
30+
// or with `getSchemaCoordinate` util, to workaround types if needed
31+
console.log('Error in resolver', getSchemaCoordinate(error), ':', error.message)
32+
}
33+
}
34+
```

.github/workflows/pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
permissions:
2020
contents: read
2121
id-token: write
22+
pull-requests: write
2223
uses: the-guild-org/shared-config/.github/workflows/release-snapshot.yml@v1
2324
if: ${{ github.event.pull_request.title != 'Upcoming Release Changes' }}
2425
with:

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/executor/src/execution/__tests__/executor-test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,4 +1271,92 @@ describe('Execute: Handles basic execution tasks', () => {
12711271
const [error] = result.errors!;
12721272
expect(error.originalError?.name).toBe('Error');
12731273
});
1274+
1275+
it('should properly add schema coordinate on errors only if enabled', () => {
1276+
const schema = new GraphQLSchema({
1277+
query: new GraphQLObjectType({
1278+
name: 'Query',
1279+
fields: {
1280+
foo: {
1281+
type: GraphQLInt,
1282+
resolve: () => {
1283+
throw new Error('Test Error');
1284+
},
1285+
},
1286+
},
1287+
}),
1288+
});
1289+
1290+
const document = parse('{ foo }');
1291+
1292+
expect(
1293+
executeSync({
1294+
schema,
1295+
document,
1296+
schemaCoordinateInErrors: true,
1297+
}).errors![0].coordinate,
1298+
).toBe('Query.foo');
1299+
1300+
expect(
1301+
executeSync({
1302+
schema,
1303+
document,
1304+
schemaCoordinateInErrors: false,
1305+
}).errors![0].coordinate,
1306+
).toBe(undefined);
1307+
});
1308+
1309+
it('should properly add schema coordinate even with abstract types', () => {
1310+
const I = new GraphQLInterfaceType({ name: 'I', fields: { f: { type: GraphQLString } } });
1311+
const A = new GraphQLObjectType({
1312+
name: 'A',
1313+
interfaces: [I],
1314+
fields: {
1315+
f: {
1316+
type: GraphQLString,
1317+
resolve: () => {
1318+
throw new Error('Error A');
1319+
},
1320+
},
1321+
},
1322+
});
1323+
1324+
const B = new GraphQLObjectType({ name: 'B', fields: { b: { type: GraphQLString } } });
1325+
1326+
const schema = new GraphQLSchema({
1327+
types: [I, A, B],
1328+
query: new GraphQLObjectType({
1329+
name: 'Query',
1330+
fields: {
1331+
i: {
1332+
type: I,
1333+
resolve: () => ({ __typename: 'A' }),
1334+
},
1335+
u: {
1336+
type: new GraphQLUnionType({
1337+
name: 'U',
1338+
types: [A, B],
1339+
}),
1340+
resolve: () => ({ __typename: 'A' }),
1341+
},
1342+
},
1343+
}),
1344+
});
1345+
1346+
expect(
1347+
executeSync({
1348+
schema,
1349+
document: parse('{ i { f } }'),
1350+
schemaCoordinateInErrors: true,
1351+
}).errors![0].coordinate,
1352+
).toBe('A.f');
1353+
1354+
expect(
1355+
executeSync({
1356+
schema,
1357+
document: parse('{ u { ...on A { f } } }'),
1358+
schemaCoordinateInErrors: true,
1359+
}).errors![0].coordinate,
1360+
).toBe('A.f');
1361+
});
12741362
});

0 commit comments

Comments
 (0)