Skip to content

Commit

Permalink
Merge pull request #171 from kjones1876/master
Browse files Browse the repository at this point in the history
fix: sync fetch errors were not passed back to query requests when batching
  • Loading branch information
remorses authored Mar 28, 2024
2 parents fbc8655 + 0dbd700 commit 8340fe0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cli/src/runtime/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
});
}

/**
Expand Down
37 changes: 37 additions & 0 deletions integration-tests/tests/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;

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
Expand Down

0 comments on commit 8340fe0

Please sign in to comment.