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

fix: sync fetch errors were not passed back to query requests when batching #171

Merged
merged 1 commit into from
Mar 28, 2024
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
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