@@ -12,6 +12,7 @@ type NamingConvention = 'change-case-all#pascalCase' | 'keep' | string;
12
12
type Options < T = TypeNode > = {
13
13
typeName : string ;
14
14
fieldName : string ;
15
+ generatorMode : 'input' | 'output' ;
15
16
types : TypeItem [ ] ;
16
17
typeNamesConvention : NamingConvention ;
17
18
enumValuesConvention : NamingConvention ;
@@ -90,13 +91,21 @@ const hashedString = (value: string) => {
90
91
return hash ;
91
92
} ;
92
93
93
- const getGeneratorDefinition = ( value : GeneratorDefinition | GeneratorName ) : GeneratorDefinition => {
94
+ const getGeneratorDefinition = (
95
+ value : GeneratorOptions | InputOutputGeneratorOptions ,
96
+ generatorMode : Options [ 'generatorMode' ] ,
97
+ ) : GeneratorDefinition => {
94
98
if ( typeof value === 'string' ) {
95
99
return {
96
100
generator : value ,
97
101
arguments : [ ] ,
98
102
} ;
99
103
}
104
+
105
+ if ( 'input' in value && 'output' in value ) {
106
+ return getGeneratorDefinition ( value [ generatorMode ] , generatorMode ) ;
107
+ }
108
+
100
109
return value ;
101
110
} ;
102
111
@@ -246,12 +255,18 @@ const handleValueGeneration = (
246
255
if ( opts . fieldGeneration ) {
247
256
// Check for a specific generation for the type & field
248
257
if ( opts . typeName in opts . fieldGeneration && opts . fieldName in opts . fieldGeneration [ opts . typeName ] ) {
249
- const generatorDefinition = getGeneratorDefinition ( opts . fieldGeneration [ opts . typeName ] [ opts . fieldName ] ) ;
258
+ const generatorDefinition = getGeneratorDefinition (
259
+ opts . fieldGeneration [ opts . typeName ] [ opts . fieldName ] ,
260
+ opts . generatorMode ,
261
+ ) ;
250
262
return getCustomValue ( generatorDefinition , opts ) ;
251
263
}
252
264
// Check for a general field generation definition
253
265
if ( '_all' in opts . fieldGeneration && opts . fieldName in opts . fieldGeneration [ '_all' ] ) {
254
- const generatorDefinition = getGeneratorDefinition ( opts . fieldGeneration [ '_all' ] [ opts . fieldName ] ) ;
266
+ const generatorDefinition = getGeneratorDefinition (
267
+ opts . fieldGeneration [ '_all' ] [ opts . fieldName ] ,
268
+ opts . generatorMode ,
269
+ ) ;
255
270
return getCustomValue ( generatorDefinition , opts ) ;
256
271
}
257
272
}
@@ -290,25 +305,36 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
290
305
if ( ! opts . dynamicValues ) mockValueGenerator . seed ( hashedString ( opts . typeName + opts . fieldName ) ) ;
291
306
const name = opts . currentType . name . value ;
292
307
const casedName = createNameConverter ( opts . typeNamesConvention , opts . transformUnderscore ) ( name ) ;
308
+
293
309
switch ( name ) {
294
310
case 'String' : {
295
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'String' ] ) : null ;
311
+ const customScalar = opts . customScalars
312
+ ? getGeneratorDefinition ( opts . customScalars [ 'String' ] , opts . generatorMode )
313
+ : null ;
296
314
return handleValueGeneration ( opts , customScalar , mockValueGenerator . word ) ;
297
315
}
298
316
case 'Float' : {
299
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'Float' ] ) : null ;
317
+ const customScalar = opts . customScalars
318
+ ? getGeneratorDefinition ( opts . customScalars [ 'Float' ] , opts . generatorMode )
319
+ : null ;
300
320
return handleValueGeneration ( opts , customScalar , mockValueGenerator . float ) ;
301
321
}
302
322
case 'ID' : {
303
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'ID' ] ) : null ;
323
+ const customScalar = opts . customScalars
324
+ ? getGeneratorDefinition ( opts . customScalars [ 'ID' ] , opts . generatorMode )
325
+ : null ;
304
326
return handleValueGeneration ( opts , customScalar , mockValueGenerator . uuid ) ;
305
327
}
306
328
case 'Boolean' : {
307
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'Boolean' ] ) : null ;
329
+ const customScalar = opts . customScalars
330
+ ? getGeneratorDefinition ( opts . customScalars [ 'Boolean' ] , opts . generatorMode )
331
+ : null ;
308
332
return handleValueGeneration ( opts , customScalar , mockValueGenerator . boolean ) ;
309
333
}
310
334
case 'Int' : {
311
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'Int' ] ) : null ;
335
+ const customScalar = opts . customScalars
336
+ ? getGeneratorDefinition ( opts . customScalars [ 'Int' ] , opts . generatorMode )
337
+ : null ;
312
338
return handleValueGeneration ( opts , customScalar , mockValueGenerator . integer ) ;
313
339
}
314
340
default : {
@@ -345,7 +371,7 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
345
371
} ) ;
346
372
case 'scalar' : {
347
373
const customScalar = opts . customScalars
348
- ? getGeneratorDefinition ( opts . customScalars [ foundType . name ] )
374
+ ? getGeneratorDefinition ( opts . customScalars [ foundType . name ] , opts . generatorMode )
349
375
: null ;
350
376
351
377
// it's a scalar, let's use a string as a value if there is no custom
@@ -559,9 +585,13 @@ type GeneratorDefinition = {
559
585
} ;
560
586
} ;
561
587
type GeneratorOptions = GeneratorName | GeneratorDefinition ;
588
+ type InputOutputGeneratorOptions = {
589
+ input : GeneratorOptions ;
590
+ output : GeneratorOptions ;
591
+ } ;
562
592
563
593
type ScalarMap = {
564
- [ name : string ] : GeneratorOptions ;
594
+ [ name : string ] : GeneratorOptions | InputOutputGeneratorOptions ;
565
595
} ;
566
596
567
597
type TypeFieldMap = {
@@ -728,6 +758,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
728
758
const value = generateMockValue ( {
729
759
typeName,
730
760
fieldName,
761
+ generatorMode : 'output' ,
731
762
currentType : node . type ,
732
763
...sharedGenerateMockOpts ,
733
764
} ) ;
@@ -753,6 +784,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
753
784
typeName : fieldName ,
754
785
fieldName : field . name . value ,
755
786
currentType : field . type ,
787
+ generatorMode : 'input' ,
756
788
...sharedGenerateMockOpts ,
757
789
} ) ;
758
790
@@ -764,6 +796,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
764
796
typeName : fieldName ,
765
797
fieldName : field . name . value ,
766
798
currentType : field . type ,
799
+ generatorMode : 'input' ,
767
800
...sharedGenerateMockOpts ,
768
801
} ) ;
769
802
0 commit comments