From 207665fe017db0de7f507af3e1beb22c33088360 Mon Sep 17 00:00:00 2001 From: Kei Kamikawa Date: Tue, 19 Apr 2022 01:08:32 +0900 Subject: [PATCH 1/2] added typesPrefix to config and tests --- src/config.ts | 19 +++++++++++++++++++ tests/myzod.spec.ts | 19 +++++++++++++++++++ tests/yup.spec.ts | 19 +++++++++++++++++++ tests/zod.spec.ts | 20 ++++++++++++++++++++ 4 files changed, 77 insertions(+) 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 */ ` From 6731256ebc05b314b7c9659567dd1c12179ceea1 Mon Sep 17 00:00:00 2001 From: Kei Kamikawa Date: Tue, 19 Apr 2022 01:13:25 +0900 Subject: [PATCH 2/2] fixed README for typesPrefix --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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`