Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server rehydration cause first client query fail #759

Closed
fikriauliya opened this issue Oct 11, 2016 · 2 comments
Closed

Server rehydration cause first client query fail #759

fikriauliya opened this issue Oct 11, 2016 · 2 comments
Labels

Comments

@fikriauliya
Copy link

fikriauliya commented Oct 11, 2016

My app has main Redux store which Apollo attached to, with store rehydration and server side rendering.

When the page is first loaded (Server side rendering), apollo/queries/0 has the following initial value (which seems valid):

screen shot 2016-10-11 at 4 58 50 pm

The query is as follow ($page = 1):

apollo.query({
  variables: translatedQueries,
  query: gql`
    query Search($page:Int) {
      search(page: $page)
      {
        products {
          id
          name
        }
      }
    }
` })
.then(response => {console.log(response.data.search); response.data.search}

Problem

When the same query is run again on client but with different $page variable (i.e. move to 2nd page), apollo.query returns prematurely after APOLLO_QUERY_INIT but before APOLLO_QUERY_STOP and APOLLO_QUERY_RESULT. And the result printed from console.log above is wrong: the same as the one on the first page.

Moreover I think it's weird thatAPOLLO_QUERY_INIT modify apollo/queries/0/variables/page from 1 to 2 (shouldn't it just append new queries: apollo/queries/1 instead of updating the existing one?).
screen shot 2016-10-11 at 5 10 29 pm

More insight

Without refreshing the page, the same query is run again with even different $page variable (i.e. move to 3rd page). Now it prints correct result. Subsequent query also works correctly

@fikriauliya fikriauliya changed the title Server rehydration data cause first client query call to fail Server rehydration cause first client query fail Oct 11, 2016
@stubailo
Copy link
Contributor

Thanks for reporting. The solution is to only include the data field on the Apollo store when doing hydration. We should make that happen automatically when using react-apollo, and document it better.

Can you try the fix of deleting every field except data on the server before hydrating?

@fikriauliya
Copy link
Author

fikriauliya commented Oct 13, 2016

Thank you for the suggestion. That's exactly what I did yesterday, patchy solution though but it solves the problem.
The drawback of that approach: refetch happen when navigating back to the first page (which had been fetched by server pre-rendering).

I don't really understand the whole code, but I guess the solution is to change the following in QueryManager.ts? => private idCounter = <id from store>

UPDATE: navigating back to first page uses the local cache. Previously it wasn't the case on me because I delete state.apollo, not just delete state.apollo.queries

For those experiencing the same problem, my solution is:

  delete state.apollo.queries
  return `
    <script>
      window.__INITIAL_STATE__ = ${serialize(state)};
    </script>
  `;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants