Skip to content

Commit 6a26d65

Browse files
committed
feat(executor): extensions.code
1 parent 54751cf commit 6a26d65

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

.changeset/funny-berries-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/executor': minor
3+
---
4+
5+
Use `extensions.code` to specify errors

packages/executor/src/execution/execute.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ export function buildExecutionContext<TData = any, TVariables = any, TContext =
438438
return [
439439
createGraphQLError(
440440
'Must provide operation name if query contains multiple operations.',
441+
{
442+
extensions: {
443+
code: 'OPERATION_RESOLUTION_FAILURE',
444+
},
445+
},
441446
),
442447
];
443448
}
@@ -453,9 +458,21 @@ export function buildExecutionContext<TData = any, TVariables = any, TContext =
453458

454459
if (operation == null) {
455460
if (operationName != null) {
456-
return [createGraphQLError(`Unknown operation named "${operationName}".`)];
461+
return [
462+
createGraphQLError(`Unknown operation named "${operationName}".`, {
463+
extensions: {
464+
code: 'OPERATION_RESOLUTION_FAILURE',
465+
},
466+
}),
467+
];
457468
}
458-
return [createGraphQLError('Must provide an operation.')];
469+
return [
470+
createGraphQLError('Must provide an operation.', {
471+
extensions: {
472+
code: 'OPERATION_RESOLUTION_FAILURE',
473+
},
474+
}),
475+
];
459476
}
460477

461478
// FIXME: https://github.com/graphql/graphql-js/issues/2203
@@ -1532,19 +1549,15 @@ export function subscribe<TData = any, TVariables = any, TContext = any>(
15321549

15331550
// Return early errors if execution context failed.
15341551
if (!('schema' in exeContext)) {
1552+
for (const error of exeContext) {
1553+
// @ts-expect-error - We are intentionally modifying the errors
1554+
const extensions = (error.extensions ||= {});
1555+
const httpExtensions = (extensions['http'] ||= {});
1556+
httpExtensions.status = 400;
1557+
error.extensions['code'] = 'BAD_USER_INPUT';
1558+
}
15351559
return {
1536-
errors: exeContext.map(e => {
1537-
Object.defineProperty(e, 'extensions', {
1538-
value: {
1539-
...e.extensions,
1540-
http: {
1541-
...(e.extensions?.['http'] || {}),
1542-
status: 400,
1543-
},
1544-
},
1545-
});
1546-
return e;
1547-
}),
1560+
errors: exeContext,
15481561
};
15491562
}
15501563

0 commit comments

Comments
 (0)