@@ -176,61 +176,11 @@ export function buildASTSchema(
176
176
}
177
177
}
178
178
179
- let queryTypeName ;
180
- let mutationTypeName ;
181
- let subscriptionTypeName ;
182
- if ( schemaDef ) {
183
- schemaDef . operationTypes . forEach ( operationType => {
184
- const typeName = operationType . type . name . value ;
185
- if ( operationType . operation === 'query' ) {
186
- if ( queryTypeName ) {
187
- throw new Error ( 'Must provide only one query type in schema.' ) ;
188
- }
189
- if ( ! nodeMap [ typeName ] ) {
190
- throw new Error (
191
- `Specified query type "${ typeName } " not found in document.`
192
- ) ;
193
- }
194
- queryTypeName = typeName ;
195
- } else if ( operationType . operation === 'mutation' ) {
196
- if ( mutationTypeName ) {
197
- throw new Error ( 'Must provide only one mutation type in schema.' ) ;
198
- }
199
- if ( ! nodeMap [ typeName ] ) {
200
- throw new Error (
201
- `Specified mutation type "${ typeName } " not found in document.`
202
- ) ;
203
- }
204
- mutationTypeName = typeName ;
205
- } else if ( operationType . operation === 'subscription' ) {
206
- if ( subscriptionTypeName ) {
207
- throw new Error ( 'Must provide only one subscription type in schema.' ) ;
208
- }
209
- if ( ! nodeMap [ typeName ] ) {
210
- throw new Error (
211
- `Specified subscription type "${ typeName } " not found in document.`
212
- ) ;
213
- }
214
- subscriptionTypeName = typeName ;
215
- }
216
- } ) ;
217
- } else {
218
- if ( nodeMap . Query ) {
219
- queryTypeName = 'Query ';
220
- }
221
- if ( nodeMap . Mutation ) {
222
- mutationTypeName = 'Mutation ';
223
- }
224
- if ( nodeMap . Subscription ) {
225
- subscriptionTypeName = 'Subscription ';
226
- }
227
- }
228
-
229
- if ( ! queryTypeName ) {
230
- throw new Error (
231
- 'Must provide schema definition with query type or a type named Query.'
232
- ) ;
233
- }
179
+ const operationTypes = schemaDef ? getOperationTypes ( schemaDef ) : {
180
+ query : nodeMap . Query ? 'Query' : null ,
181
+ mutation : nodeMap . Mutation ? 'Mutation' : null ,
182
+ subscription : nodeMap . Subscription ? 'Subscription' : null ,
183
+ } ;
234
184
235
185
const innerTypeMap = {
236
186
String : GraphQLString ,
@@ -265,19 +215,44 @@ export function buildASTSchema(
265
215
directives . push ( GraphQLDeprecatedDirective ) ;
266
216
}
267
217
218
+ if ( ! operationTypes . query ) {
219
+ throw new Error (
220
+ 'Must provide schema definition with query type or a type named Query.'
221
+ ) ;
222
+ }
223
+
268
224
return new GraphQLSchema ( {
269
- query : getObjectType ( nodeMap [ queryTypeName ] ) ,
270
- mutation : mutationTypeName ?
271
- getObjectType ( nodeMap [ mutationTypeName ] ) :
225
+ query : getObjectType ( operationTypes . query ) ,
226
+ mutation : operationTypes . mutation ?
227
+ getObjectType ( operationTypes . mutation ) :
272
228
null ,
273
- subscription : subscriptionTypeName ?
274
- getObjectType ( nodeMap [ subscriptionTypeName ] ) :
229
+ subscription : operationTypes . subscription ?
230
+ getObjectType ( operationTypes . subscription ) :
275
231
null ,
276
232
types,
277
233
directives,
278
234
astNode : schemaDef ,
279
235
} ) ;
280
236
237
+ function getOperationTypes ( schema : SchemaDefinitionNode ) {
238
+ const opTypes = { } ;
239
+ schema . operationTypes . forEach ( operationType => {
240
+ const typeName = operationType . type . name . value ;
241
+ const operation = operationType . operation ;
242
+
243
+ if ( opTypes [ operation ] ) {
244
+ throw new Error ( `Must provide only one ${ operation } type in schema.` ) ;
245
+ }
246
+ if ( ! nodeMap [ typeName ] ) {
247
+ throw new Error (
248
+ `Specified ${ operation } type "${ typeName } " not found in document.`
249
+ ) ;
250
+ }
251
+ opTypes [ operation ] = typeName ;
252
+ } ) ;
253
+ return opTypes ;
254
+ }
255
+
281
256
function getDirective (
282
257
directiveNode : DirectiveDefinitionNode
283
258
) : GraphQLDirective {
@@ -292,8 +267,8 @@ export function buildASTSchema(
292
267
} ) ;
293
268
}
294
269
295
- function getObjectType ( typeNode : TypeDefinitionNode ) : GraphQLObjectType {
296
- const type = typeDefNamed ( typeNode . name . value ) ;
270
+ function getObjectType ( name : string ) : GraphQLObjectType {
271
+ const type = typeDefNamed ( name ) ;
297
272
invariant (
298
273
type instanceof GraphQLObjectType ,
299
274
'AST must provide object type.'
0 commit comments