[graphql-client] Improves error handling for missing Response.body in multipart requests #1998
+36
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WHY are these changes introduced?
This fix was motived by development of an internal library that serves as a consumer of the graphql-client package.
We detected instances of errors in multipart GraphQL requests where, for some reason,
Response.body
was undefined despite a 200 OK response. This seems to occur mostly (if not always) on frontends that use a customwindow.fetch
implementation.The symptom of this error would be the following message:
Cannot read properties of undefined (reading 'Symbol(Symbol.asyncIterator)')
.I deduced this error to be caused by a check in
createMultipartResponseAsyncInterator()
where there is an assumption, viaresponse.body![Symbol.asyncIterator]
, thatresponse.body
is always defined, especially for responses with a truthyresponse.ok
value. Clearly this is not a safe assumption to make, especially in web environments where the fetch implementation acts in unexpected ways.WHAT is this pull request doing?
By using optional chaining instead of the non-null assertion operator, we avoid an unexpected behaviour from attempting to read a property from an undefined
response.body
and instead fall back to throwing the existing error message, which I believe is indeed the more expected result of this situation:API multipart response did not return an iterable body
.Type of change
Checklist
pnpm changeset
to create a draft changelog entry (do NOT update theCHANGELOG.md
files manually)