@@ -35,6 +35,8 @@ type Options<T = TypeNode> = {
35
35
fieldGeneration ?: TypeFieldMap ;
36
36
enumsAsTypes ?: boolean ;
37
37
useImplementingTypes : boolean ;
38
+ defaultNullableToNull : boolean ;
39
+ nonNull : boolean ;
38
40
} ;
39
41
40
42
const convertName = ( value : string , fn : ( v : string ) => string , transformUnderscore : boolean ) : string => {
@@ -236,6 +238,9 @@ const handleValueGeneration = (
236
238
if ( customScalar ) {
237
239
return getCustomValue ( customScalar , opts ) ;
238
240
}
241
+ if ( opts . defaultNullableToNull && ! opts . nonNull ) {
242
+ return null ;
243
+ }
239
244
return baseGenerator ( ) ;
240
245
} ;
241
246
@@ -375,8 +380,14 @@ const generateMockValue = (opts: Options): string | number | boolean => {
375
380
return generateMockValue ( {
376
381
...opts ,
377
382
currentType : opts . currentType . type ,
383
+ nonNull : true ,
378
384
} ) ;
379
385
case 'ListType' : {
386
+ const hasOverride = opts . fieldGeneration ?. [ opts . typeName ] ?. [ opts . fieldName ] ;
387
+ if ( ! hasOverride && opts . defaultNullableToNull && ! opts . nonNull ) {
388
+ return null ;
389
+ }
390
+
380
391
const listElements = Array . from ( { length : opts . listElementCount } , ( _ , index ) =>
381
392
generateMockValue ( {
382
393
...opts ,
@@ -513,6 +524,7 @@ export interface TypescriptMocksPluginConfig {
513
524
locale ?: string ;
514
525
enumsAsTypes ?: boolean ;
515
526
useImplementingTypes ?: boolean ;
527
+ defaultNullableToNull ?: boolean ;
516
528
}
517
529
518
530
interface TypeItem {
@@ -560,6 +572,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
560
572
const generateLibrary = config . generateLibrary || 'casual' ;
561
573
const enumsAsTypes = config . enumsAsTypes ?? false ;
562
574
const useImplementingTypes = config . useImplementingTypes ?? false ;
575
+ const defaultNullableToNull = config . defaultNullableToNull ?? false ;
563
576
564
577
if ( generateLibrary === 'faker' && config . locale ) {
565
578
faker . setLocale ( config . locale ) ;
@@ -639,6 +652,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
639
652
fieldGeneration : config . fieldGeneration ,
640
653
enumsAsTypes,
641
654
useImplementingTypes,
655
+ defaultNullableToNull,
656
+ nonNull : false ,
642
657
} ) ;
643
658
644
659
return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -673,6 +688,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
673
688
fieldGeneration : config . fieldGeneration ,
674
689
enumsAsTypes,
675
690
useImplementingTypes,
691
+ defaultNullableToNull,
692
+ nonNull : false ,
676
693
} ) ;
677
694
678
695
return ` ${ field . name . value } : overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ,` ;
0 commit comments