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

When setting/changing variables in plugins, the (cache) key is not updated

FreekVR opened this issue · comments

We're building a multilingual site, and every query is getting a default variable with the current language site like so;

    /**
     * Sets the default variables for all requests.
     */
    const defaultVariables = definePlugin(({ operation }) => {
        // Only set the site if it hasn't been set yet.
        if (operation.variables && !('site' in operation.variables)) {
            operation.variables.site = nuxt?.$i18n?.localeProperties?.value?.craftSiteHandle;
        }

        // // Only set the uri if it hasn't been set yet.
        if (operation.variables && !('uri' in operation.variables)) {
            operation.variables.uri = plainUri;
        }
    });

This is causing a problem however with the cache plugin from the defaultPlugins list, as the cache key is getting set before the plugins run (?) at any rate, when the language changes, it uses cached data for a different set of query variables now.

I re-generate the key after setting the variables in the plugin above now, which feels like a bit of a workaround

operation.key = getQueryKey(operation);

This successfully fixed the caching issue for me.

I would suggest the key would get (re)calculated in the dedup/cache plugins when they run?

Interesting, I didn't expect people to modify stuff other than the headers on the fly but that's a good way to do localization actually.

I will think of something, I think the cache plugin could recalculate the key itself.