Diizzayy / nuxt-graphql-client

⚡️ Minimal GraphQL Client + Code Generation for Nuxt3
https://nuxt-graphql-client.web.app
MIT License
357 stars 44 forks source link

useGqlHeaders and useGqlHost immutable after first call on SSR #402

Open unluckynelson opened 11 months ago

unluckynelson commented 11 months ago

Environment

Describe the bug

I need to set some headers based on the user request i first received on the server side.

Common use case to attach the requesting IP address to x-forwarded-for header to enable correct IP locale detection server side. I also attempted using useGqlHost to add query parameters but they also seem immutable after the first change.

The code snippet I use to update the headers on each request:

plugins/gql-addheaders.ts

export default defineNuxtPlugin({
    async setup(nuxtApp) {
        const router = useRouter();
        const headers = {
            'x-no-cache': router.currentRoute.value.query?.test ? 'true' : 'false',
            'x-forwarded-for': '',
        };
        if (nuxtApp.ssrContext) {
            const ipaddress = nuxtApp.ssrContext?.event.node.req.headers['x-forwarded-for'] as string || '';
            headers['x-forwarded-for'] = ipaddress.split(',')[0];
        }
        console.log(headers);
        // Headers are only set on first request
        useGqlHeaders(headers);
        // Params are only set on first request
        useGqlHost(`?x-no-cache=${headers['x-no-cache']}&x-forwarded-for=${headers['x-forwarded-for']}`);
    },
});

Expected behaviour

The headers should be updated on each request. The server side logs show that the headers remain the same even after subsequent requests to useGqlHeaders

Reproduction

No response

Additional context

No response

Logs

No response

AngeloSchulerPiletti commented 10 months ago

Hello, man.

I've found a workaround for now. Try setting your headers like this:

useGqlHeaders({headers, client: YOUR_CLIENT, respectDefaults: false})