Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,13 @@ When enabled, values will be generated dynamically when the mock function is cal
### useImplementingTypes (`boolean`, defaultValue: `false`)
When enabled, it will support the useImplementingTypes GraphQL codegen configuration.
- When a GraphQL interface is used for a field, this flag will use the implementing types, instead of the interface itself.
### defaultNullableToNull (`boolean`, defaultValue: `false`)
When enabled, it will set all nullable fields to null per default instead of generating a value.
### fieldGeneration (`{ [typeName: string]: { [fieldName: string]: GeneratorOptions } }`, defaultValue: `undefined`)
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:
Expand Down
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Options<T = TypeNode> = {
fieldGeneration?: TypeFieldMap;
enumsAsTypes?: boolean;
useImplementingTypes: boolean;
defaultNullableToNull: boolean;
nonNull: boolean;
};

const convertName = (value: string, fn: (v: string) => string, transformUnderscore: boolean): string => {
Expand Down Expand Up @@ -236,6 +238,9 @@ const handleValueGeneration = (
if (customScalar) {
return getCustomValue(customScalar, opts);
}
if (opts.defaultNullableToNull && !opts.nonNull) {
return null;
}
return baseGenerator();
};

Expand Down Expand Up @@ -375,8 +380,14 @@ const generateMockValue = (opts: Options): string | number | boolean => {
return generateMockValue({
...opts,
currentType: opts.currentType.type,
nonNull: true,
});
case 'ListType': {
const hasOverride = opts.fieldGeneration?.[opts.typeName]?.[opts.fieldName];
if (!hasOverride && opts.defaultNullableToNull && !opts.nonNull) {
return null;
}

const listElements = Array.from({ length: opts.listElementCount }, (_, index) =>
generateMockValue({
...opts,
Expand Down Expand Up @@ -513,6 +524,7 @@ export interface TypescriptMocksPluginConfig {
locale?: string;
enumsAsTypes?: boolean;
useImplementingTypes?: boolean;
defaultNullableToNull?: boolean;
}

interface TypeItem {
Expand Down Expand Up @@ -560,6 +572,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
const generateLibrary = config.generateLibrary || 'casual';
const enumsAsTypes = config.enumsAsTypes ?? false;
const useImplementingTypes = config.useImplementingTypes ?? false;
const defaultNullableToNull = config.defaultNullableToNull ?? false;

if (generateLibrary === 'faker' && config.locale) {
faker.setLocale(config.locale);
Expand Down Expand Up @@ -639,6 +652,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
fieldGeneration: config.fieldGeneration,
enumsAsTypes,
useImplementingTypes,
defaultNullableToNull,
nonNull: false,
});

return ` ${fieldName}: overrides && overrides.hasOwnProperty('${fieldName}') ? overrides.${fieldName}! : ${value},`;
Expand Down Expand Up @@ -673,6 +688,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
fieldGeneration: config.fieldGeneration,
enumsAsTypes,
useImplementingTypes,
defaultNullableToNull,
nonNull: false,
});

return ` ${field.name.value}: overrides && overrides.hasOwnProperty('${field.name.value}') ? overrides.${field.name.value}! : ${value},`;
Expand Down
Loading