Skip to content

Commit cbec0f8

Browse files
authored
Merge pull request #377 from Toanzzz/main
2 parents f67ff70 + 89fb9d6 commit cbec0f8

File tree

7 files changed

+93
-3
lines changed

7 files changed

+93
-3
lines changed

src/config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
5252
* ```
5353
*/
5454
importFrom?: string;
55+
/**
56+
* @description Will use `import type {}` rather than `import {}` when importing generated typescript types.
57+
* This gives compatibility with TypeScript's "importsNotUsedAsValues": "error" option
58+
* Should used in conjunction with `importFrom` option.
59+
*
60+
* @exampleMarkdown
61+
* ```yml
62+
* generates:
63+
* path/to/types.ts:
64+
* plugins:
65+
* - typescript
66+
* path/to/schemas.ts:
67+
* plugins:
68+
* - graphql-codegen-validation-schema
69+
* config:
70+
* schema: yup
71+
* importFrom: ./path/to/types
72+
* useTypeImports: true
73+
*
74+
* ```
75+
*/
76+
useTypeImports?: boolean;
5577
/**
5678
* @description Prefixes all import types from generated typescript type.
5779
* @default ""

src/myzod/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
2525
return {
2626
buildImports: (): string[] => {
2727
if (config.importFrom && importTypes.length > 0) {
28-
return [importZod, `import { ${importTypes.join(', ')} } from '${config.importFrom}'`];
28+
return [
29+
importZod,
30+
`import ${config.useTypeImports ? 'type ' : ''}{ ${importTypes.join(', ')} } from '${config.importFrom}'`,
31+
];
2932
}
3033
return [importZod];
3134
},

src/yup/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
2424
return {
2525
buildImports: (): string[] => {
2626
if (config.importFrom && importTypes.length > 0) {
27-
return [importYup, `import { ${importTypes.join(', ')} } from '${config.importFrom}'`];
27+
return [
28+
importYup,
29+
`import ${config.useTypeImports ? 'type ' : ''}{ ${importTypes.join(', ')} } from '${config.importFrom}'`,
30+
];
2831
}
2932
return [importYup];
3033
},

src/zod/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
2525
return {
2626
buildImports: (): string[] => {
2727
if (config.importFrom && importTypes.length > 0) {
28-
return [importZod, `import { ${importTypes.join(', ')} } from '${config.importFrom}'`];
28+
return [
29+
importZod,
30+
`import ${config.useTypeImports ? 'type ' : ''}{ ${importTypes.join(', ')} } from '${config.importFrom}'`,
31+
];
2932
}
3033
return [importZod];
3134
},

tests/myzod.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@ describe('myzod', () => {
222222
expect(result.content).toContain('phrase: myzod.string()');
223223
});
224224

225+
it('with importFrom & useTypeImports', async () => {
226+
const schema = buildSchema(/* GraphQL */ `
227+
input Say {
228+
phrase: String!
229+
}
230+
`);
231+
const result = await plugin(
232+
schema,
233+
[],
234+
{
235+
schema: 'myzod',
236+
importFrom: './types',
237+
useTypeImports: true,
238+
},
239+
{}
240+
);
241+
expect(result.prepend).toContain("import type { Say } from './types'");
242+
expect(result.content).toContain('phrase: myzod.string()');
243+
});
244+
225245
it('with enumsAsTypes', async () => {
226246
const schema = buildSchema(/* GraphQL */ `
227247
enum PageType {

tests/yup.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ describe('yup', () => {
220220
expect(result.content).toContain('phrase: yup.string().defined()');
221221
});
222222

223+
it('with importFrom & useTypeImports', async () => {
224+
const schema = buildSchema(/* GraphQL */ `
225+
input Say {
226+
phrase: String!
227+
}
228+
`);
229+
const result = await plugin(
230+
schema,
231+
[],
232+
{
233+
importFrom: './types',
234+
useTypeImports: true,
235+
},
236+
{}
237+
);
238+
expect(result.prepend).toContain("import type { Say } from './types'");
239+
expect(result.content).toContain('phrase: yup.string().defined()');
240+
});
241+
223242
it('with enumsAsTypes', async () => {
224243
const schema = buildSchema(/* GraphQL */ `
225244
enum PageType {

tests/zod.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ describe('zod', () => {
223223
expect(result.content).toContain('phrase: z.string()');
224224
});
225225

226+
it('with importFrom & useTypeImports', async () => {
227+
const schema = buildSchema(/* GraphQL */ `
228+
input Say {
229+
phrase: String!
230+
}
231+
`);
232+
const result = await plugin(
233+
schema,
234+
[],
235+
{
236+
schema: 'zod',
237+
importFrom: './types',
238+
useTypeImports: true,
239+
},
240+
{}
241+
);
242+
expect(result.prepend).toContain("import type { Say } from './types'");
243+
expect(result.content).toContain('phrase: z.string()');
244+
});
245+
226246
it('with enumsAsTypes', async () => {
227247
const schema = buildSchema(/* GraphQL */ `
228248
enum PageType {

0 commit comments

Comments
 (0)