From 0dbd700e7a761ba61adc466b6e6e280f6df2e2b8 Mon Sep 17 00:00:00 2001 From: Kieran Jones Date: Tue, 26 Mar 2024 14:35:41 +0000 Subject: [PATCH] fix: sync fetch errors were not passed back to query requests when batching --- cli/src/runtime/batcher.ts | 14 +++++++++-- integration-tests/tests/execution.ts | 37 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cli/src/runtime/batcher.ts b/cli/src/runtime/batcher.ts index 646a7b01..449f79f6 100644 --- a/cli/src/runtime/batcher.ts +++ b/cli/src/runtime/batcher.ts @@ -45,8 +45,13 @@ function dispatchQueueBatch(client: QueryBatcher, queue: Queue): void { if (batchedQuery.length === 1) { batchedQuery = batchedQuery[0] } - - client.fetcher(batchedQuery).then((responses: any) => { + (() => { + try { + return client.fetcher(batchedQuery); + } catch(e) { + return Promise.reject(e); + } + })().then((responses: any) => { if (queue.length === 1 && !Array.isArray(responses)) { if (responses.errors && responses.errors.length) { queue[0].reject( @@ -71,6 +76,11 @@ function dispatchQueueBatch(client: QueryBatcher, queue: Queue): void { } } }) + .catch((e) => { + for (let i = 0; i < queue.length; i++) { + queue[i].reject(e) + } + }); } /** diff --git a/integration-tests/tests/execution.ts b/integration-tests/tests/execution.ts index cd1069f7..dbcab086 100644 --- a/integration-tests/tests/execution.ts +++ b/integration-tests/tests/execution.ts @@ -610,6 +610,43 @@ describe('execute queries', async function () { assert.strictEqual(headersCalledNTimes, 2) }), ) + + + it( + 'raises synchronously thrown fetch errors in batch mode', + withServer(async () => { + let client = createClient({ + url: URL, + batch: true, + fetch: () => { + return fetch('http://not.a.domain.google.com/'); + } + }) + + const makeCall = () => client.query({ + repository: { + __args: { + name: 'genql', + }, + createdAt: true, + }, + }) + + await assert.rejects(makeCall, (err) => { + if (!(err instanceof TypeError)) { + assert.fail('err is not Error'); + } + const cause = err.cause as Record; + + assert.strictEqual(err.name, 'TypeError'); + assert.strictEqual(cause.code, 'ENOTFOUND'); + assert.strictEqual(cause.syscall, 'getaddrinfo'); + return true; + }, 'failed to throw') + + }), + ) + }) // // TODO apollo server changed everything in version 3 and i don't have time to fix their shit