Skip to content

Commit

Permalink
feat(types-import): support useTypeImports (#4927)
Browse files Browse the repository at this point in the history
* feat(types-import): support useTypeImports

* chore(import-types): add changeset
  • Loading branch information
cevr authored Oct 19, 2020
1 parent 92d8f87 commit b04535c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/slimy-forks-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/import-types-preset': minor
---

Adds support for `useTypeImports` when using the preset `import-types`
3 changes: 2 additions & 1 deletion packages/presets/import-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ export const preset: Types.OutputPreset<ImportTypesConfig> = {
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`,
},
});
}
Expand Down
26 changes: 26 additions & 0 deletions packages/presets/import-types/tests/types-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit b04535c

Please # to comment.