diff --git a/.changeset/slimy-forks-work.md b/.changeset/slimy-forks-work.md new file mode 100644 index 00000000000..781dcdee63a --- /dev/null +++ b/.changeset/slimy-forks-work.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/import-types-preset': minor +--- + +Adds support for `useTypeImports` when using the preset `import-types` diff --git a/packages/presets/import-types/src/index.ts b/packages/presets/import-types/src/index.ts index 97f0181aa0a..bf7d70c6f19 100644 --- a/packages/presets/import-types/src/index.ts +++ b/packages/presets/import-types/src/index.ts @@ -72,9 +72,10 @@ export const preset: Types.OutputPreset = { options.schemaAst ) ) { + const importType = options.config.useTypeImports ? 'import type' : 'import'; plugins.unshift({ add: { - content: `import * as ${importTypesNamespace} from '${options.presetConfig.typesPath}';\n`, + content: `${importType} * as ${importTypesNamespace} from '${options.presetConfig.typesPath}';\n`, }, }); } diff --git a/packages/presets/import-types/tests/types-import.spec.ts b/packages/presets/import-types/tests/types-import.spec.ts index f0c18eb7858..49539b697c9 100644 --- a/packages/presets/import-types/tests/types-import.spec.ts +++ b/packages/presets/import-types/tests/types-import.spec.ts @@ -111,6 +111,32 @@ describe('import-types preset', () => { ); }); + it('Should prepend the "add" plugin with the correct import type', async () => { + const result = await preset.buildGeneratesSection({ + baseOutputDir: './src/operation.ts', + config: { + useTypeImports: true, + }, + presetConfig: { + typesPath: './types', + }, + schema: schemaDocumentNode, + documents: testDocuments.slice(0, 2), + plugins: [{ typescript: {} }], + pluginMap: { typescript: {} as any }, + }); + + expect(result.map(o => o.plugins)[0]).toEqual( + expect.arrayContaining([ + { + add: { + content: `import type * as Types from './types';\n`, + }, + }, + ]) + ); + }); + it('Should prepend the "add" plugin with the correct import, when only using fragment spread', async () => { const result = await preset.buildGeneratesSection({ baseOutputDir: './src/operation.ts',