@@ -35,6 +35,8 @@ type Options<T = TypeNode> = {
3535 fieldGeneration ?: TypeFieldMap ;
3636 enumsAsTypes ?: boolean ;
3737 useImplementingTypes : boolean ;
38+ defaultNullableToNull : boolean ;
39+ nonNull : boolean ;
3840} ;
3941
4042const convertName = ( value : string , fn : ( v : string ) => string , transformUnderscore : boolean ) : string => {
@@ -236,6 +238,9 @@ const handleValueGeneration = (
236238 if ( customScalar ) {
237239 return getCustomValue ( customScalar , opts ) ;
238240 }
241+ if ( opts . defaultNullableToNull && ! opts . nonNull ) {
242+ return null ;
243+ }
239244 return baseGenerator ( ) ;
240245} ;
241246
@@ -375,8 +380,14 @@ const generateMockValue = (opts: Options): string | number | boolean => {
375380 return generateMockValue ( {
376381 ...opts ,
377382 currentType : opts . currentType . type ,
383+ nonNull : true ,
378384 } ) ;
379385 case 'ListType' : {
386+ const hasOverride = opts . fieldGeneration ?. [ opts . typeName ] ?. [ opts . fieldName ] ;
387+ if ( ! hasOverride && opts . defaultNullableToNull && ! opts . nonNull ) {
388+ return null ;
389+ }
390+
380391 const listElements = Array . from ( { length : opts . listElementCount } , ( _ , index ) =>
381392 generateMockValue ( {
382393 ...opts ,
@@ -513,6 +524,7 @@ export interface TypescriptMocksPluginConfig {
513524 locale ?: string ;
514525 enumsAsTypes ?: boolean ;
515526 useImplementingTypes ?: boolean ;
527+ defaultNullableToNull ?: boolean ;
516528}
517529
518530interface TypeItem {
@@ -560,6 +572,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
560572 const generateLibrary = config . generateLibrary || 'casual' ;
561573 const enumsAsTypes = config . enumsAsTypes ?? false ;
562574 const useImplementingTypes = config . useImplementingTypes ?? false ;
575+ const defaultNullableToNull = config . defaultNullableToNull ?? false ;
563576
564577 if ( generateLibrary === 'faker' && config . locale ) {
565578 faker . setLocale ( config . locale ) ;
@@ -639,6 +652,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
639652 fieldGeneration : config . fieldGeneration ,
640653 enumsAsTypes,
641654 useImplementingTypes,
655+ defaultNullableToNull,
656+ nonNull : false ,
642657 } ) ;
643658
644659 return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -673,6 +688,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
673688 fieldGeneration : config . fieldGeneration ,
674689 enumsAsTypes,
675690 useImplementingTypes,
691+ defaultNullableToNull,
692+ nonNull : false ,
676693 } ) ;
677694
678695 return ` ${ field . name . value } : overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ,` ;
0 commit comments