From 3cb64446e990d62783b95f8eeb1864b4f6ca6a93 Mon Sep 17 00:00:00 2001 From: Conrawl Rogers Date: Fri, 1 Mar 2024 15:01:27 -0400 Subject: [PATCH] fix: ensure `useCookie` called with context Closes #524 --- playground/components/GithubDemo.vue | 30 ++++++++++++++++------------ playground/nuxt.config.ts | 7 ++++++- playground/queries/discussions.gql | 2 +- src/runtime/composables.ts | 20 ++++++++++++------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/playground/components/GithubDemo.vue b/playground/components/GithubDemo.vue index 6b092991..616d5204 100644 --- a/playground/components/GithubDemo.vue +++ b/playground/components/GithubDemo.vue @@ -47,29 +47,34 @@ diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 0d010067..4371eb3d 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -13,11 +13,16 @@ export default defineNuxtConfig({ default: './apollo/default.ts', github: { httpEndpoint: 'https://api.github.com/graphql', - tokenStorage: 'localStorage' + tokenStorage: 'cookie' }, todos: { httpEndpoint: 'https://nuxt-gql-server-2gl6xp7kua-ue.a.run.app/query', wsEndpoint: 'wss://nuxt-gql-server-2gl6xp7kua-ue.a.run.app/query', + defaultOptions: { + watchQuery: { + fetchPolicy: 'cache-and-network' + } + }, httpLinkOptions: { headers: { 'X-CUSTOM-HEADER': '123' diff --git a/playground/queries/discussions.gql b/playground/queries/discussions.gql index ee0c0084..1c65bf20 100644 --- a/playground/queries/discussions.gql +++ b/playground/queries/discussions.gql @@ -1,5 +1,5 @@ query discussions { - repository(owner: "nuxt", name: "framework") { + repository(owner: "nuxt", name: "nuxt") { discussions(first: 5) { nodes { author { diff --git a/src/runtime/composables.ts b/src/runtime/composables.ts index 7644afea..9497ba40 100644 --- a/src/runtime/composables.ts +++ b/src/runtime/composables.ts @@ -1,7 +1,7 @@ import { hash } from 'ohash' import { print } from 'graphql' import type { ApolloClient, OperationVariables, QueryOptions, DefaultContext } from '@apollo/client' -import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app' +import type { AsyncData, AsyncDataOptions, NuxtError, NuxtApp } from 'nuxt/app' import type { RestartableClient } from './ws' import { ref, isRef, reactive, useCookie, useNuxtApp, useAsyncData } from '#imports' import { NuxtApollo } from '#apollo' @@ -192,7 +192,7 @@ export function useApollo (): { * * @param {string} token The token to be applied. * @param {string} client - Name of the Apollo client. Defaults to `default`. - * @param {boolean} skipResetStore - If `true`, the cache will not be reset. + * @param {boolean} skipResetStore - If `false`, Resets your entire store by clearing out your cache and then re-executing all of your active queries. * */ onLogin: (token?: string, client?: ApolloClientKeys, skipResetStore?: boolean) => Promise @@ -200,15 +200,15 @@ export function useApollo (): { * Remove the auth token from the Apollo client, and optionally reset it's cache. * * @param {string} client - Name of the Apollo client. Defaults to `default`. - * @param {boolean} skipResetStore - If `true`, the cache will not be reset. + * @param {boolean} skipResetStore - If `false`, Resets your entire store by clearing out your cache and then re-executing all of your active queries. * */ onLogout: (client?: ApolloClientKeys, skipResetStore?: boolean) => Promise } export function useApollo () { - const nuxtApp = useNuxtApp() as { - _apolloClients?: Record>; - _apolloWsClients?: Record; + const nuxtApp = useNuxtApp() as NuxtApp & { + _apolloClients?: Record> + _apolloWsClients?: Record } const getToken = async (client?: ApolloClientKeys) => { @@ -216,6 +216,8 @@ export function useApollo () { const conf = NuxtApollo?.clients?.[client] + if (!conf) { return } + const token = ref(null) await (nuxtApp as ReturnType).callHook('apollo:auth', { token, client }) @@ -223,7 +225,9 @@ export function useApollo () { const tokenName = conf.tokenName! - return conf?.tokenStorage === 'cookie' ? useCookie(tokenName).value : (process.client && localStorage.getItem(tokenName)) || null + return conf?.tokenStorage === 'cookie' + ? nuxtApp.runWithContext(() => useCookie(tokenName).value) + : (process.client && localStorage.getItem(tokenName)) || null } type TAuthUpdate = {token?: string, client?: ApolloClientKeys, mode: 'login' | 'logout', skipResetStore?: boolean} const updateAuth = async ({ token, client, mode, skipResetStore }: TAuthUpdate) => { @@ -231,6 +235,8 @@ export function useApollo () { const conf = NuxtApollo?.clients?.[client] + if (!conf) { return } + const tokenName = client && conf.tokenName! if (conf?.tokenStorage === 'cookie') {