From 9b91197798368e61203290d5c7281d419c2cc6b2 Mon Sep 17 00:00:00 2001 From: omahili Date: Mon, 14 Jul 2025 16:39:27 +0200 Subject: [PATCH] fix: split results and add failing test scenarios --- packages/batch-execute/src/splitResult.ts | 5 ++- .../batch-execute/tests/batchExecute.test.ts | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/batch-execute/src/splitResult.ts b/packages/batch-execute/src/splitResult.ts index 2f84ea1fe..4257a8109 100644 --- a/packages/batch-execute/src/splitResult.ts +++ b/packages/batch-execute/src/splitResult.ts @@ -10,7 +10,10 @@ export function splitResult( { data, errors }: ExecutionResult, numResults: number, ): Array { - const splitResults = new Array(numResults); + const splitResults: ExecutionResult[] = Array.from( + { length: numResults }, + () => ({ data: null }), + ); if (data) { for (const prefixedKey in data) { diff --git a/packages/batch-execute/tests/batchExecute.test.ts b/packages/batch-execute/tests/batchExecute.test.ts index 3f94394e0..9adcc6ea0 100644 --- a/packages/batch-execute/tests/batchExecute.test.ts +++ b/packages/batch-execute/tests/batchExecute.test.ts @@ -22,7 +22,9 @@ describe('batch execution', () => { field1: String field2: String field3(input: String): String + fieldNonNullable: String! boom(message: String): String + boomNonNullable(message: String): String! boomWithPath(message: String, path: [String]): String extension: String widget: Widget @@ -37,6 +39,7 @@ describe('batch execution', () => { field2: () => '2', field3: (_root, { input }) => String(input), boom: (_root, { message }) => new Error(message), + boomNonNullable: (_root, { message }) => new Error(message), boomWithPath: (_root, { message, path }) => createGraphQLError(message, { path }), extension: () => createGraphQLError('boom', { extensions }), @@ -231,6 +234,35 @@ describe('batch execution', () => { expect(executorCalls).toEqual(1); }); + it('returns error for the failing non-nullable field', async () => { + const [first] = (await Promise.all([ + batchExec({ + document: parse( + '{ fieldNonNullable boomNonNullable(message: "failed") }', + ), + }), + ])) as ExecutionResult[]; + + expect(first?.data).toBeNull(); + expect(first?.errors?.length).toEqual(1); + expect(first?.errors?.[0]?.message).toMatch(/failed/); + expect(executorCalls).toEqual(1); + }); + + it('returns error for the failing non-nullable field across multiple executions', async () => { + const [first, second] = (await Promise.all([ + batchExec({ document: parse('{ fieldNonNullable }') }), + batchExec({ document: parse('{ boomNonNullable(message: "failed") }') }), + ])) as ExecutionResult[]; + + expect(first?.data).toBeNull(); + expect(first?.errors).toBeUndefined(); + expect(second?.data).toBeNull(); + expect(second?.errors?.length).toEqual(1); + expect(second?.errors?.[0]?.message).toMatch(/failed/); + expect(executorCalls).toEqual(1); + }); + it('pathed errors contain extensions', async () => { const [first] = (await Promise.all([ batchExec({ document: parse('{ extension }') }),