Skip to content

Commit

Permalink
feat(executor): extensions.code (#6868)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Feb 18, 2025
1 parent 8568c88 commit b53941c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-berries-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/executor': minor
---

Use `extensions.code` to specify errors
41 changes: 27 additions & 14 deletions packages/executor/src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ export function buildExecutionContext<TData = any, TVariables = any, TContext =
return [
createGraphQLError(
'Must provide operation name if query contains multiple operations.',
{
extensions: {
code: 'OPERATION_RESOLUTION_FAILURE',
},
},
),
];
}
Expand All @@ -453,9 +458,21 @@ export function buildExecutionContext<TData = any, TVariables = any, TContext =

if (operation == null) {
if (operationName != null) {
return [createGraphQLError(`Unknown operation named "${operationName}".`)];
return [
createGraphQLError(`Unknown operation named "${operationName}".`, {
extensions: {
code: 'OPERATION_RESOLUTION_FAILURE',
},
}),
];
}
return [createGraphQLError('Must provide an operation.')];
return [
createGraphQLError('Must provide an operation.', {
extensions: {
code: 'OPERATION_RESOLUTION_FAILURE',
},
}),
];
}

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

// Return early errors if execution context failed.
if (!('schema' in exeContext)) {
for (const error of exeContext) {
// @ts-expect-error - We are intentionally modifying the errors
const extensions = (error.extensions ||= {});
const httpExtensions = (extensions['http'] ||= {});
httpExtensions.status = 400;
error.extensions['code'] = 'BAD_USER_INPUT';
}
return {
errors: exeContext.map(e => {
Object.defineProperty(e, 'extensions', {
value: {
...e.extensions,
http: {
...(e.extensions?.['http'] || {}),
status: 400,
},
},
});
return e;
}),
errors: exeContext,
};
}

Expand Down

0 comments on commit b53941c

Please sign in to comment.