diff --git a/README.md b/README.md index ab89f50f..17b96da8 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,33 @@ import { IGeneratedInput } from './graphql' /* generates validation schema here */ ``` +### `typesSuffix` + +type: `string` default: (empty) + +Suffixes all import types from generated typescript type. + +```yml +generates: + path/to/graphql.ts: + plugins: + - typescript + path/to/validation.ts: + plugins: + - typescript-validation-schema + config: + typesSuffix: I + importFrom: ./graphql # path for generated ts code +``` + +Then the generator generates code with import statement like below. + +```ts +import { GeneratedInputI } from './graphql' + +/* generates validation schema here */ +``` + ### `enumsAsTypes` type: `boolean` default: `false` diff --git a/src/config.ts b/src/config.ts index 7e675bde..36ff9fd7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -71,6 +71,25 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig { * ``` */ typesPrefix?: string; + /** + * @description Suffixes 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: + * typesSuffix: I + * importFrom: ./path/to/types + * ``` + */ + typesSuffix?: 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 0a344b24..7f2ac073 100644 --- a/tests/myzod.spec.ts +++ b/tests/myzod.spec.ts @@ -299,6 +299,25 @@ describe('myzod', () => { expect(result.prepend).toContain("import { ISay } from './types'"); expect(result.content).toContain('export function ISaySchema(): myzod.Type {'); }); + it('with typesSuffix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'myzod', + typesSuffix: 'I', + importFrom: './types', + }, + {} + ); + expect(result.prepend).toContain("import { SayI } from './types'"); + expect(result.content).toContain('export function SayISchema(): 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 1e963697..ec9d1058 100644 --- a/tests/yup.spec.ts +++ b/tests/yup.spec.ts @@ -294,4 +294,23 @@ describe('yup', () => { expect(result.prepend).toContain("import { ISay } from './types'"); expect(result.content).toContain('export function ISaySchema(): yup.SchemaOf {'); }); + + it('with typesSuffix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + typesSuffix: 'I', + importFrom: './types', + }, + {} + ); + expect(result.prepend).toContain("import { SayI } from './types'"); + expect(result.content).toContain('export function SayISchema(): yup.SchemaOf {'); + }); }); diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index f0b7a7c9..16070981 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -300,6 +300,26 @@ describe('zod', () => { expect(result.prepend).toContain("import { ISay } from './types'"); expect(result.content).toContain('export function ISaySchema(): z.ZodObject> {'); }); + + it('with typesSuffix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'zod', + typesSuffix: 'I', + importFrom: './types', + }, + {} + ); + expect(result.prepend).toContain("import { SayI } from './types'"); + expect(result.content).toContain('export function SayISchema(): z.ZodObject> {'); + }); describe('issues #19', () => { it('string field', async () => { const schema = buildSchema(/* GraphQL */ `