1- import { ASTKindToNode , NamedTypeNode , parse , printSchema , TypeNode } from 'graphql' ;
1+ import { ASTKindToNode , ListTypeNode , NamedTypeNode , parse , printSchema , TypeNode } from 'graphql' ;
22import casual from 'casual' ;
33import { PluginFunction , oldVisit } from '@graphql-codegen/plugin-helpers' ;
44import { 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