Skip to content

Commit f636c68

Browse files
author
Jimmy Paolini
authored
feat: listElementCount option (#88)
Add an option to configure how many list elements should be generated in mocks.
1 parent b3e55ba commit f636c68

File tree

4 files changed

+392
-18
lines changed

4 files changed

+392
-18
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ specific type in a particular call stack, subsequent resolutions will return an
2828
The prefix to add to the mock function name. Cannot be empty since it will clash with the associated
2929
typescript definition from `@graphql-codegen/typescript`
3030

31+
### listElementCount (`number`, defaultValue: `1`)
32+
33+
How many elements should be generated for lists. For example, with `listElementCount: 3` a schema field `names: [String!]!` would generate `3` names in each mock.
34+
3135
### enumValues (`string`, defaultValue: `pascal-case#pascalCase`)
3236

3337
Changes the case of the enums. Accepts `upper-case#upperCase`, `pascal-case#pascalCase` or `keep`

src/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ASTKindToNode, NamedTypeNode, parse, printSchema, TypeNode } from 'graphql';
1+
import { ASTKindToNode, ListTypeNode, NamedTypeNode, parse, printSchema, TypeNode } from 'graphql';
22
import casual from 'casual';
33
import { PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers';
44
import { pascalCase } from 'pascal-case';
@@ -21,6 +21,7 @@ type Options<T = TypeNode> = {
2121
currentType: T;
2222
customScalars?: ScalarMap;
2323
transformUnderscore: boolean;
24+
listElementCount?: number;
2425
dynamicValues?: boolean;
2526
};
2627

@@ -216,11 +217,14 @@ const generateMockValue = (opts: Options): string | number | boolean => {
216217
currentType: opts.currentType.type,
217218
});
218219
case 'ListType': {
219-
const value = generateMockValue({
220-
...opts,
221-
currentType: opts.currentType.type,
222-
});
223-
return `[${value}]`;
220+
const listElements = Array.from({ length: opts.listElementCount }, (_, index) =>
221+
generateMockValue({
222+
...opts,
223+
fieldName: opts.fieldName + index,
224+
currentType: (opts.currentType as ListTypeNode).type,
225+
}),
226+
);
227+
return `[${listElements.join(', ')}]`;
224228
}
225229
default:
226230
throw new Error('unreached');
@@ -326,6 +330,7 @@ export interface TypescriptMocksPluginConfig {
326330
typesPrefix?: string;
327331
enumsPrefix?: string;
328332
transformUnderscore?: boolean;
333+
listElementCount?: number;
329334
dynamicValues?: boolean;
330335
}
331336

@@ -365,6 +370,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
365370
const enumValuesConvention = config.enumValues || 'pascal-case#pascalCase';
366371
const typenamesConvention = config.typenames || 'pascal-case#pascalCase';
367372
const transformUnderscore = config.transformUnderscore ?? true;
373+
const listElementCount = config.listElementCount > 0 ? config.listElementCount : 1;
368374
// List of types that are enums
369375
const types: TypeItem[] = [];
370376
const visitor: VisitorType = {
@@ -407,6 +413,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
407413
currentType: node.type,
408414
customScalars: config.scalars,
409415
transformUnderscore,
416+
listElementCount,
410417
dynamicValues: config.dynamicValues,
411418
});
412419

0 commit comments

Comments
 (0)