logaretm / villus

🏎 A tiny and fast GraphQL client for Vue.js

Home Page:https://villus.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

updating the cache after a mutation

fanckush opened this issue · comments

does Villus update the local cache after a mutation?

vue-apollo partially does

If a mutation modifies multiple entities, or if it creates or deletes one or many entities, the Apollo Client will not automatically update the cache to reflect the changes made by the mutation. Instead, you should update the cache using an update function in the options.

How should I approach this when using mutations in villus? Is there a way to update the stale cache after a successful mutation or maybe (as stated above) does Villus do this for me automatically?

Maybe i'm way off and this is outside the scope of villus. In this case is there a workaround?
thank you!

Hello, sorry for the late reply. At the moment this is outside villus scope since it does not make a relationship between queries and mutations.

However, it is possible for you to build a cache plugin that does this. Something along those lines:

import { definePlugin } from 'villus';

let myCache = {};

const cache = definePlugin({ afterQuery, useResult, operation }) => {
    if (operation.cachePolicy === 'network-only') {
      return;
    }

  if (operation.type === 'mutation') {
    // delete entries from cache
    return;
  }

  // add entries to cache ....
})

Of course, there is a little more involved than that, the main challenge is figuring out which mutations should clear which queries, it is up to you to figure out how such a connection be made. It can be based on a mutation name for example.

I spoke too soon. Having said that, there is a PR #184 that implements cache clearing for mutations that has the same tags as the queries. This is the connection challenge I mentioned.

If that is what you are looking for, then expect it to be out soon.