Skip to content

Commit

Permalink
[graphql-client] Improves error handling for missing Response.body in…
Browse files Browse the repository at this point in the history
… multipart requests
  • Loading branch information
ecbrodie committed Jan 21, 2025
1 parent f905b0f commit 4acad02
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/funny-mangos-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/graphql-client': patch
---

Better error handling for missing Response.body in multipart requests
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function createMultipartResponseAsyncInterator(

if (
!response.body?.getReader &&
!(response.body as any)![Symbol.asyncIterator]
!(response.body as any)?.[Symbol.asyncIterator]
) {
throw new Error('API multipart response did not return an iterable body', {
cause: response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,36 @@ describe('GraphQL Client', () => {
});
});

it('returns an async iterator that returns a response object with an error when Response.body is undefined', async () => {
const mockedResponse = new Response(undefined, {
status: 200,
headers: new Headers({
'Content-Type': 'multipart/mixed; boundary=graphql',
}),
});
Object.defineProperty(mockedResponse, 'body', {
value: undefined,
writable: true,
});

fetchMock.mockResolvedValue(mockedResponse);

const responseStream = await client.requestStream(operation);

const results: any = [];
for await (const response of responseStream) {
results.push(response);
}

expect(results[0].errors).toEqual({
message:
'GraphQL Client: API multipart response did not return an iterable body',
networkStatusCode: 200,
response: mockedResponse,
});
expect(results[0].data).toBeUndefined();
});

it('returns an async iterator that returns a response object with no data value and a JSON parsing error if the returned data is a malformed JSON', async () => {
const multipleResponsesArray = [
`
Expand Down

0 comments on commit 4acad02

Please sign in to comment.