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 22, 2025
1 parent 7e679cd commit d3531c5
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. Instead of being "Cannot read properties of undefined (reading 'Symbol(Symbol.asyncIterator)')", it will now be the more useful and accurate message "API multipart response did not return an iterable body".
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 d3531c5

Please sign in to comment.