1
- import { ASTKindToNode , NamedTypeNode , parse , printSchema , TypeNode } from 'graphql' ;
1
+ import { ASTKindToNode , ListTypeNode , NamedTypeNode , parse , printSchema , TypeNode } from 'graphql' ;
2
2
import casual from 'casual' ;
3
3
import { PluginFunction , oldVisit } from '@graphql-codegen/plugin-helpers' ;
4
4
import { pascalCase } from 'pascal-case' ;
@@ -21,6 +21,7 @@ type Options<T = TypeNode> = {
21
21
currentType : T ;
22
22
customScalars ?: ScalarMap ;
23
23
transformUnderscore : boolean ;
24
+ listElementCount ?: number ;
24
25
dynamicValues ?: boolean ;
25
26
} ;
26
27
@@ -216,11 +217,14 @@ const generateMockValue = (opts: Options): string | number | boolean => {
216
217
currentType : opts . currentType . type ,
217
218
} ) ;
218
219
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 ( ', ' ) } ]` ;
224
228
}
225
229
default :
226
230
throw new Error ( 'unreached' ) ;
@@ -326,6 +330,7 @@ export interface TypescriptMocksPluginConfig {
326
330
typesPrefix ?: string ;
327
331
enumsPrefix ?: string ;
328
332
transformUnderscore ?: boolean ;
333
+ listElementCount ?: number ;
329
334
dynamicValues ?: boolean ;
330
335
}
331
336
@@ -365,6 +370,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
365
370
const enumValuesConvention = config . enumValues || 'pascal-case#pascalCase' ;
366
371
const typenamesConvention = config . typenames || 'pascal-case#pascalCase' ;
367
372
const transformUnderscore = config . transformUnderscore ?? true ;
373
+ const listElementCount = config . listElementCount > 0 ? config . listElementCount : 1 ;
368
374
// List of types that are enums
369
375
const types : TypeItem [ ] = [ ] ;
370
376
const visitor : VisitorType = {
@@ -407,6 +413,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
407
413
currentType : node . type ,
408
414
customScalars : config . scalars ,
409
415
transformUnderscore,
416
+ listElementCount,
410
417
dynamicValues : config . dynamicValues ,
411
418
} ) ;
412
419
0 commit comments