Skip to content

Commit 9d61d75

Browse files
authored
Run onFetchDone hooks even if onFetch has ended early with a response (#8658)
1 parent 3f27b6a commit 9d61d75

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

.changeset/quiet-swans-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/utils': patch
3+
---
4+
5+
Run onFetchDone hooks even if onFetch has ended early with a response

packages/legacy/utils/src/wrapFetchWithHooks.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,8 @@ export function wrapFetchWithHooks<TContext>(
7878
onFetchDoneHooks,
7979
),
8080
function handleIterationResult() {
81-
if (response$) {
82-
return response$;
83-
}
8481
return handleMaybePromise(
85-
() => fetchFn(url, options, context, info),
82+
() => response$ || fetchFn(url, options, context, info),
8683
function (response: Response) {
8784
return handleMaybePromise(
8885
() =>

packages/legacy/utils/test/fetch-instrumentation.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ describe('Fetch instrumentation', () => {
2727
expect(await res.json()).toEqual({ hello: 'world' });
2828
expect(receivedExecutionRequest).toBe(executionRequest);
2929
});
30+
it('should call onFetchDone when onFetch ends with response early', async () => {
31+
const onFetchDoneFn = jest.fn();
32+
const early = new Response('Early response');
33+
const wrappedFetch = wrapFetchWithHooks([
34+
({ endResponse }) => {
35+
endResponse(early);
36+
return onFetchDoneFn;
37+
},
38+
]);
39+
await wrappedFetch('http://localhost:4000');
40+
expect(onFetchDoneFn).toHaveBeenCalledWith(expect.objectContaining({ response: early }));
41+
});
3042
});

0 commit comments

Comments
 (0)