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

Manually setting cache

betweenbrain opened this issue · comments

Hello,

We are looking to achieve something very similar to issue #29. In reviewing https://villus.logaretm.com/plugins/cache/#clearing-cache, it seems like one should be able to call cachePlugin.setCacheResult(...), but my initial attempts following the example throws TypeError: j.setCacheResult is not a function. Is this expected?

Sorry for the delay, just got around to this. The plugin doesn't exposesetCacheResult, only the clear method.

But I guess we could expose it, will try to tackle it this week. PR is welcome as well, thanks for reporting this.

Thanks for the response! And, now I owe you an apology for the delay 😆

Would exposing it be as simple as adding something like cachPlugin.setCacheResult = setCacheResult(operation, result); to https://github.com/logaretm/villus/blob/main/packages/villus/src/cache.ts#L80?

Hello, it is the other way around. Sorry, I didn't find the time to work on it, busy with vee-validate at the moment.

It is as simple as you suggested. But how would you set the cache yourself? Would you pass an object containing the query and variables? Because it relies on key being provided, so it might be worth regenerating the key before then.

cachPlugin.setCacheResult = function (operation, result) {
  // getQueryKey imported from utils
  const key = operation.key || getQueryKey(operation);
 setCacheResult({ key, ...operation }, result);
}

Hello, thanks again for the response. No apologies are needed!

But how would you set the cache yourself? Would you pass an object containing the query and variables?

Yes, that's exactly how I was thinking we would do it.

Is there anything that I can do to help move this forward?

Is there anything that I can do to help move this forward?

Feel free to PR it if you have the time, I know it's a 1 liner but I usually do open-source on weekends and whenever I can sneak in work days. I will take it on by the weekend if you won't have the time.

Great! PR submitted at #198. I hope that helps.

@logaretm do you have a chance to review the above PR? Thanks!