question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent mutation from caching

See original GitHub issue

I have an editable element. On every change I send the serialized content to my server using a mutation, so one mutation per change.

Now cache entries in ROOT_MUTATION pile up. The serialized string can be quite long. Is there any way to prevent a mutation from caching the result?

I use version ^3.0.0-beta.23.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
cloudrathacommented, Jul 24, 2020

I had the same issue, and kinda thought it would be a configuration in the cache to mark a mutation as not cacheable. But it’s on the mutation level.

const result = useMutation(SOME_MUTATION, {
    fetchPolicy: 'no-cache',
});

no-cache does not read or write to the cache.

The docs for useMutation don’t show the fetchPolicy but it’s there.

2reactions
cloudrathacommented, Jul 24, 2020

@henryqdineen you can use the onComplete handler and use the client to write to the cache.

export default function Login() {
  const client: ApolloClient<any> = useApolloClient();
  const [login, { loading, error }] = useMutation<LoginTypes.login, LoginTypes.loginVariables>(
    LOGIN_USER,
    {
      onCompleted({ login }) {
        client.writeQuery({
          query: gql`
             query GetIsLoggedIn {
                isLoggedIn
             }
          `,
          data: {
            isLoggedIn: true
          }
        });
        // client.writeData({ data: { isLoggedIn: true } });
      }
    }
  );

  if (loading) return <Loading />;
  if (error) return <p>An error occurred</p>;

  return <LoginForm login={login} />;
}

Taken from the docs: https://www.apollographql.com/docs/tutorial/mutations/#expose-apollo-client-with-useapolloclient But I hastily updated the example to use the writeQuery, as writeData is deprecated and removed in v3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced topics on caching in Apollo Client
Refetching queries after a mutation​​ In certain cases, writing an update function to update the cache after a mutation can be complex, or...
Read more >
Mutations & Caching with GraphQL, Ember & Apollo Tutorial
One cool thing about Apollo is that you can manually control the contents of the cache. This is really handy, especially after a...
Read more >
Caching in GraphQL: How to prevent excessive and ...
Learn how caching can be used to cache GraphQL requests to prevent ... It means that each field in a query/mutation schema can...
Read more >
Login and updating the cache - Full stack open
We are using an effect hook to save the token's value to the state of the App component and the local storage after...
Read more >
Prevent an Unnecessary Refetch with nextFetchPolicy and ...
The problem was due to a change in how the cache-and-network fetch ... response is to avoid doing a separate query after the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found