logaretm / villus

🏎 A tiny and fast GraphQL client for Vue.js
https://villus.dev
MIT License
790 stars 31 forks source link

Manually setting cache #194

Open betweenbrain opened 1 year ago

betweenbrain commented 1 year ago

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?

logaretm commented 1 year ago

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.

betweenbrain commented 1 year ago

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?

logaretm commented 1 year ago

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);
}
betweenbrain commented 1 year ago

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?

logaretm commented 1 year ago

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.

betweenbrain commented 1 year ago

Great! PR submitted at https://github.com/logaretm/villus/pull/198. I hope that helps.

betweenbrain commented 11 months ago

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