@@ -84,6 +84,13 @@ export interface DefinitionsGeneratorOptions {
84
84
* @default false
85
85
*/
86
86
enumsAsTypes ?: boolean ;
87
+
88
+ /**
89
+ * If provided, specifies a function to transform type names.
90
+ * @example (name) => `${name}Schema`
91
+ * @default undefined
92
+ */
93
+ typeName ?: ( name : string ) => string ;
87
94
}
88
95
89
96
@Injectable ( )
@@ -176,7 +183,7 @@ export class GraphQLAstExplorer {
176
183
return this . toEnumDefinitionStructure ( item , options ) ;
177
184
case 'UnionTypeDefinition' :
178
185
case 'UnionTypeExtension' :
179
- return this . toUnionDefinitionStructure ( item ) ;
186
+ return this . toUnionDefinitionStructure ( item , options ) ;
180
187
}
181
188
}
182
189
@@ -230,10 +237,14 @@ export class GraphQLAstExplorer {
230
237
? tsMorphLib . StructureKind . Class
231
238
: tsMorphLib . StructureKind . Interface ;
232
239
const isRoot = this . root . indexOf ( parentName ) >= 0 ;
240
+ // Don't transform root type names (Query, Mutation, Subscription)
241
+ const transformedName = isRoot
242
+ ? parentName
243
+ : this . getTransformedTypeName ( parentName , options ) ;
233
244
const parentStructure :
234
245
| ClassDeclarationStructure
235
246
| InterfaceDeclarationStructure = {
236
- name : this . addSymbolIfRoot ( upperFirst ( parentName ) ) ,
247
+ name : this . addSymbolIfRoot ( upperFirst ( transformedName ) ) ,
237
248
isExported : true ,
238
249
isAbstract : isRoot && mode === 'class' ,
239
250
kind : structureKind ,
@@ -245,11 +256,21 @@ export class GraphQLAstExplorer {
245
256
if ( interfaces ) {
246
257
if ( mode === 'class' ) {
247
258
( parentStructure as ClassDeclarationStructure ) . implements = interfaces
248
- . map ( ( element ) => get ( element , 'name.value' ) )
259
+ . map ( ( element ) => {
260
+ const interfaceName = get ( element , 'name.value' ) ;
261
+ return interfaceName
262
+ ? this . getTransformedTypeName ( interfaceName , options )
263
+ : null ;
264
+ } )
249
265
. filter ( Boolean ) ;
250
266
} else {
251
267
parentStructure . extends = interfaces
252
- . map ( ( element ) => get ( element , 'name.value' ) )
268
+ . map ( ( element ) => {
269
+ const interfaceName = get ( element , 'name.value' ) ;
270
+ return interfaceName
271
+ ? this . getTransformedTypeName ( interfaceName , options )
272
+ : null ;
273
+ } )
253
274
. filter ( Boolean ) ;
254
275
}
255
276
}
@@ -395,7 +416,11 @@ export class GraphQLAstExplorer {
395
416
getType ( typeName : string , options : DefinitionsGeneratorOptions ) : string {
396
417
const defaults = this . getDefaultTypes ( options ) ;
397
418
const isDefault = defaults [ typeName ] ;
398
- return isDefault ? defaults [ typeName ] : upperFirst ( typeName ) ;
419
+ if ( isDefault ) {
420
+ return defaults [ typeName ] ;
421
+ }
422
+ const transformedName = this . getTransformedTypeName ( typeName , options ) ;
423
+ return upperFirst ( transformedName ) ;
399
424
}
400
425
401
426
getDefaultTypes ( options : DefinitionsGeneratorOptions ) : {
@@ -444,9 +469,11 @@ export class GraphQLAstExplorer {
444
469
const mappedTypeName =
445
470
typeof typeMapping === 'string' ? typeMapping : typeMapping ?. name ;
446
471
472
+ const transformedName = this . getTransformedTypeName ( name , options ) ;
473
+
447
474
return {
448
475
kind : tsMorphLib . StructureKind . TypeAlias ,
449
- name,
476
+ name : transformedName ,
450
477
type : mappedTypeName ?? options . defaultScalarType ?? 'any' ,
451
478
isExported : true ,
452
479
} ;
@@ -460,13 +487,15 @@ export class GraphQLAstExplorer {
460
487
if ( ! name ) {
461
488
return undefined ;
462
489
}
490
+ const transformedName = this . getTransformedTypeName ( name , options ) ;
491
+
463
492
if ( options . enumsAsTypes ) {
464
493
const values = item . values . map (
465
494
( value ) => `"${ get ( value , 'name.value' ) } "` ,
466
495
) ;
467
496
return {
468
497
kind : tsMorphLib . StructureKind . TypeAlias ,
469
- name,
498
+ name : transformedName ,
470
499
type : values . join ( ' | ' ) ,
471
500
isExported : true ,
472
501
} ;
@@ -477,26 +506,30 @@ export class GraphQLAstExplorer {
477
506
} ) ) ;
478
507
return {
479
508
kind : tsMorphLib . StructureKind . Enum ,
480
- name,
509
+ name : transformedName ,
481
510
members,
482
511
isExported : true ,
483
512
} ;
484
513
}
485
514
486
515
toUnionDefinitionStructure (
487
516
item : UnionTypeDefinitionNode | UnionTypeExtensionNode ,
517
+ options : DefinitionsGeneratorOptions ,
488
518
) : TypeAliasDeclarationStructure {
489
519
const name = get ( item , 'name.value' ) ;
490
520
if ( ! name ) {
491
521
return undefined ;
492
522
}
493
- const types : string [ ] = map ( item . types , ( value ) =>
494
- get ( value , 'name.value' ) ,
495
- ) ;
523
+ const transformedName = this . getTransformedTypeName ( name , options ) ;
524
+
525
+ const types : string [ ] = map ( item . types , ( value ) => {
526
+ const typeName = get ( value , 'name.value' ) ;
527
+ return typeName ? this . getTransformedTypeName ( typeName , options ) : null ;
528
+ } ) . filter ( Boolean ) ;
496
529
497
530
return {
498
531
kind : tsMorphLib . StructureKind . TypeAlias ,
499
- name,
532
+ name : transformedName ,
500
533
type : types . join ( ' | ' ) ,
501
534
isExported : true ,
502
535
} ;
@@ -509,4 +542,14 @@ export class GraphQLAstExplorer {
509
542
isRoot ( name : string ) : boolean {
510
543
return [ 'IQuery' , 'IMutation' , 'ISubscription' ] . indexOf ( name ) >= 0 ;
511
544
}
545
+
546
+ private getTransformedTypeName (
547
+ name : string ,
548
+ options : DefinitionsGeneratorOptions ,
549
+ ) : string {
550
+ if ( ! options . typeName ) {
551
+ return name ;
552
+ }
553
+ return options . typeName ( name ) ;
554
+ }
512
555
}
0 commit comments