@@ -13,14 +13,40 @@ type KeysOf<T> = Array<T extends T ? keyof T extends string ? keyof T : never :
13
13
type TQuery < T > = QueryOptions < OperationVariables , T > [ 'query' ]
14
14
type TVariables < T > = QueryOptions < OperationVariables , T > [ 'variables' ] | null
15
15
type TAsyncQuery < T > = {
16
+ /**
17
+ * A unique key to ensure the query can be properly de-duplicated across requests. Defaults to a hash of the query and variables.
18
+ */
16
19
key ?: string
20
+ /**
21
+ * A GraphQL query string parsed into an AST with the gql template literal.
22
+ */
17
23
query : TQuery < T >
24
+ /**
25
+ * An object containing all of the GraphQL variables your query requires to execute.
26
+ *
27
+ * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
28
+ */
18
29
variables ?: TVariables < T >
30
+ /**
31
+ * The name of the Apollo Client to use. Defaults to `default`.
32
+ */
19
33
clientId ?: ApolloClientKeys
34
+ /**
35
+ * If you're using Apollo Link, this object is the initial value of the context object that's passed along your link chain.
36
+ */
20
37
context ?: DefaultContext
38
+ /**
39
+ * If `true`, this overrides the default fetchPolicy for the Apollo Client to `cache-first`.
40
+ * */
21
41
cache ?: boolean
22
42
}
23
43
44
+ /**
45
+ * `useAsyncQuery` resolves the GraphQL query asynchronously in a SSR-friendly composable.
46
+ *
47
+ * @param opts An object containing the query, variables, clientId, context, and cache options.
48
+ * @param options Customize the underlying `useAsyncData` composable.
49
+ */
24
50
export function useAsyncQuery <
25
51
T ,
26
52
DataT = T ,
@@ -29,6 +55,15 @@ export function useAsyncQuery <
29
55
NuxtErrorDataT = unknown
30
56
> ( opts : TAsyncQuery < T > , options ?: AsyncDataOptions < T , DataT , PickKeys , DefaultT > ) : AsyncData < PickFrom < DataT , PickKeys > | DefaultT , ( NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError < NuxtErrorDataT > ) | null >
31
57
58
+ /**
59
+ * `useAsyncQuery` resolves the GraphQL query asynchronously in a SSR-friendly composable.
60
+ *
61
+ * @param query A GraphQL query string parsed into an AST with the gql template literal.
62
+ * @param variables An object containing all of the GraphQL variables your query requires to execute.
63
+ * @param clientId The name of the Apollo Client to use. Defaults to `default`.
64
+ * @param context The context object that's passed along your link chain.
65
+ * @param options Customize the underlying `useAsyncData` composable.
66
+ */
32
67
export function useAsyncQuery <
33
68
T ,
34
69
DataT = T ,
@@ -42,6 +77,12 @@ export function useAsyncQuery <T> (...args: any[]) {
42
77
return useAsyncData < T > ( key , fn , options )
43
78
}
44
79
80
+ /**
81
+ * `useLazyAsyncQuery` resolves the GraphQL query after loading the route, instead of blocking client-side navigation.
82
+ *
83
+ * @param opts An object containing the query, variables, clientId, context, and cache options.
84
+ * @param options Customize the underlying `useAsyncData` composable.
85
+ */
45
86
export function useLazyAsyncQuery <
46
87
T ,
47
88
DataT = T ,
@@ -50,6 +91,15 @@ export function useLazyAsyncQuery <
50
91
NuxtErrorDataT = unknown
51
92
> ( opts : TAsyncQuery < T > , options ?: AsyncDataOptions < T , DataT , PickKeys , DefaultT > ) : AsyncData < PickFrom < DataT , PickKeys > | DefaultT , ( NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError < NuxtErrorDataT > ) | null >
52
93
94
+ /**
95
+ * `useLazyAsyncQuery` resolves the GraphQL query after loading the route, instead of blocking client-side navigation.
96
+ *
97
+ * @param query A GraphQL query string parsed into an AST with the gql template literal.
98
+ * @param variables An object containing all of the GraphQL variables your query requires to execute.
99
+ * @param clientId The name of the Apollo Client to use. Defaults to `default`.
100
+ * @param context The context object that's passed along your link chain.
101
+ * @param options Customize the underlying `useAsyncData` composable.
102
+ */
53
103
export function useLazyAsyncQuery <
54
104
T ,
55
105
DataT = T ,
@@ -113,7 +163,7 @@ const prep = <T> (...args: any[]) => {
113
163
options . watch . push ( variables )
114
164
}
115
165
116
- const key = args ?. [ 0 ] ?. key || hash ( { query : print ( query ) , variables, clientId } )
166
+ const key : string = args ?. [ 0 ] ?. key || hash ( { query : print ( query ) , variables, clientId } )
117
167
118
168
const fn = ( ) => clients ! [ clientId ! ] ?. query < T > ( {
119
169
query,
@@ -126,9 +176,32 @@ const prep = <T> (...args: any[]) => {
126
176
}
127
177
128
178
export function useApollo ( ) : {
179
+ /**
180
+ * Access the configured apollo clients.
181
+ */
129
182
clients : Record < ApolloClientKeys , ApolloClient < any > > | undefined
183
+ /**
184
+ * Retrieve the auth token for the specified client. Adheres to the `apollo:auth` hook.
185
+ *
186
+ * @param {string } client The client who's token to retrieve. Defaults to `default`.
187
+ */
130
188
getToken : ( client ?: ApolloClientKeys ) => Promise < string | null | undefined >
189
+
190
+ /**
191
+ * Apply auth token to the specified Apollo client, and optionally reset it's cache.
192
+ *
193
+ * @param {string } token The token to be applied.
194
+ * @param {string } client - Name of the Apollo client. Defaults to `default`.
195
+ * @param {boolean } skipResetStore - If `true`, the cache will not be reset.
196
+ * */
131
197
onLogin : ( token ?: string , client ?: ApolloClientKeys , skipResetStore ?: boolean ) => Promise < void >
198
+
199
+ /**
200
+ * Remove the auth token from the Apollo client, and optionally reset it's cache.
201
+ *
202
+ * @param {string } client - Name of the Apollo client. Defaults to `default`.
203
+ * @param {boolean } skipResetStore - If `true`, the cache will not be reset.
204
+ * */
132
205
onLogout : ( client ?: ApolloClientKeys , skipResetStore ?: boolean ) => Promise < void >
133
206
}
134
207
@@ -186,33 +259,12 @@ export function useApollo () {
186
259
}
187
260
188
261
return {
189
- /**
190
- * Retrieve the auth token for the specified client. Adheres to the `apollo:auth` hook.
191
- *
192
- * @param {string } client The client who's token to retrieve. Defaults to `default`.
193
- */
194
262
getToken,
195
263
196
- /**
197
- * Access the configured apollo clients.
198
- */
199
264
clients : nuxtApp ?. _apolloClients ,
200
265
201
- /**
202
- * Apply auth token to the specified Apollo client, and optionally reset it's cache.
203
- *
204
- * @param {string } token The token to be applied.
205
- * @param {string } client - Name of the Apollo client. Defaults to `default`.
206
- * @param {boolean } skipResetStore - If `true`, the cache will not be reset.
207
- * */
208
266
onLogin : ( token ?: string , client ?: ApolloClientKeys , skipResetStore ?: boolean ) => updateAuth ( { token, client, skipResetStore, mode : 'login' } ) ,
209
267
210
- /**
211
- * Remove the auth token from the Apollo client, and optionally reset it's cache.
212
- *
213
- * @param {string } client - Name of the Apollo client. Defaults to `default`.
214
- * @param {boolean } skipResetStore - If `true`, the cache will not be reset.
215
- * */
216
268
onLogout : ( client ?: ApolloClientKeys , skipResetStore ?: boolean ) => updateAuth ( { client, skipResetStore, mode : 'logout' } )
217
269
}
218
270
}
0 commit comments