|
1 | 1 | import { hash } from 'ohash' |
2 | 2 | import { print } from 'graphql' |
3 | 3 | import type { ApolloClient, OperationVariables, QueryOptions, DefaultContext } from '@apollo/client' |
4 | | -import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app' |
| 4 | +import type { AsyncData, AsyncDataOptions, NuxtError, NuxtApp } from 'nuxt/app' |
5 | 5 | import type { RestartableClient } from './ws' |
6 | 6 | import { ref, isRef, reactive, useCookie, useNuxtApp, useAsyncData } from '#imports' |
7 | 7 | import { NuxtApollo } from '#apollo' |
@@ -192,45 +192,51 @@ export function useApollo (): { |
192 | 192 | * |
193 | 193 | * @param {string} token The token to be applied. |
194 | 194 | * @param {string} client - Name of the Apollo client. Defaults to `default`. |
195 | | - * @param {boolean} skipResetStore - If `true`, the cache will not be reset. |
| 195 | + * @param {boolean} skipResetStore - If `false`, Resets your entire store by clearing out your cache and then re-executing all of your active queries. |
196 | 196 | * */ |
197 | 197 | onLogin: (token?: string, client?: ApolloClientKeys, skipResetStore?: boolean) => Promise<void> |
198 | 198 |
|
199 | 199 | /** |
200 | 200 | * Remove the auth token from the Apollo client, and optionally reset it's cache. |
201 | 201 | * |
202 | 202 | * @param {string} client - Name of the Apollo client. Defaults to `default`. |
203 | | - * @param {boolean} skipResetStore - If `true`, the cache will not be reset. |
| 203 | + * @param {boolean} skipResetStore - If `false`, Resets your entire store by clearing out your cache and then re-executing all of your active queries. |
204 | 204 | * */ |
205 | 205 | onLogout: (client?: ApolloClientKeys, skipResetStore?: boolean) => Promise<void> |
206 | 206 | } |
207 | 207 |
|
208 | 208 | export function useApollo () { |
209 | | - const nuxtApp = useNuxtApp() as { |
210 | | - _apolloClients?: Record<ApolloClientKeys, ApolloClient<any>>; |
211 | | - _apolloWsClients?: Record<ApolloClientKeys, RestartableClient>; |
| 209 | + const nuxtApp = useNuxtApp() as NuxtApp & { |
| 210 | + _apolloClients?: Record<ApolloClientKeys, ApolloClient<any>> |
| 211 | + _apolloWsClients?: Record<ApolloClientKeys, RestartableClient> |
212 | 212 | } |
213 | 213 |
|
214 | 214 | const getToken = async (client?: ApolloClientKeys) => { |
215 | 215 | client = client || 'default' |
216 | 216 |
|
217 | 217 | const conf = NuxtApollo?.clients?.[client] |
218 | 218 |
|
| 219 | + if (!conf) { return } |
| 220 | + |
219 | 221 | const token = ref<string | null>(null) |
220 | 222 | await (nuxtApp as ReturnType<typeof useNuxtApp>).callHook('apollo:auth', { token, client }) |
221 | 223 |
|
222 | 224 | if (token.value) { return token.value } |
223 | 225 |
|
224 | 226 | const tokenName = conf.tokenName! |
225 | 227 |
|
226 | | - return conf?.tokenStorage === 'cookie' ? useCookie(tokenName).value : (process.client && localStorage.getItem(tokenName)) || null |
| 228 | + return conf?.tokenStorage === 'cookie' |
| 229 | + ? nuxtApp.runWithContext(() => useCookie(tokenName).value) |
| 230 | + : (process.client && localStorage.getItem(tokenName)) || null |
227 | 231 | } |
228 | 232 | type TAuthUpdate = {token?: string, client?: ApolloClientKeys, mode: 'login' | 'logout', skipResetStore?: boolean} |
229 | 233 | const updateAuth = async ({ token, client, mode, skipResetStore }: TAuthUpdate) => { |
230 | 234 | client = client || 'default' |
231 | 235 |
|
232 | 236 | const conf = NuxtApollo?.clients?.[client] |
233 | 237 |
|
| 238 | + if (!conf) { return } |
| 239 | + |
234 | 240 | const tokenName = client && conf.tokenName! |
235 | 241 |
|
236 | 242 | if (conf?.tokenStorage === 'cookie') { |
|
0 commit comments