apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

Fetch policy of no-cache on polling useQuery is ignored, cache is updated

Open jmtimko5 opened this issue 3 years ago • 3 comments

Intended outcome: useQuery hook that is polling with a fetchPolicy: no-cache should not update the cache. Actual outcome: The cache is updated with the query results from polling.

How to reproduce the issue: I have the following simple query below which fetches ID and one field the SQN. Document

const LOAD_DASHBOARD = gql`
	query LoadDashboard($id: ID!) {
		dashboardById(id: $id) {
			id
			sqn
		}
	}
`;

Code

const { data, startPolling, stopPolling } = useQuery<Query>(gql, {
		fetchPolicy: "no-cache",
		variables: {
			id,
		},
	});

If I call startPolling the next time the query is run the cache is updated when the call issue from the polling is executed, despite the fetch policy of no-cache. Are fetch policies ignored when polling?

Versions System: OS: macOS 12.2.1 Binaries: Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm Browsers: Chrome: 101.0.4951.54 Firefox: 94.0.2 Safari: 15.3 npmPackages: @apollo/client: ^3.5.10 => 3.5.10 @apollo/react-testing: ^4.0.0 => 4.0.0

jmtimko5 avatar May 06 '22 21:05 jmtimko5

@jmtimko5 Just in case it helps, can you try npm i @apollo/client@next (to get version 3.6.3)?

benjamn avatar May 06 '22 21:05 benjamn

@benjamn I am having this exact same issue on 3.6.9

ZachCMP avatar Jul 29 '22 16:07 ZachCMP

I have extremely painfull errors happening that quite obviously are created because calling functions returning values leads to cache being changed in unexpected ways. When I run the functions using sql the correct values are returned. When running them using graphIQL previously used values are returned.

I have worked through multiple interations of trying to correct this error. Manipulating dataIdFromObject did not help. Setting fetchPolicy: 'no-cache' did not work.

This is awful. I now have to find a solution not using apollographql. Which forces me to implement two ways of connecting to the db, while authenticating correctly.

Yikes.

barbalex avatar Aug 08 '22 11:08 barbalex

Would someone here be able to provide a small runnable reproduction that clearly demonstrates the issue?

hwillson avatar Aug 15 '22 16:08 hwillson

Here's a super quick reproduction of what I believe @jmtimko5 is talking about: https://codesandbox.io/s/fetch-policy-cache-polling-xqzwdi?file=/src/index.jsx

Here's a link to a similar open issue involving watchQuery: https://github.com/apollographql/apollo-client/issues/6187

MrDoomBringer avatar Aug 15 '22 17:08 MrDoomBringer

Commenting to say this should be resolved in https://github.com/apollographql/apollo-client/pull/10020! Thanks for opening this issue

All the best, Emmanuel, Intern :-)

MrDoomBringer avatar Sep 02 '22 00:09 MrDoomBringer