Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-feet-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@envelop/core': minor
---

Handle incremental execution errors in useErrorHandler
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@
},
"devDependencies": {
"@graphql-tools/schema": "10.0.25",
"@graphql-tools/utils": "10.9.1",
"@repeaterjs/repeater": "3.0.6",
"@graphql-tools/executor": "^1.1.0",
"@graphql-tools/utils": "10.0.11",
"graphql": "16.8.1",
"typescript": "5.9.2"
},
Expand All @@ -79,4 +80,4 @@
"typescript": {
"definition": "dist/typings/index.d.ts"
}
}
}
23 changes: 19 additions & 4 deletions packages/core/src/plugins/use-error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { DefaultContext, ExecutionResult, Plugin, TypedExecutionArgs } from '@envelop/types';
import {
DefaultContext,
ExecutionResult,
IncrementalExecutionResult,
Plugin,
TypedExecutionArgs,
} from '@envelop/types';
import { handleStreamOrSingleExecutionResult } from '../utils.js';
import { isGraphQLError, SerializableGraphQLErrorLike } from './use-masked-errors.js';

Expand All @@ -13,15 +19,24 @@ export type ErrorHandler = ({
}) => void;

type ErrorHandlerCallback<ContextType> = {
result: ExecutionResult;
result: ExecutionResult | IncrementalExecutionResult;
args: TypedExecutionArgs<ContextType>;
};

const makeHandleResult =
<ContextType extends Record<any, any>>(errorHandler: ErrorHandler) =>
({ result, args }: ErrorHandlerCallback<ContextType>) => {
if (result.errors?.length) {
errorHandler({ errors: result.errors, context: args, phase: 'execution' });
const errors = result.errors ? [...result.errors] : [];
if ('incremental' in result && result.incremental) {
for (const increment of result.incremental) {
if (increment.errors) {
errors.push(...increment.errors);
}
}
}

if (errors.length) {
errorHandler({ errors, context: args, phase: 'execution' });
}
};

Expand Down
37 changes: 36 additions & 1 deletion packages/core/test/plugins/use-error-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useExtendContext } from '@envelop/core';
import * as GraphQLJS from 'graphql';
import { useEngine, useExtendContext } from '@envelop/core';
import {
assertStreamExecutionValue,
collectAsyncIteratorValues,
createTestkit,
} from '@envelop/testing';
import { Plugin } from '@envelop/types';
import { normalizedExecutor } from '@graphql-tools/executor';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { createGraphQLError } from '@graphql-tools/utils';
import { Repeater } from '@repeaterjs/repeater';
Expand Down Expand Up @@ -139,4 +141,37 @@ describe('useErrorHandler', () => {
}),
);
});

it('should invoke error handler when error happens during incremental execution', async () => {
const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
directive @defer on FRAGMENT_SPREAD | INLINE_FRAGMENT

type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: () => {
throw new Error('kaboom');
},
},
},
});

const mockHandler = jest.fn();
const testInstance = createTestkit(
[
useEngine({ ...GraphQLJS, execute: normalizedExecutor, subscribe: normalizedExecutor }),
useErrorHandler(mockHandler),
],
schema,
);
const result = await testInstance.execute(`query { ... @defer { foo } }`);
assertStreamExecutionValue(result);
await collectAsyncIteratorValues(result);

expect(mockHandler).toHaveBeenCalledWith(expect.objectContaining({ phase: 'execution' }));
});
});
29 changes: 27 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading