Skip to content

Commit 2b28782

Browse files
authored
Merge pull request #45 from Code-Hex/add/typesPrefix-config
added typesPrefix config
2 parents 0476bb5 + 6731256 commit 2b28782

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
@@ -80,6 +80,33 @@ import { GeneratedInput } from './graphql'
8080
/* generates validation schema here */
8181
```
8282

83+
### `typesPrefix`
84+
85+
type: `string` default: (empty)
86+
87+
Prefixes all import types from generated typescript type.
88+
89+
```yml
90+
generates:
91+
path/to/graphql.ts:
92+
plugins:
93+
- typescript
94+
path/to/validation.ts:
95+
plugins:
96+
- typescript-validation-schema
97+
config:
98+
typesPrefix: I
99+
importFrom: ./graphql # path for generated ts code
100+
```
101+
102+
Then the generator generates code with import statement like below.
103+
104+
```ts
105+
import { IGeneratedInput } from './graphql'
106+
107+
/* generates validation schema here */
108+
```
109+
83110
### `enumsAsTypes`
84111

85112
type: `boolean` default: `false`

src/config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
5252
* ```
5353
*/
5454
importFrom?: string;
55+
/**
56+
* @description Prefixes all import types from generated typescript type.
57+
* @default ""
58+
*
59+
* @exampleMarkdown
60+
* ```yml
61+
* generates:
62+
* path/to/types.ts:
63+
* plugins:
64+
* - typescript
65+
* path/to/schemas.ts:
66+
* plugins:
67+
* - graphql-codegen-validation-schema
68+
* config:
69+
* typesPrefix: I
70+
* importFrom: ./path/to/types
71+
* ```
72+
*/
73+
typesPrefix?: string;
5574
/**
5675
* @description Generates validation schema for enum as TypeScript `type`
5776
* @default false

tests/myzod.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,25 @@ describe('myzod', () => {
280280
expect(result.content).toContain(wantContain);
281281
}
282282
});
283+
it('with typesPrefix', async () => {
284+
const schema = buildSchema(/* GraphQL */ `
285+
input Say {
286+
phrase: String!
287+
}
288+
`);
289+
const result = await plugin(
290+
schema,
291+
[],
292+
{
293+
schema: 'myzod',
294+
typesPrefix: 'I',
295+
importFrom: './types',
296+
},
297+
{}
298+
);
299+
expect(result.prepend).toContain("import { ISay } from './types'");
300+
expect(result.content).toContain('export function ISaySchema(): myzod.Type<ISay> {');
301+
});
283302
describe('issues #19', () => {
284303
it('string field', async () => {
285304
const schema = buildSchema(/* GraphQL */ `

tests/yup.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,23 @@ describe('yup', () => {
275275
expect(result.content).toContain(wantContain);
276276
}
277277
});
278+
279+
it('with typesPrefix', async () => {
280+
const schema = buildSchema(/* GraphQL */ `
281+
input Say {
282+
phrase: String!
283+
}
284+
`);
285+
const result = await plugin(
286+
schema,
287+
[],
288+
{
289+
typesPrefix: 'I',
290+
importFrom: './types',
291+
},
292+
{}
293+
);
294+
expect(result.prepend).toContain("import { ISay } from './types'");
295+
expect(result.content).toContain('export function ISaySchema(): yup.SchemaOf<ISay> {');
296+
});
278297
});

tests/zod.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,26 @@ describe('zod', () => {
280280
expect(result.content).toContain(wantContain);
281281
}
282282
});
283+
284+
it('with typesPrefix', async () => {
285+
const schema = buildSchema(/* GraphQL */ `
286+
input Say {
287+
phrase: String!
288+
}
289+
`);
290+
const result = await plugin(
291+
schema,
292+
[],
293+
{
294+
schema: 'zod',
295+
typesPrefix: 'I',
296+
importFrom: './types',
297+
},
298+
{}
299+
);
300+
expect(result.prepend).toContain("import { ISay } from './types'");
301+
expect(result.content).toContain('export function ISaySchema(): z.ZodObject<Properties<ISay>> {');
302+
});
283303
describe('issues #19', () => {
284304
it('string field', async () => {
285305
const schema = buildSchema(/* GraphQL */ `

0 commit comments

Comments
 (0)