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

Mutations with fetchPolicy: 'no-cache' still trigger refetchQueries #10238

Closed
bendenoz opened this issue Oct 28, 2022 · 6 comments · Fixed by #11515 or #11546
Closed

Mutations with fetchPolicy: 'no-cache' still trigger refetchQueries #10238

bendenoz opened this issue Oct 28, 2022 · 6 comments · Fixed by #11515 or #11546
Labels

Comments

@bendenoz
Copy link
Contributor

Intended outcome:

I'm trying to execute mutations without triggering cache reconciliation because I have a lot of data in the cache, which can take up to >5secs.

Actual outcome:

even with fetchPolicy: 'no-cache', I see refetchQueries triggered.

How to reproduce the issue:

Load > 8000 objects in the cache in one big list query, trigger a mutation on unrelated objects with fetchPolicy: 'no-cache'

Versions

3.7.0

I believe the problem is in

mutation.refetchQueries ||

The test currently line 402 checks if refetchQueries exists, and since it appears to be an array it's always true, i guess it should be mutation.refetchQueries.length > 0

My current workaround it to pass false refetchQueries: false to the mutate call but it fails the typescript constraint

@MacroMackie
Copy link

MacroMackie commented Apr 10, 2023

This has affected my team as well.

We have a note editor which periodically triggers api requests that look like this:

client.mutate({
  mutation: UpdateNoteMutation,
  fetchPolicy: "no-cache",
  variables: {
    input: {
      noteId,
    },
  },
});

The mutation triggers cache reconciliation, which can cause latency spikes when users are typing in our editor.

Our workaround has been to modify it at the "client defaultOptions" level, but definitely not ideal:

const newClient = new ApolloClient({
  ...
  defaultOptions: {
    mutate: {
      // ApolloClient's QueryManager updates the cache if this is undefined because it's replaced
      // with []. This will circumvent that issue.
      // See: https://github.com/apollographql/apollo-client/issues/10238
      //
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      refetchQueries: false as any,
    },
  },
});

I'm happy to share more info if that would be helpful!

@jessgoldq4
Copy link

jessgoldq4 commented Sep 14, 2023

@bendenoz is this still an issue in the latest version of apollo-client?

Edit: Based on reading the code in main, it seems the answer is yes. Maybe an easy fix to create a PR. For now my workaround is to set refetchQueries: null in the mutation options

@druvisc
Copy link

druvisc commented Oct 17, 2023

You don't even need refetchQueries for automatic cache reconciliation to trigger (even on queries with fetchPolicy: "network-only"): #11219 (comment).

The only workaround is to set nextFetchPolicy: 'standby' on the queries so you can control when the data is re-fetched.

@vladar
Copy link
Contributor

vladar commented Jan 23, 2024

I've opened a PR for a quick fix: #11515. Curious if you see any pitfals with this simple fix?

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Copy link
Contributor

github-actions bot commented Mar 2, 2024

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 Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
6 participants