File tree Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change @@ -80,6 +80,33 @@ import { GeneratedInput } from './graphql'
80
80
/* generates validation schema here */
81
81
` ` `
82
82
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
+
83
110
# ## `enumsAsTypes`
84
111
85
112
type : ` boolean` default: `false`
Original file line number Diff line number Diff line change @@ -52,6 +52,25 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
52
52
* ```
53
53
*/
54
54
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 ;
55
74
/**
56
75
* @description Generates validation schema for enum as TypeScript `type`
57
76
* @default false
Original file line number Diff line number Diff line change @@ -280,6 +280,25 @@ describe('myzod', () => {
280
280
expect ( result . content ) . toContain ( wantContain ) ;
281
281
}
282
282
} ) ;
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
+ } ) ;
283
302
describe ( 'issues #19' , ( ) => {
284
303
it ( 'string field' , async ( ) => {
285
304
const schema = buildSchema ( /* GraphQL */ `
Original file line number Diff line number Diff line change @@ -275,4 +275,23 @@ describe('yup', () => {
275
275
expect ( result . content ) . toContain ( wantContain ) ;
276
276
}
277
277
} ) ;
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
+ } ) ;
278
297
} ) ;
Original file line number Diff line number Diff line change @@ -280,6 +280,26 @@ describe('zod', () => {
280
280
expect ( result . content ) . toContain ( wantContain ) ;
281
281
}
282
282
} ) ;
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
+ } ) ;
283
303
describe ( 'issues #19' , ( ) => {
284
304
it ( 'string field' , async ( ) => {
285
305
const schema = buildSchema ( /* GraphQL */ `
You can’t perform that action at this time.
0 commit comments