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

Apollo Client 3 - optimistic response with pollInterval doesnt work. #6858

Closed
andymrussell opened this issue Aug 18, 2020 · 12 comments
Closed
Assignees
Milestone

Comments

@andymrussell
Copy link

Intended outcome:

When using optimisticResponse or using cache.writeQuery as part of a mutation, components using pollInterval don't seem to update until the payload from the mutation has been successful.

I have 2 components using the same data. 1 is polling, the other fetches the data from the cache - network.
The non polling component updates correctly, however the polling component waits from the response to come back from the API before updating.

Actual outcome:

Once, the response comes back from the server the polling component updates.

How to reproduce the issue:

Query some data using a hook:
const { data, refetch } = useGetChannelByIdQuery({ skip: !activeChannel, variables: { id: activeChannelId! }, fetchPolicy: 'cache-and-network', pollInterval: 3000, });

Create a mutation and set an optimistic response:
const onFavoriteChannel = (isFavorite: boolean) => { onFavoriteChannelMutation({ variables: { input: { id: props.channel.id, isFavorite } }, optimisticResponse: { __typename: 'Mutation', favoriteChannel: { id: props.channel.id, name: props.channel.name, isFavorite, __typename: 'Channel', }, }, }); };

OR anything with proxy.writeQuery:

Remove the pollInterval from the query, to confirm it works.

Versions

System:
OS: macOS Mojave 10.14
Binaries:
Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
npm: 6.14.6 - ~/.nvm/versions/node/v12.18.3/bin/npm
Browsers:
Chrome: 84.0.4147.125
Firefox: 28.0
Safari: 12.0
npmPackages:
@apollo/client: ^3.1.3 => 3.1.3

@benjamn
Copy link
Member

benjamn commented Aug 18, 2020

Can you try @apollo/client@3.2.0-beta.3? I have a hunch that #6854 could possibly be related to this.

@andymrussell
Copy link
Author

Just tried it, and it looks to be the same issue.

However, it looks like if I bump the polling to 30 seconds from 3 seconds, it works as expected until its first poll response comes back.
At that point it starts to fail as mentioned above.
Not sure if that helps in anyway?

@benjamn
Copy link
Member

benjamn commented Aug 25, 2020

@andymrussell Ok, I have a slightly stronger hunch that @apollo/client@3.2.0-beta.5 (which includes #6893) will help with this issue (thanks to c4d100f), if you want to try that.

@benjamn benjamn added this to the Post 3.0 milestone Aug 25, 2020
@benjamn benjamn self-assigned this Aug 25, 2020
@gastonmorixe
Copy link

@benjamn this is still broken in v3.3.13

@gastonmorixe
Copy link

btw, tried 3.4.0-beta.19 and it's broken too

@andrujoh
Copy link

andrujoh commented Mar 29, 2021

@benjamn this is still broken in v3.3.13

Same for me. For now. I am considering setting up a graphql subscription to handle it.

Edit: I solved this by not having an initial pollInterval and instead controlling it with startPolling and stopPolling. I had the problem appear when the app lost connection as I relied on optimisticResponse and cache.modify in the update function for offline functionality.
Here is part of the solution.

useEffect(() => {
  if (!isOnline) {
    stopPolling();
  } else {
    startPolling(5000);
  }
  return stopPolling();
// eslint-disable-next-line
}, [isOnline]);

@gastonmorixe
Copy link

@andrujoh good work around, I ended up going for subscriptions.

@brainkim
Copy link
Contributor

Thanks for reporting!

I attempted to reproduce the issue here but could not replicate the issue you were describing. I am probably misunderstanding the exact way your queries and mutations are set up. If you can try and create a runnable reproduction, I’ll take another look!

@brainkim
Copy link
Contributor

Might be related to #7115

@m-lautenbach
Copy link

I had the same issue, but updating from 3.1.3 to 3.4.17 fixed it for me.

As described above, optimistic updates worked until the first polling happened. Afterwards, the cache would only update with the server response of the mutation.

@hwillson hwillson added the 🔍 investigate Investigate further label May 31, 2022
@alessbell
Copy link
Member

Thanks for the update, @m-lautenbach! I'm going to close this issue in that case, but if anyone can provide a runnable reproduction with v3.4.17 or greater (via CodeSandbox or forkable GitHub repo), please drop a comment here and we will re-open. Thanks!

@alessbell alessbell closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants