Skip to content

Commit 9b91197

Browse files
committed
fix: split results and add failing test scenarios
1 parent 9fc1378 commit 9b91197

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/batch-execute/src/splitResult.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export function splitResult(
1010
{ data, errors }: ExecutionResult,
1111
numResults: number,
1212
): Array<ExecutionResult> {
13-
const splitResults = new Array<ExecutionResult>(numResults);
13+
const splitResults: ExecutionResult[] = Array.from(
14+
{ length: numResults },
15+
() => ({ data: null }),
16+
);
1417

1518
if (data) {
1619
for (const prefixedKey in data) {

packages/batch-execute/tests/batchExecute.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ describe('batch execution', () => {
2222
field1: String
2323
field2: String
2424
field3(input: String): String
25+
fieldNonNullable: String!
2526
boom(message: String): String
27+
boomNonNullable(message: String): String!
2628
boomWithPath(message: String, path: [String]): String
2729
extension: String
2830
widget: Widget
@@ -37,6 +39,7 @@ describe('batch execution', () => {
3739
field2: () => '2',
3840
field3: (_root, { input }) => String(input),
3941
boom: (_root, { message }) => new Error(message),
42+
boomNonNullable: (_root, { message }) => new Error(message),
4043
boomWithPath: (_root, { message, path }) =>
4144
createGraphQLError(message, { path }),
4245
extension: () => createGraphQLError('boom', { extensions }),
@@ -231,6 +234,35 @@ describe('batch execution', () => {
231234
expect(executorCalls).toEqual(1);
232235
});
233236

237+
it('returns error for the failing non-nullable field', async () => {
238+
const [first] = (await Promise.all([
239+
batchExec({
240+
document: parse(
241+
'{ fieldNonNullable boomNonNullable(message: "failed") }',
242+
),
243+
}),
244+
])) as ExecutionResult[];
245+
246+
expect(first?.data).toBeNull();
247+
expect(first?.errors?.length).toEqual(1);
248+
expect(first?.errors?.[0]?.message).toMatch(/failed/);
249+
expect(executorCalls).toEqual(1);
250+
});
251+
252+
it('returns error for the failing non-nullable field across multiple executions', async () => {
253+
const [first, second] = (await Promise.all([
254+
batchExec({ document: parse('{ fieldNonNullable }') }),
255+
batchExec({ document: parse('{ boomNonNullable(message: "failed") }') }),
256+
])) as ExecutionResult[];
257+
258+
expect(first?.data).toBeNull();
259+
expect(first?.errors).toBeUndefined();
260+
expect(second?.data).toBeNull();
261+
expect(second?.errors?.length).toEqual(1);
262+
expect(second?.errors?.[0]?.message).toMatch(/failed/);
263+
expect(executorCalls).toEqual(1);
264+
});
265+
234266
it('pathed errors contain extensions', async () => {
235267
const [first] = (await Promise.all([
236268
batchExec({ document: parse('{ extension }') }),

0 commit comments

Comments
 (0)