react-apollo-hooks icon indicating copy to clipboard operation
react-apollo-hooks copied to clipboard

Mutation refetchQueries has only effect if useQuery is rendered at the same time

Open phryneas opened this issue 5 years ago • 10 comments

Heyo, I've created a CodeSandbox to showcase this:

Edit react-apollo-hook-cache-bug-reproductoin

In this example, if you click on "both", put something in the form and submit it, the list updates instantly. (As I would expect it to)

But if you click on the "form"-Button, so only the form, but not the list is rendered, then fill & submit the form and then go back to the "list" view, the list is still in the old state, which means the getList query seems not to have updated.

Funny enough, when using the evaluated graphql (passing the GET_LIST constant) instead of the string getList, it seems to update. But in that case, I have to provide all variables - and if the form does not know, which variables are used in a query in a different component, that means it is impossible to trigger the query from the form.

phryneas avatar Mar 15 '19 22:03 phryneas

useMutation proxies all options to client.mutate, it looks like an issue in apollo-client.

avocadowastaken avatar Mar 19 '19 11:03 avocadowastaken

I thought so, too, but then it behaves differently depending on how it is rendered in react - and as far as I am aware, apollo-client does not know about react, does it?

Personally, I am suspecting a bug somewhere in useQuery here.

phryneas avatar Mar 19 '19 11:03 phryneas

Just for verification: Here is a non-hook version using react-apollo - everything else is the same.

Edit react-apollo-no-cache-bug-reproduction

That version is working - I really suspect this is a bug in react-apollo-hooks.

phryneas avatar Mar 26 '19 09:03 phryneas

Just ran into this same issue. Switched from using useMutation and useQuery to the <Mutation /> and <Query /> and it solved the problem, so I think @phryneas is correct that either the issue lies within react-apollo-hooks, or else react-apollo is doing its own work to paper over the issue in apollo-client.

SeanRoberts avatar Apr 04 '19 19:04 SeanRoberts

The code example should probably be more simplified.

slikts avatar Apr 11 '19 07:04 slikts

Seriously? This has three views, because two views are required to show off the bug and one is required to show that there are conditions the bug does not happen. It consists of two components (that could be straight out of the readme of this lib), a query, a mutation and one "App" to switch between the views.

What do you want to simplify there?

phryneas avatar Apr 11 '19 08:04 phryneas

after a muation, I have a list of refetch queries. I then expect the UI to update based on the refetch, but it does not. I get a 404 from GraphQL saying I didn't send variables: { ... : id } in my useQuery. Debugging shows that I am setting it, however useQuery is forgetting it. I put a useEffect and called refetch() again, and it works but I still get a 404 in my error message. useQuery is forgetting to put variables or they are out of scope when refetchQueries invokes it. My components are several functions down.

fullstackwebdev avatar Apr 16 '19 02:04 fullstackwebdev

I just tested, '@apollo/react-hooks' does not seem to have the error.

https://codesandbox.io/s/reactapollohookcachebugreproduction-h7ubh

So well, I'm switching. But this is definitely a bug of this package.

phryneas avatar May 29 '19 14:05 phryneas

@phryneas Is there documentation/discussion anywhere for @apollo/react-hooks? I'm guessing that's what we'll eventually all be using anyway but it looks pretty early stage right now.

SeanRoberts avatar May 29 '19 15:05 SeanRoberts

@phryneas Is there documentation/discussion anywhere for @apollo/react-hooks? I'm guessing that's what we'll eventually all be using anyway but it looks pretty early stage right now.

No idea. But the API is almost the same as this project, so switching over is pretty painless, and everything seems to work - which is more than I currently have.

phryneas avatar May 29 '19 16:05 phryneas