Skip to content

Commit ea0a388

Browse files
authored
Merge pull request #46 from Code-Hex/add/typesSuffix-config
add typesSuffix config
2 parents 2b28782 + 6da9550 commit ea0a388

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,33 @@ import { IGeneratedInput } from './graphql'
107107
/* generates validation schema here */
108108
```
109109

110+
### `typesSuffix`
111+
112+
type: `string` default: (empty)
113+
114+
Suffixes all import types from generated typescript type.
115+
116+
```yml
117+
generates:
118+
path/to/graphql.ts:
119+
plugins:
120+
- typescript
121+
path/to/validation.ts:
122+
plugins:
123+
- typescript-validation-schema
124+
config:
125+
typesSuffix: I
126+
importFrom: ./graphql # path for generated ts code
127+
```
128+
129+
Then the generator generates code with import statement like below.
130+
131+
```ts
132+
import { GeneratedInputI } from './graphql'
133+
134+
/* generates validation schema here */
135+
```
136+
110137
### `enumsAsTypes`
111138

112139
type: `boolean` default: `false`

src/config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
7171
* ```
7272
*/
7373
typesPrefix?: string;
74+
/**
75+
* @description Suffixes all import types from generated typescript type.
76+
* @default ""
77+
*
78+
* @exampleMarkdown
79+
* ```yml
80+
* generates:
81+
* path/to/types.ts:
82+
* plugins:
83+
* - typescript
84+
* path/to/schemas.ts:
85+
* plugins:
86+
* - graphql-codegen-validation-schema
87+
* config:
88+
* typesSuffix: I
89+
* importFrom: ./path/to/types
90+
* ```
91+
*/
92+
typesSuffix?: string;
7493
/**
7594
* @description Generates validation schema for enum as TypeScript `type`
7695
* @default false

tests/myzod.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,25 @@ describe('myzod', () => {
299299
expect(result.prepend).toContain("import { ISay } from './types'");
300300
expect(result.content).toContain('export function ISaySchema(): myzod.Type<ISay> {');
301301
});
302+
it('with typesSuffix', async () => {
303+
const schema = buildSchema(/* GraphQL */ `
304+
input Say {
305+
phrase: String!
306+
}
307+
`);
308+
const result = await plugin(
309+
schema,
310+
[],
311+
{
312+
schema: 'myzod',
313+
typesSuffix: 'I',
314+
importFrom: './types',
315+
},
316+
{}
317+
);
318+
expect(result.prepend).toContain("import { SayI } from './types'");
319+
expect(result.content).toContain('export function SayISchema(): myzod.Type<SayI> {');
320+
});
302321
describe('issues #19', () => {
303322
it('string field', async () => {
304323
const schema = buildSchema(/* GraphQL */ `

tests/yup.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,23 @@ describe('yup', () => {
294294
expect(result.prepend).toContain("import { ISay } from './types'");
295295
expect(result.content).toContain('export function ISaySchema(): yup.SchemaOf<ISay> {');
296296
});
297+
298+
it('with typesSuffix', async () => {
299+
const schema = buildSchema(/* GraphQL */ `
300+
input Say {
301+
phrase: String!
302+
}
303+
`);
304+
const result = await plugin(
305+
schema,
306+
[],
307+
{
308+
typesSuffix: 'I',
309+
importFrom: './types',
310+
},
311+
{}
312+
);
313+
expect(result.prepend).toContain("import { SayI } from './types'");
314+
expect(result.content).toContain('export function SayISchema(): yup.SchemaOf<SayI> {');
315+
});
297316
});

tests/zod.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,26 @@ describe('zod', () => {
300300
expect(result.prepend).toContain("import { ISay } from './types'");
301301
expect(result.content).toContain('export function ISaySchema(): z.ZodObject<Properties<ISay>> {');
302302
});
303+
304+
it('with typesSuffix', async () => {
305+
const schema = buildSchema(/* GraphQL */ `
306+
input Say {
307+
phrase: String!
308+
}
309+
`);
310+
const result = await plugin(
311+
schema,
312+
[],
313+
{
314+
schema: 'zod',
315+
typesSuffix: 'I',
316+
importFrom: './types',
317+
},
318+
{}
319+
);
320+
expect(result.prepend).toContain("import { SayI } from './types'");
321+
expect(result.content).toContain('export function SayISchema(): z.ZodObject<Properties<SayI>> {');
322+
});
303323
describe('issues #19', () => {
304324
it('string field', async () => {
305325
const schema = buildSchema(/* GraphQL */ `

0 commit comments

Comments
 (0)