diff --git a/README.md b/README.md index 58ea9d21..ab89f50f 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,33 @@ import { GeneratedInput } from './graphql' /* generates validation schema here */ ``` +### `typesPrefix` + +type: `string` default: (empty) + +Prefixes all import types from generated typescript type. + +```yml +generates: + path/to/graphql.ts: + plugins: + - typescript + path/to/validation.ts: + plugins: + - typescript-validation-schema + config: + typesPrefix: I + importFrom: ./graphql # path for generated ts code +``` + +Then the generator generates code with import statement like below. + +```ts +import { IGeneratedInput } from './graphql' + +/* generates validation schema here */ +``` + ### `enumsAsTypes` type: `boolean` default: `false` diff --git a/src/config.ts b/src/config.ts index fd6503ed..7e675bde 100644 --- a/src/config.ts +++ b/src/config.ts @@ -52,6 +52,25 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig { * ``` */ importFrom?: string; + /** + * @description Prefixes all import types from generated typescript type. + * @default "" + * + * @exampleMarkdown + * ```yml + * generates: + * path/to/types.ts: + * plugins: + * - typescript + * path/to/schemas.ts: + * plugins: + * - graphql-codegen-validation-schema + * config: + * typesPrefix: I + * importFrom: ./path/to/types + * ``` + */ + typesPrefix?: string; /** * @description Generates validation schema for enum as TypeScript `type` * @default false diff --git a/tests/myzod.spec.ts b/tests/myzod.spec.ts index 07b57b89..0a344b24 100644 --- a/tests/myzod.spec.ts +++ b/tests/myzod.spec.ts @@ -280,6 +280,25 @@ describe('myzod', () => { expect(result.content).toContain(wantContain); } }); + it('with typesPrefix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'myzod', + typesPrefix: 'I', + importFrom: './types', + }, + {} + ); + expect(result.prepend).toContain("import { ISay } from './types'"); + expect(result.content).toContain('export function ISaySchema(): myzod.Type {'); + }); describe('issues #19', () => { it('string field', async () => { const schema = buildSchema(/* GraphQL */ ` diff --git a/tests/yup.spec.ts b/tests/yup.spec.ts index 70fbebff..1e963697 100644 --- a/tests/yup.spec.ts +++ b/tests/yup.spec.ts @@ -275,4 +275,23 @@ describe('yup', () => { expect(result.content).toContain(wantContain); } }); + + it('with typesPrefix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + typesPrefix: 'I', + importFrom: './types', + }, + {} + ); + expect(result.prepend).toContain("import { ISay } from './types'"); + expect(result.content).toContain('export function ISaySchema(): yup.SchemaOf {'); + }); }); diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index 7f9be47c..f0b7a7c9 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -280,6 +280,26 @@ describe('zod', () => { expect(result.content).toContain(wantContain); } }); + + it('with typesPrefix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'zod', + typesPrefix: 'I', + importFrom: './types', + }, + {} + ); + expect(result.prepend).toContain("import { ISay } from './types'"); + expect(result.content).toContain('export function ISaySchema(): z.ZodObject> {'); + }); describe('issues #19', () => { it('string field', async () => { const schema = buildSchema(/* GraphQL */ `