@@ -15,6 +15,8 @@ import { GraphQLObjectType } from 'graphql';
15
15
16
16
export interface AstOptions {
17
17
schemaComposer ?: SchemaComposer < any > ;
18
+ prefix ?: string ;
19
+ suffix ?: string ;
18
20
}
19
21
20
22
export function astToSchema < TContext = any > (
@@ -36,29 +38,31 @@ export function astToSchema<TContext = any>(
36
38
sc = new SchemaComposer ( ) ;
37
39
}
38
40
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 ) ;
42
44
43
45
return sc ;
44
46
}
45
47
46
48
function populateRoot (
47
49
sc : SchemaComposer < any > ,
48
50
rootName : 'Query' | 'Mutation' | 'Subscription' ,
49
- astRootNode : AstRootTypeNode
51
+ astRootNode : AstRootTypeNode ,
52
+ opts ?: AstOptions
50
53
) {
51
54
const tc = sc [ rootName ] ;
52
55
Object . keys ( astRootNode . children ) . forEach ( ( key ) => {
53
- createFields ( sc , astRootNode . children [ key ] , rootName , tc ) ;
56
+ createFields ( sc , astRootNode . children [ key ] , tc , rootName , opts || { } ) ;
54
57
} ) ;
55
58
}
56
59
57
60
export function createFields (
58
61
sc : SchemaComposer < any > ,
59
62
ast : AstDirNode | AstFileNode | void ,
60
- prefix : string ,
61
- parent : ObjectTypeComposer
63
+ parent : ObjectTypeComposer ,
64
+ pathPrefix : string ,
65
+ opts : AstOptions = { }
62
66
) : void {
63
67
if ( ! ast ) return ;
64
68
@@ -76,7 +80,7 @@ export function createFields(
76
80
if ( name . endsWith ( '.index' ) ) {
77
81
const fieldName = name . slice ( 0 , - 6 ) ; // remove ".index" from field name
78
82
parent . addNestedFields ( {
79
- [ fieldName ] : prepareNamespaceFieldConfig ( sc , ast , ` ${ prefix } ${ getTypename ( ast ) } ` ) ,
83
+ [ fieldName ] : prepareNamespaceFieldConfig ( sc , ast , getTypename ( ast , pathPrefix , opts ) ) ,
80
84
} ) ;
81
85
} else {
82
86
parent . addNestedFields ( {
@@ -88,7 +92,7 @@ export function createFields(
88
92
}
89
93
90
94
if ( ast . kind === 'dir' ) {
91
- const typename = ` ${ prefix } ${ getTypename ( ast ) } ` ;
95
+ const typename = getTypename ( ast , pathPrefix , opts ) ;
92
96
let fc : ObjectTypeComposerFieldConfig < any , any > ;
93
97
if ( ast . children [ 'index' ] && ast . children [ 'index' ] . kind === 'file' ) {
94
98
fc = prepareNamespaceFieldConfig ( sc , ast . children [ 'index' ] , typename ) ;
@@ -103,15 +107,17 @@ export function createFields(
103
107
} ,
104
108
} ) ;
105
109
110
+ const pathPrefixForChild = getTypename ( ast , pathPrefix , { } ) ;
106
111
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 ) ;
108
113
} ) ;
109
114
}
110
115
}
111
116
112
- function getTypename ( ast : AstDirNode | AstFileNode ) : string {
117
+ function getTypename ( ast : AstDirNode | AstFileNode , pathPrefix : string , opts : AstOptions ) : string {
113
118
const name = ast . name ;
114
119
120
+ let typename = pathPrefix ;
115
121
if ( name . indexOf ( '.' ) !== - 1 ) {
116
122
const namesArray = name . split ( '.' ) ;
117
123
@@ -121,12 +127,16 @@ function getTypename(ast: AstDirNode | AstFileNode): string {
121
127
) ;
122
128
}
123
129
124
- return namesArray . reduce ( ( prev , current ) => {
130
+ typename += namesArray . reduce ( ( prev , current ) => {
125
131
return prev + upperFirst ( current ) ;
126
132
} , '' ) ;
127
133
} else {
128
- return upperFirst ( name ) ;
134
+ typename += upperFirst ( name ) ;
129
135
}
136
+
137
+ if ( opts . prefix ) typename = `${ opts . prefix } ${ typename } ` ;
138
+ if ( opts . suffix ) typename += opts . suffix ;
139
+ return typename ;
130
140
}
131
141
132
142
function prepareNamespaceFieldConfig (
0 commit comments