Skip to content

Allow for a more granular auto-import config #587

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export default defineNuxtModule<NuxtApolloConfig<any>>({
}
},
defaults: {
autoImports: true,
autoImports: {
gql: true,
'vue-apollo': true
},
authType: 'Bearer',
authHeader: 'Authorization',
tokenStorage: 'cookie',
Expand Down Expand Up @@ -122,31 +125,37 @@ export default defineNuxtModule<NuxtApolloConfig<any>>({
// 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 || {}
Expand Down
18 changes: 15 additions & 3 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export type NuxtAppApollo = Partial<{
_apolloWsClients?: Record<string, RestartableClient>;
}>;

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.
Expand Down Expand Up @@ -110,11 +122,11 @@ export type ClientConfig = {

export interface NuxtApolloConfig<T = ClientConfig> {
/**
* 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.
Expand Down