apollo-client-integrations: Not sending data to the client.

I am trying to integrate this with

It’s almost working, my code looks like this:

import { WrapApolloProvider } from '@apollo/client-react-streaming'
import { buildManualDataTransport } from '@apollo/client-react-streaming/manual-transport'
import { useStream } from 'react-streaming'
import { renderToString } from 'react-dom/server'
import React from 'react'

export const WrappedApolloProvider = WrapApolloProvider(
  buildManualDataTransport({
    useInsertHtml() {
      const stream = useStream()
      if (!stream) {
        return () => {}
      }
      return (callback: () => React.ReactNode) => {
        const reactNode = callback()
        const injectionsString = renderToString(reactNode)
        console.log({ reactNode, injectionsString })
        stream.injectToStream(injectionsString)
      }
    }
  })
)

Issue: the data is not sent to the client. The output of the console.log above is the following:

{
  reactNode: {
    '$$typeof': Symbol(react.transitional.element),
    type: [Function: RehydrateOnClient],
    key: null,
    props: {},
    _owner: null,
    _store: {}
  },
  injectionsString: ''
}
{
  reactNode: {
    '$$typeof': Symbol(react.transitional.element),
    type: [Function: RehydrateOnClient],
    key: null,
    props: {},
    _owner: null,
    _store: {}
  },
  injectionsString: '<script>(window[Symbol.for("ApolloSSRDataTransport")] ??= []).push({"rehydrate":{},"events":[{"type":"started","options":{"fetchPolicy":"cache-first","query":"query Dragons{dragons{name first_flight diameter{feet}launch_payload_mass{lb}}}","notifyOnNetworkStatusChange":false,"nextFetchPolicy":undefined},"id":"1"}]})</script>'
}
{
  reactNode: {
    '$$typeof': Symbol(react.transitional.element),
    type: [Function: RehydrateOnClient],
    key: null,
    props: {},
    _owner: null,
    _store: {}
  },
  injectionsString: ''
}
{
  reactNode: {
    '$$typeof': Symbol(react.transitional.element),
    type: [Function: RehydrateOnClient],
    key: null,
    props: {},
    _owner: null,
    _store: {}
  },
  injectionsString: '<script>(window[Symbol.for("ApolloSSRDataTransport")] ??= []).push({"rehydrate":{},"events":[{"type":"complete","id":"1"}]})</script>'
}

As you can see, the callback is called 4 times, but it produces only 2 script tags, with no data included. image

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Last I heard from the team they were trying to find more granular things than an injectToStream helper, but I have no idea on their current stance.

I tried to hack together my “optimal” api by just editing React sources, and have multiple attempts, if you’re interested in the exploration 😃

There’s no official guidance on when it is safe to inject - our createInjectionTransformStream is based on the implementation from Next.js in the hopes that they know what they are doing.

Generally: it seems that React might inject multiple chunks that belong together, but always does so synchronously. So waiting for the next Promise.resolve().then, setImmediate or similar should always give you a safe point in time to inject.

Thanks for your answer, that makes sense.

I’m currently working on fixing it (I’m very looking forward to vike-react-apollo) but I’m having issues because the injected chunks are being inserted in the wrong place.

Is there a rule of when it’s allowed to inject to the stream? I’ve yet to read an official guide from React about this.

PS: super cool to see more people playing with this! Please share your final result - maybe we can even add it as another integration test?

Apollo has quite a lot of goodies to make integration easy, it’s quite neat. And, yes, we’d be up for an integration test!