apollographql / apollo-link

:link: Interface for fetching and modifying control flow of GraphQL requests
https://www.apollographql.com/docs/link/
MIT License
1.44k stars 344 forks source link

Fallback to chunked XHR streaming if browser does not support ReadableStream API for `@defer` #753

Open clarencenpy opened 6 years ago

clarencenpy commented 6 years ago

This issue is related to ongoing work to add support for @defer (#714).

Right now, when we detect that the browser does not support the ReadableStream API used to read a streaming response for a deferred query, it defaults back to calling response.text() which waits for the full multipart HTTP response to be ready - meaning that the benefits of @defer are lost.

Ideally, we want to keep polyfilling to a minimum so that we do not bloat the bundle for Apollo Client. Instead, we could do something like chunked XHR streaming.

rdy commented 6 years ago

The chunked XHR streaming looks like it will work for @defer. I've implemented against it before and it should save the bundle size places where the browser does not support the readable stream response in fetch or text-encoding polyfill which is quite large.

The only thing I'm not sure about is how complete the XMLHttpRequest implementation in ReactNative is, it would warrant a test to make sure it also supports the chunked response before going down this path.

robrichard commented 6 years ago

@clarencenpy I'm currently working on implementing apollo-server's @defer support in our Relay stack. As part of that, I wrote fetch-multipart-graphql, which includes fallback logic to use chunked XHR streaming. I've tested it in the latest Chrome, Firefox, Safari, and IE 11 and it is all working correctly with the latest apollo-server alpha. The only polyfill required is a TextDecoder/TextEncoder with utf-8 support which is small. I figured this might be a helpful starting point for getting cross-browser support in apollo-link.