Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
5 changes: 4 additions & 1 deletion src/myzod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
},
Expand Down
5 changes: 4 additions & 1 deletion src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
},
Expand Down
5 changes: 4 additions & 1 deletion src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
},
Expand Down
20 changes: 20 additions & 0 deletions tests/myzod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions tests/yup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down