Skip to content

Commit 992200b

Browse files
authored
implement defaultNullableToNull config option (ardeois#128) (ardeois#130)
1 parent 2ecc1b2 commit 992200b

File tree

4 files changed

+272
-0
lines changed

4 files changed

+272
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,13 @@ When enabled, values will be generated dynamically when the mock function is cal
160160
### useImplementingTypes (`boolean`, defaultValue: `false`)
161161
162162
When enabled, it will support the useImplementingTypes GraphQL codegen configuration.
163+
163164
- When a GraphQL interface is used for a field, this flag will use the implementing types, instead of the interface itself.
164165
166+
### defaultNullableToNull (`boolean`, defaultValue: `false`)
167+
168+
When enabled, it will set all nullable fields to null per default instead of generating a value.
169+
165170
### fieldGeneration (`{ [typeName: string]: { [fieldName: string]: GeneratorOptions } }`, defaultValue: `undefined`)
166171
167172
This setting allows you to add specific generation to a field for a given type. For example if you have a type called `User` and a field called `birthDate` you can override any generated value there as follows:

src/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -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

4042
const 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

518530
interface 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

Comments
 (0)