adamsoffer / next-apollo

React higher-order component for integrating Apollo Client with Next.js
MIT License
482 stars 64 forks source link

Duplicate GraphQL queries #58

Closed wayne530 closed 4 years ago

wayne530 commented 4 years ago

Hi there - new to nextjs + Apollo, so apologies in advance if there is an obvious reason/solution to this. Following the example from the README, my next pages are able to query/mutate data using GraphQL but I noticed the server seems to be making a query while compiling, then the client also makes the same query. It's not necessarily an issue for read-only operations, but some mutations aren't idempotent so the second call from the browser fails.

Is this the expected behavior? Is there a way to have the query execute in either the server or the browser for a given page, but not both? If not, is the only solution to make mutations idempotent so both requests will succeed?

Thanks in advance for any tips/pointers!

colinappnovation commented 4 years ago

Note: Do not be alarmed that you see two renders being executed. Apollo recursively traverses the React render tree looking for Apollo query components. When it has done that, it fetches all these queries and then passes the result to a cache. This cache is then used to render the data on the server side (another React render). https://www.apollographql.com/docs/react/api/react-ssr/#getdatafromtree

wayne530 commented 4 years ago

@colinappnovation thanks for the reply. To be clear, I’m not seeing multiple different queries, I’m seeing exactly two identical graphql queries on a page that in theory should only execute one. By adding a console.log, I verified the server is executing one and the client is executing one. I would expect that if the page is rendered server-side, I would only see a single query by the server, and similarly, if the page is rendered client side, I’d see a single query by the client. Seeing both feels unexpected and certainly could have unintended consequences for mutations.

wayne530 commented 4 years ago

@adamsoffer I'm wondering if you might be able to comment on the "expectedness" of what I'm seeing? Do you see the same thing with your canonical example?

adamsoffer commented 4 years ago

@wayne530 - Looks like someone got to the bottom of this a couple days ago on the next.js apollo example :) https://github.com/zeit/next.js/pull/8917

I'll look into implementing the fix here as well.

wayne530 commented 4 years ago

That's awesome news, thanks @adamsoffer for the update!

wayne530 commented 4 years ago

Hey @adamsoffer, just checking to see if you have any updates on this?

chandan-reddy-k commented 4 years ago

@adamsoffer @wayne530 was this issue resolved ? I face the same issue.

wayne530 commented 4 years ago

@chandan-reddy-k unfortunately, I don’t think it has yet.

adamsoffer commented 4 years ago

This should be fixed in v4.0.0