Skip to content

Commit

Permalink
feat: allow for granular autoimport config
Browse files Browse the repository at this point in the history
  • Loading branch information
Eazash committed Jan 1, 2024
1 parent 0d9f839 commit 3dc06a1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
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

0 comments on commit 3dc06a1

Please sign in to comment.