@@ -15,6 +15,8 @@ import { GraphQLObjectType } from 'graphql';
1515
1616export interface AstOptions {
1717 schemaComposer ?: SchemaComposer < any > ;
18+ prefix ?: string ;
19+ suffix ?: string ;
1820}
1921
2022export function astToSchema < TContext = any > (
@@ -36,29 +38,31 @@ export function astToSchema<TContext = any>(
3638 sc = new SchemaComposer ( ) ;
3739 }
3840
39- if ( ast . query ) populateRoot ( sc , 'Query' , ast . query ) ;
40- if ( ast . mutation ) populateRoot ( sc , 'Mutation' , ast . mutation ) ;
41- if ( ast . subscription ) populateRoot ( sc , 'Subscription' , ast . subscription ) ;
41+ if ( ast . query ) populateRoot ( sc , 'Query' , ast . query , opts ) ;
42+ if ( ast . mutation ) populateRoot ( sc , 'Mutation' , ast . mutation , opts ) ;
43+ if ( ast . subscription ) populateRoot ( sc , 'Subscription' , ast . subscription , opts ) ;
4244
4345 return sc ;
4446}
4547
4648function populateRoot (
4749 sc : SchemaComposer < any > ,
4850 rootName : 'Query' | 'Mutation' | 'Subscription' ,
49- astRootNode : AstRootTypeNode
51+ astRootNode : AstRootTypeNode ,
52+ opts ?: AstOptions
5053) {
5154 const tc = sc [ rootName ] ;
5255 Object . keys ( astRootNode . children ) . forEach ( ( key ) => {
53- createFields ( sc , astRootNode . children [ key ] , rootName , tc ) ;
56+ createFields ( sc , astRootNode . children [ key ] , tc , rootName , opts || { } ) ;
5457 } ) ;
5558}
5659
5760export function createFields (
5861 sc : SchemaComposer < any > ,
5962 ast : AstDirNode | AstFileNode | void ,
60- prefix : string ,
61- parent : ObjectTypeComposer
63+ parent : ObjectTypeComposer ,
64+ pathPrefix : string ,
65+ opts : AstOptions = { }
6266) : void {
6367 if ( ! ast ) return ;
6468
@@ -76,7 +80,7 @@ export function createFields(
7680 if ( name . endsWith ( '.index' ) ) {
7781 const fieldName = name . slice ( 0 , - 6 ) ; // remove ".index" from field name
7882 parent . addNestedFields ( {
79- [ fieldName ] : prepareNamespaceFieldConfig ( sc , ast , ` ${ prefix } ${ getTypename ( ast ) } ` ) ,
83+ [ fieldName ] : prepareNamespaceFieldConfig ( sc , ast , getTypename ( ast , pathPrefix , opts ) ) ,
8084 } ) ;
8185 } else {
8286 parent . addNestedFields ( {
@@ -88,7 +92,7 @@ export function createFields(
8892 }
8993
9094 if ( ast . kind === 'dir' ) {
91- const typename = ` ${ prefix } ${ getTypename ( ast ) } ` ;
95+ const typename = getTypename ( ast , pathPrefix , opts ) ;
9296 let fc : ObjectTypeComposerFieldConfig < any , any > ;
9397 if ( ast . children [ 'index' ] && ast . children [ 'index' ] . kind === 'file' ) {
9498 fc = prepareNamespaceFieldConfig ( sc , ast . children [ 'index' ] , typename ) ;
@@ -103,15 +107,17 @@ export function createFields(
103107 } ,
104108 } ) ;
105109
110+ const pathPrefixForChild = getTypename ( ast , pathPrefix , { } ) ;
106111 Object . keys ( ast . children ) . forEach ( ( key ) => {
107- createFields ( sc , ast . children [ key ] , typename , fc . type as any ) ;
112+ createFields ( sc , ast . children [ key ] , fc . type as any , pathPrefixForChild , opts ) ;
108113 } ) ;
109114 }
110115}
111116
112- function getTypename ( ast : AstDirNode | AstFileNode ) : string {
117+ function getTypename ( ast : AstDirNode | AstFileNode , pathPrefix : string , opts : AstOptions ) : string {
113118 const name = ast . name ;
114119
120+ let typename = pathPrefix ;
115121 if ( name . indexOf ( '.' ) !== - 1 ) {
116122 const namesArray = name . split ( '.' ) ;
117123
@@ -121,12 +127,16 @@ function getTypename(ast: AstDirNode | AstFileNode): string {
121127 ) ;
122128 }
123129
124- return namesArray . reduce ( ( prev , current ) => {
130+ typename += namesArray . reduce ( ( prev , current ) => {
125131 return prev + upperFirst ( current ) ;
126132 } , '' ) ;
127133 } else {
128- return upperFirst ( name ) ;
134+ typename += upperFirst ( name ) ;
129135 }
136+
137+ if ( opts . prefix ) typename = `${ opts . prefix } ${ typename } ` ;
138+ if ( opts . suffix ) typename += opts . suffix ;
139+ return typename ;
130140}
131141
132142function prepareNamespaceFieldConfig (
0 commit comments