@@ -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,14 +91,26 @@ const hashedString = (value: string) => {
90
91
return hash ;
91
92
} ;
92
93
93
- const getGeneratorDefinition = ( value : GeneratorDefinition | GeneratorName ) : GeneratorDefinition => {
94
- if ( typeof value === 'string' ) {
94
+ const getGeneratorDefinition = (
95
+ opts : GeneratorOptions | InputOutputGeneratorOptions ,
96
+ generatorMode : Options [ 'generatorMode' ] ,
97
+ ) : GeneratorDefinition => {
98
+ if ( isAnInputOutputGeneratorOptions ( opts ) ) {
99
+ return buildGeneratorDefinition ( opts [ generatorMode ] ) ;
100
+ }
101
+
102
+ return buildGeneratorDefinition ( opts ) ;
103
+ } ;
104
+
105
+ const buildGeneratorDefinition = ( opts : GeneratorOptions ) => {
106
+ if ( typeof opts === 'string' ) {
95
107
return {
96
- generator : value ,
108
+ generator : opts ,
97
109
arguments : [ ] ,
98
110
} ;
99
111
}
100
- return value ;
112
+
113
+ return opts ;
101
114
} ;
102
115
103
116
const getCasualCustomValue = (
@@ -246,12 +259,18 @@ const handleValueGeneration = (
246
259
if ( opts . fieldGeneration ) {
247
260
// Check for a specific generation for the type & field
248
261
if ( opts . typeName in opts . fieldGeneration && opts . fieldName in opts . fieldGeneration [ opts . typeName ] ) {
249
- const generatorDefinition = getGeneratorDefinition ( opts . fieldGeneration [ opts . typeName ] [ opts . fieldName ] ) ;
262
+ const generatorDefinition = getGeneratorDefinition (
263
+ opts . fieldGeneration [ opts . typeName ] [ opts . fieldName ] ,
264
+ opts . generatorMode ,
265
+ ) ;
250
266
return getCustomValue ( generatorDefinition , opts ) ;
251
267
}
252
268
// Check for a general field generation definition
253
269
if ( '_all' in opts . fieldGeneration && opts . fieldName in opts . fieldGeneration [ '_all' ] ) {
254
- const generatorDefinition = getGeneratorDefinition ( opts . fieldGeneration [ '_all' ] [ opts . fieldName ] ) ;
270
+ const generatorDefinition = getGeneratorDefinition (
271
+ opts . fieldGeneration [ '_all' ] [ opts . fieldName ] ,
272
+ opts . generatorMode ,
273
+ ) ;
255
274
return getCustomValue ( generatorDefinition , opts ) ;
256
275
}
257
276
}
@@ -290,25 +309,36 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
290
309
if ( ! opts . dynamicValues ) mockValueGenerator . seed ( hashedString ( opts . typeName + opts . fieldName ) ) ;
291
310
const name = opts . currentType . name . value ;
292
311
const casedName = createNameConverter ( opts . typeNamesConvention , opts . transformUnderscore ) ( name ) ;
312
+
293
313
switch ( name ) {
294
314
case 'String' : {
295
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'String' ] ) : null ;
315
+ const customScalar = opts . customScalars
316
+ ? getGeneratorDefinition ( opts . customScalars [ 'String' ] , opts . generatorMode )
317
+ : null ;
296
318
return handleValueGeneration ( opts , customScalar , mockValueGenerator . word ) ;
297
319
}
298
320
case 'Float' : {
299
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'Float' ] ) : null ;
321
+ const customScalar = opts . customScalars
322
+ ? getGeneratorDefinition ( opts . customScalars [ 'Float' ] , opts . generatorMode )
323
+ : null ;
300
324
return handleValueGeneration ( opts , customScalar , mockValueGenerator . float ) ;
301
325
}
302
326
case 'ID' : {
303
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'ID' ] ) : null ;
327
+ const customScalar = opts . customScalars
328
+ ? getGeneratorDefinition ( opts . customScalars [ 'ID' ] , opts . generatorMode )
329
+ : null ;
304
330
return handleValueGeneration ( opts , customScalar , mockValueGenerator . uuid ) ;
305
331
}
306
332
case 'Boolean' : {
307
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'Boolean' ] ) : null ;
333
+ const customScalar = opts . customScalars
334
+ ? getGeneratorDefinition ( opts . customScalars [ 'Boolean' ] , opts . generatorMode )
335
+ : null ;
308
336
return handleValueGeneration ( opts , customScalar , mockValueGenerator . boolean ) ;
309
337
}
310
338
case 'Int' : {
311
- const customScalar = opts . customScalars ? getGeneratorDefinition ( opts . customScalars [ 'Int' ] ) : null ;
339
+ const customScalar = opts . customScalars
340
+ ? getGeneratorDefinition ( opts . customScalars [ 'Int' ] , opts . generatorMode )
341
+ : null ;
312
342
return handleValueGeneration ( opts , customScalar , mockValueGenerator . integer ) ;
313
343
}
314
344
default : {
@@ -345,7 +375,7 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
345
375
} ) ;
346
376
case 'scalar' : {
347
377
const customScalar = opts . customScalars
348
- ? getGeneratorDefinition ( opts . customScalars [ foundType . name ] )
378
+ ? getGeneratorDefinition ( opts . customScalars [ foundType . name ] , opts . generatorMode )
349
379
: null ;
350
380
351
381
// it's a scalar, let's use a string as a value if there is no custom
@@ -559,9 +589,18 @@ type GeneratorDefinition = {
559
589
} ;
560
590
} ;
561
591
type GeneratorOptions = GeneratorName | GeneratorDefinition ;
592
+ type InputOutputGeneratorOptions = {
593
+ input : GeneratorOptions ;
594
+ output : GeneratorOptions ;
595
+ } ;
596
+
597
+ const isAnInputOutputGeneratorOptions = (
598
+ opts : GeneratorOptions | InputOutputGeneratorOptions ,
599
+ ) : opts is InputOutputGeneratorOptions =>
600
+ opts !== undefined && typeof opts !== 'string' && 'input' in opts && 'output' in opts ;
562
601
563
602
type ScalarMap = {
564
- [ name : string ] : GeneratorOptions ;
603
+ [ name : string ] : GeneratorOptions | InputOutputGeneratorOptions ;
565
604
} ;
566
605
567
606
type TypeFieldMap = {
@@ -728,6 +767,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
728
767
const value = generateMockValue ( {
729
768
typeName,
730
769
fieldName,
770
+ generatorMode : 'output' ,
731
771
currentType : node . type ,
732
772
...sharedGenerateMockOpts ,
733
773
} ) ;
@@ -753,6 +793,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
753
793
typeName : fieldName ,
754
794
fieldName : field . name . value ,
755
795
currentType : field . type ,
796
+ generatorMode : 'input' ,
756
797
...sharedGenerateMockOpts ,
757
798
} ) ;
758
799
@@ -764,6 +805,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
764
805
typeName : fieldName ,
765
806
fieldName : field . name . value ,
766
807
currentType : field . type ,
808
+ generatorMode : 'input' ,
767
809
...sharedGenerateMockOpts ,
768
810
} ) ;
769
811
0 commit comments