From 3dc06a1c5b27e701db969a877a7e6b16874d4f8a Mon Sep 17 00:00:00 2001 From: Ezira Ashenafi Date: Mon, 1 Jan 2024 09:47:56 +0300 Subject: [PATCH] feat: allow for granular autoimport config --- src/module.ts | 61 +++++++++++++++++++++++++++++--------------------- src/types.d.ts | 18 ++++++++++++--- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/module.ts b/src/module.ts index 2b498375..26af7150 100644 --- a/src/module.ts +++ b/src/module.ts @@ -28,7 +28,10 @@ export default defineNuxtModule>({ } }, defaults: { - autoImports: true, + autoImports: { + gql: true, + 'vue-apollo': true + }, authType: 'Bearer', authHeader: 'Authorization', tokenStorage: 'cookie', @@ -122,31 +125,37 @@ export default defineNuxtModule>({ // TODO: Integrate @vue/apollo-components? addImports([ - { name: 'gql', from: 'graphql-tag' }, - ...[ - 'useApollo', - 'useAsyncQuery', - 'useLazyAsyncQuery' - ].map(n => ({ name: n, from: resolve('runtime/composables') })), - ...(!options?.autoImports - ? [] - : [ - 'useQuery', - 'useLazyQuery', - 'useMutation', - 'useSubscription', - - 'useApolloClient', - - 'useQueryLoading', - 'useMutationLoading', - 'useSubscriptionLoading', - - 'useGlobalQueryLoading', - 'useGlobalMutationLoading', - 'useGlobalSubscriptionLoading' - ].map(n => ({ name: n, from: '@vue/apollo-composable' }))) - ]) + 'useApollo', + 'useAsyncQuery', + 'useLazyAsyncQuery' + ].map(n => ({ name: n, from: resolve('runtime/composables') }))) + + // Resolve individual autoImport config + const shouldAutoImportGqlTag = typeof options.autoImports === 'boolean' ? options.autoImports : options.autoImports?.gql + const shouldAutoImportVueApolloComposables = typeof options.autoImports === 'boolean' ? options.autoImports : options.autoImports?.['vue-apollo'] + + if (shouldAutoImportGqlTag) { + addImports({ name: 'gql', from: 'graphql-tag' }) + } + if (shouldAutoImportVueApolloComposables) { + addImports([ + ...(!options?.autoImports + ? [] + : [ + 'useQuery', + 'useLazyQuery', + 'useMutation', + 'useSubscription', + 'useApolloClient', + 'useQueryLoading', + 'useMutationLoading', + 'useSubscriptionLoading', + 'useGlobalQueryLoading', + 'useGlobalMutationLoading', + 'useGlobalSubscriptionLoading' + ].map(n => ({ name: n, from: '@vue/apollo-composable' }))) + ]) + } nuxt.hook('vite:extendConfig', (config) => { config.optimizeDeps = config.optimizeDeps || {} diff --git a/src/types.d.ts b/src/types.d.ts index 11771b23..ad85c7c3 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -11,6 +11,18 @@ export type NuxtAppApollo = Partial<{ _apolloWsClients?: Record; }>; +export type AutoImportConfig = { + /** + * Specify if the `gql` tag function should be auto-imported + * @default true + */ + gql: boolean, + /** + * Specify if vue-apollo composables should be auto-imported + */ + 'vue-apollo': boolean +} + export type ClientConfig = { /** * The GraphQL endpoint. @@ -110,11 +122,11 @@ export type ClientConfig = { export interface NuxtApolloConfig { /** - * Determine if vue-apollo composables should be automatically imported. - * @type {boolean} + * Determine if vue-apollo composables and `gql` function should be automatically imported. + * @type {boolean | AutoImportConfig} * @default true **/ - autoImports?: boolean; + autoImports?: boolean | AutoImportConfig; /** * Configuration of the Apollo clients.