Skip to content

Commit 56de988

Browse files
committed
fix: improve typing for composables
1 parent 61d4b08 commit 56de988

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"dev": "nuxi dev playground",
2323
"dev:build": "nuxi build playground",
2424
"dev:prepare": "nuxt-module-build build --stub && nuxi prepare playground",
25-
"lint": "eslint --ext .ts,.js,.vue --ignore-path .gitignore .",
25+
"lint": "eslint --ext .js,.ts,.vue --ignore-path .gitignore .",
26+
"lint:fix": "eslint --fix --ext .js,.ts,.vue --ignore-path .gitignore .",
2627
"release": "standard-version --prerelease alpha && git push --follow-tags && pnpm publish --tag next"
2728
},
2829
"dependencies": {

playground/nuxt.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export default defineNuxtConfig({
2+
devtools: { enabled: true },
3+
24
modules: ['@nuxt/ui', '@nuxtjs/apollo'],
35

46
colorMode: {

src/module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export default defineNuxtModule<ModuleOptions>({
4343
},
4444
async setup (options, nuxt) {
4545
if (!options.clients || !Object.keys(options.clients).length) {
46-
throw new Error('[@nuxtjs/apollo] Atleast one client must be configured.')
46+
logger.warn('No apollo clients configured.')
47+
return
4748
}
4849

4950
const { resolve } = createResolver(import.meta.url)

src/runtime/composables.ts

+74-22
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,40 @@ type KeysOf<T> = Array<T extends T ? keyof T extends string ? keyof T : never :
1313
type TQuery<T> = QueryOptions<OperationVariables, T>['query']
1414
type TVariables<T> = QueryOptions<OperationVariables, T>['variables'] | null
1515
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+
*/
1619
key?: string
20+
/**
21+
* A GraphQL query string parsed into an AST with the gql template literal.
22+
*/
1723
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+
*/
1829
variables?: TVariables<T>
30+
/**
31+
* The name of the Apollo Client to use. Defaults to `default`.
32+
*/
1933
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+
*/
2037
context?: DefaultContext
38+
/**
39+
* If `true`, this overrides the default fetchPolicy for the Apollo Client to `cache-first`.
40+
* */
2141
cache?: boolean
2242
}
2343

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+
*/
2450
export function useAsyncQuery <
2551
T,
2652
DataT = T,
@@ -29,6 +55,15 @@ export function useAsyncQuery <
2955
NuxtErrorDataT = unknown
3056
> (opts: TAsyncQuery<T>, options?: AsyncDataOptions<T, DataT, PickKeys, DefaultT>): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | null>
3157

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+
*/
3267
export function useAsyncQuery <
3368
T,
3469
DataT = T,
@@ -42,6 +77,12 @@ export function useAsyncQuery <T> (...args: any[]) {
4277
return useAsyncData<T>(key, fn, options)
4378
}
4479

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+
*/
4586
export function useLazyAsyncQuery <
4687
T,
4788
DataT = T,
@@ -50,6 +91,15 @@ export function useLazyAsyncQuery <
5091
NuxtErrorDataT = unknown
5192
> (opts: TAsyncQuery<T>, options?: AsyncDataOptions<T, DataT, PickKeys, DefaultT>): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | null>
5293

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+
*/
53103
export function useLazyAsyncQuery <
54104
T,
55105
DataT = T,
@@ -113,7 +163,7 @@ const prep = <T> (...args: any[]) => {
113163
options.watch.push(variables)
114164
}
115165

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 })
117167

118168
const fn = () => clients![clientId!]?.query<T>({
119169
query,
@@ -126,9 +176,32 @@ const prep = <T> (...args: any[]) => {
126176
}
127177

128178
export function useApollo (): {
179+
/**
180+
* Access the configured apollo clients.
181+
*/
129182
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+
*/
130188
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+
* */
131197
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+
* */
132205
onLogout: (client?: ApolloClientKeys, skipResetStore?: boolean) => Promise<void>
133206
}
134207

@@ -186,33 +259,12 @@ export function useApollo () {
186259
}
187260

188261
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-
*/
194262
getToken,
195263

196-
/**
197-
* Access the configured apollo clients.
198-
*/
199264
clients: nuxtApp?._apolloClients,
200265

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-
* */
208266
onLogin: (token?: string, client?: ApolloClientKeys, skipResetStore?: boolean) => updateAuth({ token, client, skipResetStore, mode: 'login' }),
209267

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-
* */
216268
onLogout: (client?: ApolloClientKeys, skipResetStore?: boolean) => updateAuth({ client, skipResetStore, mode: 'logout' })
217269
}
218270
}

0 commit comments

Comments
 (0)