React-apollo: getDataFromTree resolves before <Query> wrapped with React.forwardRef finish fetching data

Created on 17 Oct 2018  ·  10Comments  ·  Source: apollographql/react-apollo

Intended outcome:
I have following code:

import React, { Component } from 'react';
import { Query } from 'react-apollo';
import infoQuery from './queries/info.graphql';

const Info = React.forwardRef((props, ref) => (
  <div ref={ref}>
    <Query query={infoQuery}>
      {({ data, loading, error }) => {
        if (loading) {
          return 'Loading...';
        }

        if (error) {
          return 'Error :(';
        }

        return 'Returned info: ' + data.info;
      }}
    </Query>
  </div>
));

export default class App from Component {
  infoRef = React.createRef();

  render() {
    return (
      <Info ref={infoRef} />
    );
  }
}

Actual outcome:
If I try to server render this App component I can't see any mentions of Returned info: ... in HTML response. I see only Loading....

It looks like getDataFromTree resolves Promise before Query actually finishes.

How to reproduce the issue:
I don't know any public platform where I can place SSR example (like codesandbox but for SSR), but you can copy-paste code from above.

Version

Most helpful comment

@gregmartyn Yep, but that's a different issue. If you look at the tests that were written for this PR, none of them actually have a Query component nested within a Context.Consumer.

All 10 comments

@seb-saletes Yes, I think so. But I think this repo (react-apollo) is better place for this issue, because this problem related only to React.

This also happens when wrapping a Query component in a Context Consumer (using the new Context API).

@gregmartyn Yep, but that's a different issue. If you look at the tests that were written for this PR, none of them actually have a Query component nested within a Context.Consumer.

Ah right I see a Provider > Query > Consumer but no Provider > Consumer > Query

This issue will be fixed by #2533, I believe.

This issue will be fixed by #2533, I believe.

Did a short test, and it seems to work now, great work :+1:
Unfortunately I'm currently unavailable to write some tests, or did your PR already contain tests for the Provider > Consumer > Query scenario?

Looks like all works fine. Thanks!

Was this page helpful?
0 / 5 - 0 ratings