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

Stitched schema loses error 'extensions' when batched #5191

Merged
merged 9 commits into from
Apr 23, 2023
Merged
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/dry-boats-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/batch-execute': patch
---

fix: batched executor returns original error when it has no path
4 changes: 2 additions & 2 deletions packages/batch-execute/src/splitResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { GraphQLError } from 'graphql';

import { createGraphQLError, ExecutionResult, relocatedError } from '@graphql-tools/utils';
import { ExecutionResult, relocatedError } from '@graphql-tools/utils';

import { parseKey } from './prefix.js';

Expand Down Expand Up @@ -41,7 +41,7 @@ export function splitResult({ data, errors }: ExecutionResult, numResults: numbe
} else {
splitResults.forEach(result => {
const resultErrors = (result.errors = (result.errors || []) as GraphQLError[]);
resultErrors.push(createGraphQLError(error.message));
resultErrors.push(error);
});
}
}
Expand Down
27 changes: 26 additions & 1 deletion packages/batch-execute/tests/batchExecute.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { parse, print, OperationDefinitionNode, validate } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { createBatchingExecutor } from '@graphql-tools/batch-execute';
import { ExecutionResult, Executor } from '@graphql-tools/utils';
import { createGraphQLError, ExecutionResult, Executor, MaybeAsyncIterable } from '@graphql-tools/utils';
import { normalizedExecutor } from '@graphql-tools/executor';

describe('batch execution', () => {
let executorCalls = 0;
let executorDocument: string | undefined;
let executorVariables: any | undefined;
const extensions = { foo: 'bar' };

const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
Expand All @@ -16,6 +17,7 @@ describe('batch execution', () => {
field2: String
field3(input: String): String
boom(message: String): String
extension: String
widget: Widget
}
type Widget {
Expand All @@ -28,6 +30,7 @@ describe('batch execution', () => {
field2: () => '2',
field3: (_root, { input }) => String(input),
boom: (_root, { message }) => new Error(message),
extension: () => createGraphQLError('boom', { extensions }),
widget: () => ({ name: 'wingnut' }),
},
},
Expand Down Expand Up @@ -185,4 +188,26 @@ describe('batch execution', () => {
expect(second?.errors?.[0].message).toMatch(/notgonnawork/);
expect(executorCalls).toEqual(1);
});

it('pathed errors contain extensions', async () => {
const [first] = (await Promise.all([batchExec({ document: parse('{ extension }') })])) as ExecutionResult[];

expect(first?.errors?.length).toEqual(1);
expect(first?.errors?.[0].message).toMatch(/boom/);
expect(first?.errors?.[0].extensions).toEqual(extensions);
expect(executorCalls).toEqual(1);
});

it('non pathed errors contain extensions', async () => {
const errorExec: Executor = (): MaybeAsyncIterable<ExecutionResult> => {
return { errors: [createGraphQLError('boom', { extensions })] };
};
const batchExec = createBatchingExecutor(errorExec);

const [first] = (await Promise.all([batchExec({ document: parse('{ boom }') })])) as ExecutionResult[];

expect(first?.errors?.length).toEqual(1);
expect(first?.errors?.[0].message).toMatch(/boom/);
expect(first?.errors?.[0].extensions).toEqual(extensions);
});
});