diff --git a/src/config.ts b/src/config.ts index de6d0492..967c7e06 100644 --- a/src/config.ts +++ b/src/config.ts @@ -52,6 +52,28 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig { * ``` */ importFrom?: string; + /** + * @description Will use `import type {}` rather than `import {}` when importing generated typescript types. + * This gives compatibility with TypeScript's "importsNotUsedAsValues": "error" option + * Should used in conjunction with `importFrom` option. + * + * @exampleMarkdown + * ```yml + * generates: + * path/to/types.ts: + * plugins: + * - typescript + * path/to/schemas.ts: + * plugins: + * - graphql-codegen-validation-schema + * config: + * schema: yup + * importFrom: ./path/to/types + * useTypeImports: true + * + * ``` + */ + useTypeImports?: boolean; /** * @description Prefixes all import types from generated typescript type. * @default "" diff --git a/src/myzod/index.ts b/src/myzod/index.ts index e742ce77..d96692df 100644 --- a/src/myzod/index.ts +++ b/src/myzod/index.ts @@ -25,7 +25,10 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche return { buildImports: (): string[] => { if (config.importFrom && importTypes.length > 0) { - return [importZod, `import { ${importTypes.join(', ')} } from '${config.importFrom}'`]; + return [ + importZod, + `import ${config.useTypeImports ? 'type ' : ''}{ ${importTypes.join(', ')} } from '${config.importFrom}'`, + ]; } return [importZod]; }, diff --git a/src/yup/index.ts b/src/yup/index.ts index 04672a0d..fa319151 100644 --- a/src/yup/index.ts +++ b/src/yup/index.ts @@ -24,7 +24,10 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema return { buildImports: (): string[] => { if (config.importFrom && importTypes.length > 0) { - return [importYup, `import { ${importTypes.join(', ')} } from '${config.importFrom}'`]; + return [ + importYup, + `import ${config.useTypeImports ? 'type ' : ''}{ ${importTypes.join(', ')} } from '${config.importFrom}'`, + ]; } return [importYup]; }, diff --git a/src/zod/index.ts b/src/zod/index.ts index cbb08644..7fabe75d 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -25,7 +25,10 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema return { buildImports: (): string[] => { if (config.importFrom && importTypes.length > 0) { - return [importZod, `import { ${importTypes.join(', ')} } from '${config.importFrom}'`]; + return [ + importZod, + `import ${config.useTypeImports ? 'type ' : ''}{ ${importTypes.join(', ')} } from '${config.importFrom}'`, + ]; } return [importZod]; }, diff --git a/tests/myzod.spec.ts b/tests/myzod.spec.ts index d98912a6..c59bfab4 100644 --- a/tests/myzod.spec.ts +++ b/tests/myzod.spec.ts @@ -222,6 +222,26 @@ describe('myzod', () => { expect(result.content).toContain('phrase: myzod.string()'); }); + it('with importFrom & useTypeImports', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'myzod', + importFrom: './types', + useTypeImports: true, + }, + {} + ); + expect(result.prepend).toContain("import type { Say } from './types'"); + expect(result.content).toContain('phrase: myzod.string()'); + }); + it('with enumsAsTypes', async () => { const schema = buildSchema(/* GraphQL */ ` enum PageType { diff --git a/tests/yup.spec.ts b/tests/yup.spec.ts index 6fa3f85f..4670fb76 100644 --- a/tests/yup.spec.ts +++ b/tests/yup.spec.ts @@ -220,6 +220,25 @@ describe('yup', () => { expect(result.content).toContain('phrase: yup.string().defined()'); }); + it('with importFrom & useTypeImports', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + importFrom: './types', + useTypeImports: true, + }, + {} + ); + expect(result.prepend).toContain("import type { Say } from './types'"); + expect(result.content).toContain('phrase: yup.string().defined()'); + }); + it('with enumsAsTypes', async () => { const schema = buildSchema(/* GraphQL */ ` enum PageType { diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index b84f5fde..ae7f869d 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -223,6 +223,26 @@ describe('zod', () => { expect(result.content).toContain('phrase: z.string()'); }); + it('with importFrom & useTypeImports', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'zod', + importFrom: './types', + useTypeImports: true, + }, + {} + ); + expect(result.prepend).toContain("import type { Say } from './types'"); + expect(result.content).toContain('phrase: z.string()'); + }); + it('with enumsAsTypes', async () => { const schema = buildSchema(/* GraphQL */ ` enum PageType {