Skip to content
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

feat(useErrorHandler): handle incremental execution errors #2113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -60,6 +60,7 @@
"tslib": "^2.5.0"
},
"devDependencies": {
"@graphql-tools/executor": "^1.1.0",
"@graphql-tools/schema": "10.0.2",
"@graphql-tools/utils": "10.0.11",
"@repeaterjs/repeater": "3.0.5",
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';

@@ -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' });
}
};

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';
@@ -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' }));
});
});
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

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