1
1
import { hash } from 'ohash'
2
2
import { print } from 'graphql'
3
- import type { OperationVariables , QueryOptions , DefaultContext } from '@apollo/client'
3
+ import type { ApolloClient , OperationVariables , QueryOptions , DefaultContext } from '@apollo/client'
4
4
import type { AsyncData , AsyncDataOptions , NuxtError } from 'nuxt/app'
5
- import type { NuxtAppApollo } from '../types '
5
+ import type { RestartableClient } from './ws '
6
6
import { ref , isRef , reactive , useCookie , useNuxtApp , useAsyncData } from '#imports'
7
+ import { NuxtApollo } from '#apollo'
8
+ import type { ApolloClientKeys } from '#apollo'
7
9
8
10
type PickFrom < T , K extends Array < string > > = T extends Array < any > ? T : T extends Record < string , any > ? keyof T extends K [ number ] ? T : K [ number ] extends never ? T : Pick < T , K [ number ] > : T
9
11
type KeysOf < T > = Array < T extends T ? keyof T extends string ? keyof T : never : never >
@@ -14,7 +16,7 @@ type TAsyncQuery<T> = {
14
16
key ?: string
15
17
query : TQuery < T >
16
18
variables ?: TVariables < T >
17
- clientId ?: string
19
+ clientId ?: ApolloClientKeys
18
20
context ?: DefaultContext
19
21
cache ?: boolean
20
22
}
@@ -33,7 +35,7 @@ export function useAsyncQuery <
33
35
PickKeys extends KeysOf < DataT > = KeysOf < DataT > ,
34
36
DefaultT = null ,
35
37
NuxtErrorDataT = unknown
36
- > ( query : TQuery < T > , variables ?: TVariables < T > , clientId ?: string , context ?: DefaultContext , options ?: AsyncDataOptions < T , DataT , PickKeys , DefaultT > ) : AsyncData < PickFrom < DataT , PickKeys > | DefaultT , ( NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError < NuxtErrorDataT > ) | null >
38
+ > ( query : TQuery < T > , variables ?: TVariables < T > , clientId ?: ApolloClientKeys , context ?: DefaultContext , options ?: AsyncDataOptions < T , DataT , PickKeys , DefaultT > ) : AsyncData < PickFrom < DataT , PickKeys > | DefaultT , ( NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError < NuxtErrorDataT > ) | null >
37
39
38
40
export function useAsyncQuery < T > ( ...args : any [ ] ) {
39
41
const { key, fn, options } = prep < T > ( ...args )
@@ -68,7 +70,7 @@ const prep = <T> (...args: any[]) => {
68
70
let variables : TVariables < T >
69
71
70
72
let cache : boolean = true
71
- let clientId : string | undefined
73
+ let clientId : ApolloClientKeys | undefined
72
74
let context : DefaultContext
73
75
74
76
let options : AsyncDataOptions < T , T , KeysOf < T > , null > = { }
@@ -80,7 +82,7 @@ const prep = <T> (...args: any[]) => {
80
82
cache = args ?. [ 0 ] ?. cache ?? true
81
83
context = args ?. [ 0 ] ?. context
82
84
clientId = args ?. [ 0 ] ?. clientId
83
-
85
+
84
86
if ( typeof args ?. [ 1 ] === 'object' ) {
85
87
options = args ?. [ 1 ]
86
88
}
@@ -99,7 +101,7 @@ const prep = <T> (...args: any[]) => {
99
101
if ( ! query ) { throw new Error ( '@nuxtjs/apollo: no query provided' ) }
100
102
101
103
if ( ! clientId || ! clients ?. [ clientId ] ) {
102
- clientId = clients ?. default ? 'default' : Object . keys ( clients ! ) ?. [ 0 ]
104
+ clientId = ( clients ?. default ? 'default' : Object . keys ( clients ! ) ?. [ 0 ] ) as ApolloClientKeys
103
105
104
106
if ( ! clientId ) { throw new Error ( '@nuxtjs/apollo: no client found' ) }
105
107
}
@@ -108,7 +110,7 @@ const prep = <T> (...args: any[]) => {
108
110
variables = isRef ( variables ) ? variables : reactive ( variables )
109
111
110
112
options . watch = options . watch || [ ]
111
- options . watch . push ( variables )
113
+ options . watch . push ( variables )
112
114
}
113
115
114
116
const key = args ?. [ 0 ] ?. key || hash ( { query : print ( query ) , variables, clientId } )
@@ -123,10 +125,20 @@ const prep = <T> (...args: any[]) => {
123
125
return { key, query, clientId, variables, fn, options }
124
126
}
125
127
126
- export const useApollo = ( ) => {
127
- const nuxtApp = useNuxtApp ( ) as NuxtAppApollo
128
+ export function useApollo ( ) : {
129
+ clients : Record < ApolloClientKeys , ApolloClient < any > > | undefined
130
+ getToken : ( client ?: ApolloClientKeys ) => Promise < string | null | undefined >
131
+ onLogin : ( token ?: string , client ?: ApolloClientKeys , skipResetStore ?: boolean ) => Promise < void >
132
+ onLogout : ( client ?: ApolloClientKeys , skipResetStore ?: boolean ) => Promise < void >
133
+ }
134
+
135
+ export function useApollo ( ) {
136
+ const nuxtApp = useNuxtApp ( ) as {
137
+ _apolloClients ?: Record < ApolloClientKeys , ApolloClient < any > > ;
138
+ _apolloWsClients ?: Record < ApolloClientKeys , RestartableClient > ;
139
+ }
128
140
129
- const getToken = async ( client ?: string ) => {
141
+ const getToken = async ( client ?: ApolloClientKeys ) => {
130
142
client = client || 'default'
131
143
132
144
const conf = NuxtApollo ?. clients ?. [ client ]
@@ -140,7 +152,7 @@ export const useApollo = () => {
140
152
141
153
return conf ?. tokenStorage === 'cookie' ? useCookie ( tokenName ) . value : ( process . client && localStorage . getItem ( tokenName ) ) || null
142
154
}
143
- type TAuthUpdate = { token ?: string , client ?: string , mode : 'login' | 'logout' , skipResetStore ?: boolean }
155
+ type TAuthUpdate = { token ?: string , client ?: ApolloClientKeys , mode : 'login' | 'logout' , skipResetStore ?: boolean }
144
156
const updateAuth = async ( { token, client, mode, skipResetStore } : TAuthUpdate ) => {
145
157
client = client || 'default'
146
158
@@ -169,6 +181,7 @@ export const useApollo = () => {
169
181
170
182
if ( skipResetStore ) { return }
171
183
184
+ // eslint-disable-next-line no-console
172
185
await nuxtApp ?. _apolloClients ?. [ client ] . resetStore ( ) . catch ( e => console . log ( '%cError on cache reset' , 'color: orange;' , e . message ) )
173
186
}
174
187
@@ -192,14 +205,14 @@ export const useApollo = () => {
192
205
* @param {string } client - Name of the Apollo client. Defaults to `default`.
193
206
* @param {boolean } skipResetStore - If `true`, the cache will not be reset.
194
207
* */
195
- onLogin : ( token ?: string , client ?: string , skipResetStore ?: boolean ) => updateAuth ( { token, client, skipResetStore, mode : 'login' } ) ,
208
+ onLogin : ( token ?: string , client ?: ApolloClientKeys , skipResetStore ?: boolean ) => updateAuth ( { token, client, skipResetStore, mode : 'login' } ) ,
196
209
197
210
/**
198
211
* Remove the auth token from the Apollo client, and optionally reset it's cache.
199
212
*
200
213
* @param {string } client - Name of the Apollo client. Defaults to `default`.
201
214
* @param {boolean } skipResetStore - If `true`, the cache will not be reset.
202
215
* */
203
- onLogout : ( client ?: string , skipResetStore ?: boolean ) => updateAuth ( { client, skipResetStore, mode : 'logout' } )
216
+ onLogout : ( client ?: ApolloClientKeys , skipResetStore ?: boolean ) => updateAuth ( { client, skipResetStore, mode : 'logout' } )
204
217
}
205
218
}
0 commit comments