@@ -12,6 +12,7 @@ type NamingConvention = 'change-case-all#pascalCase' | 'keep' | string;
1212type Options < T = TypeNode > = {
1313 typeName : string ;
1414 fieldName : string ;
15+ generatorMode : 'input' | 'output' ;
1516 types : TypeItem [ ] ;
1617 typeNamesConvention : NamingConvention ;
1718 enumValuesConvention : NamingConvention ;
@@ -90,14 +91,26 @@ const hashedString = (value: string) => {
9091 return hash ;
9192} ;
9293
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' ) {
95107 return {
96- generator : value ,
108+ generator : opts ,
97109 arguments : [ ] ,
98110 } ;
99111 }
100- return value ;
112+
113+ return opts ;
101114} ;
102115
103116const getCasualCustomValue = (
@@ -246,12 +259,18 @@ const handleValueGeneration = (
246259 if ( opts . fieldGeneration ) {
247260 // Check for a specific generation for the type & field
248261 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+ ) ;
250266 return getCustomValue ( generatorDefinition , opts ) ;
251267 }
252268 // Check for a general field generation definition
253269 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+ ) ;
255274 return getCustomValue ( generatorDefinition , opts ) ;
256275 }
257276 }
@@ -290,25 +309,36 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
290309 if ( ! opts . dynamicValues ) mockValueGenerator . seed ( hashedString ( opts . typeName + opts . fieldName ) ) ;
291310 const name = opts . currentType . name . value ;
292311 const casedName = createNameConverter ( opts . typeNamesConvention , opts . transformUnderscore ) ( name ) ;
312+
293313 switch ( name ) {
294314 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 ;
296318 return handleValueGeneration ( opts , customScalar , mockValueGenerator . word ) ;
297319 }
298320 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 ;
300324 return handleValueGeneration ( opts , customScalar , mockValueGenerator . float ) ;
301325 }
302326 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 ;
304330 return handleValueGeneration ( opts , customScalar , mockValueGenerator . uuid ) ;
305331 }
306332 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 ;
308336 return handleValueGeneration ( opts , customScalar , mockValueGenerator . boolean ) ;
309337 }
310338 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 ;
312342 return handleValueGeneration ( opts , customScalar , mockValueGenerator . integer ) ;
313343 }
314344 default : {
@@ -345,7 +375,7 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
345375 } ) ;
346376 case 'scalar' : {
347377 const customScalar = opts . customScalars
348- ? getGeneratorDefinition ( opts . customScalars [ foundType . name ] )
378+ ? getGeneratorDefinition ( opts . customScalars [ foundType . name ] , opts . generatorMode )
349379 : null ;
350380
351381 // it's a scalar, let's use a string as a value if there is no custom
@@ -559,9 +589,18 @@ type GeneratorDefinition = {
559589 } ;
560590} ;
561591type 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 ;
562601
563602type ScalarMap = {
564- [ name : string ] : GeneratorOptions ;
603+ [ name : string ] : GeneratorOptions | InputOutputGeneratorOptions ;
565604} ;
566605
567606type TypeFieldMap = {
@@ -728,6 +767,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
728767 const value = generateMockValue ( {
729768 typeName,
730769 fieldName,
770+ generatorMode : 'output' ,
731771 currentType : node . type ,
732772 ...sharedGenerateMockOpts ,
733773 } ) ;
@@ -753,6 +793,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
753793 typeName : fieldName ,
754794 fieldName : field . name . value ,
755795 currentType : field . type ,
796+ generatorMode : 'input' ,
756797 ...sharedGenerateMockOpts ,
757798 } ) ;
758799
@@ -764,6 +805,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
764805 typeName : fieldName ,
765806 fieldName : field . name . value ,
766807 currentType : field . type ,
808+ generatorMode : 'input' ,
767809 ...sharedGenerateMockOpts ,
768810 } ) ;
769811
0 commit comments