@@ -77,7 +77,7 @@ function buildASTSchema(documentAST, options) {
77
77
}
78
78
79
79
var schemaDef ;
80
- var nodeMap = Object . create ( null ) ;
80
+ var typeDefs = [ ] ;
81
81
var directiveDefs = [ ] ;
82
82
var _iteratorNormalCompletion = true ;
83
83
var _didIteratorError = false ;
@@ -90,7 +90,7 @@ function buildASTSchema(documentAST, options) {
90
90
if ( def . kind === _kinds . Kind . SCHEMA_DEFINITION ) {
91
91
schemaDef = def ;
92
92
} else if ( ( 0 , _predicates . isTypeDefinitionNode ) ( def ) ) {
93
- nodeMap [ def . name . value ] = def ;
93
+ typeDefs . push ( def ) ;
94
94
} else if ( def . kind === _kinds . Kind . DIRECTIVE_DEFINITION ) {
95
95
directiveDefs . push ( def ) ;
96
96
}
@@ -110,16 +110,21 @@ function buildASTSchema(documentAST, options) {
110
110
}
111
111
}
112
112
113
+ var astBuilder = new ASTDefinitionBuilder ( options , function ( typeName ) {
114
+ var type = typeMap [ typeName ] ;
115
+ ! type ? ( 0 , _invariant . default ) ( 0 , "Type \"" . concat ( typeName , "\" not found in document." ) ) : void 0 ;
116
+ return type ;
117
+ } ) ;
118
+ var typeMap = keyByNameNode ( typeDefs , function ( node ) {
119
+ return astBuilder . buildType ( node ) ;
120
+ } ) ;
113
121
var operationTypes = schemaDef ? getOperationTypes ( schemaDef ) : {
114
- query : nodeMap . Query ,
115
- mutation : nodeMap . Mutation ,
116
- subscription : nodeMap . Subscription
122
+ query : ' Query' ,
123
+ mutation : ' Mutation' ,
124
+ subscription : ' Subscription'
117
125
} ;
118
- var definitionBuilder = new ASTDefinitionBuilder ( nodeMap , options , function ( typeName ) {
119
- throw new Error ( "Type \"" . concat ( typeName , "\" not found in document." ) ) ;
120
- } ) ;
121
126
var directives = directiveDefs . map ( function ( def ) {
122
- return definitionBuilder . buildDirective ( def ) ;
127
+ return astBuilder . buildDirective ( def ) ;
123
128
} ) ; // If specified directives were not explicitly declared, add them.
124
129
125
130
if ( ! directives . some ( function ( directive ) {
@@ -138,18 +143,16 @@ function buildASTSchema(documentAST, options) {
138
143
return directive . name === 'deprecated' ;
139
144
} ) ) {
140
145
directives . push ( _directives . GraphQLDeprecatedDirective ) ;
141
- } // Note: While this could make early assertions to get the correctly
142
- // typed values below, that would throw immediately while type system
143
- // validation with validateSchema() will produce more actionable results.
144
-
146
+ }
145
147
146
148
return new _schema . GraphQLSchema ( {
147
- query : operationTypes . query ? definitionBuilder . buildType ( operationTypes . query ) : null ,
148
- mutation : operationTypes . mutation ? definitionBuilder . buildType ( operationTypes . mutation ) : null ,
149
- subscription : operationTypes . subscription ? definitionBuilder . buildType ( operationTypes . subscription ) : null ,
150
- types : ( 0 , _objectValues . default ) ( nodeMap ) . map ( function ( node ) {
151
- return definitionBuilder . buildType ( node ) ;
152
- } ) ,
149
+ // Note: While this could make early assertions to get the correctly
150
+ // typed values below, that would throw immediately while type system
151
+ // validation with validateSchema() will produce more actionable results.
152
+ query : operationTypes . query ? typeMap [ operationTypes . query ] : null ,
153
+ mutation : operationTypes . mutation ? typeMap [ operationTypes . mutation ] : null ,
154
+ subscription : operationTypes . subscription ? typeMap [ operationTypes . subscription ] : null ,
155
+ types : ( 0 , _objectValues . default ) ( typeMap ) ,
153
156
directives : directives ,
154
157
astNode : schemaDef ,
155
158
assumeValid : options && options . assumeValid ,
@@ -165,7 +168,7 @@ function buildASTSchema(documentAST, options) {
165
168
try {
166
169
for ( var _iterator2 = schema . operationTypes [ Symbol . iterator ] ( ) , _step2 ; ! ( _iteratorNormalCompletion2 = ( _step2 = _iterator2 . next ( ) ) . done ) ; _iteratorNormalCompletion2 = true ) {
167
170
var operationType = _step2 . value ;
168
- opTypes [ operationType . operation ] = operationType . type ;
171
+ opTypes [ operationType . operation ] = operationType . type . name . value ;
169
172
}
170
173
} catch ( err ) {
171
174
_didIteratorError2 = true ;
@@ -186,47 +189,36 @@ function buildASTSchema(documentAST, options) {
186
189
}
187
190
}
188
191
192
+ var stdTypeMap = ( 0 , _keyMap . default ) ( _scalars . specifiedScalarTypes . concat ( _introspection . introspectionTypes ) , function ( type ) {
193
+ return type . name ;
194
+ } ) ;
195
+
189
196
var ASTDefinitionBuilder =
190
197
/*#__PURE__*/
191
198
function ( ) {
192
- function ASTDefinitionBuilder ( typeDefinitionsMap , options , resolveType ) {
193
- this . _typeDefinitionsMap = typeDefinitionsMap ;
199
+ function ASTDefinitionBuilder ( options , resolveType ) {
194
200
this . _options = options ;
195
- this . _resolveType = resolveType ; // Initialize to the GraphQL built in scalars and introspection types.
196
-
197
- this . _cache = ( 0 , _keyMap . default ) ( _scalars . specifiedScalarTypes . concat ( _introspection . introspectionTypes ) , function ( type ) {
198
- return type . name ;
199
- } ) ;
201
+ this . _resolveType = resolveType ;
200
202
}
201
203
202
204
var _proto = ASTDefinitionBuilder . prototype ;
203
205
204
- _proto . buildType = function buildType ( node ) {
205
- var typeName = node . name . value ;
206
-
207
- if ( ! this . _cache [ typeName ] ) {
208
- if ( node . kind === _kinds . Kind . NAMED_TYPE ) {
209
- var defNode = this . _typeDefinitionsMap [ typeName ] ;
210
- this . _cache [ typeName ] = defNode ? this . _makeSchemaDef ( defNode ) : this . _resolveType ( node . name . value ) ;
211
- } else {
212
- this . _cache [ typeName ] = this . _makeSchemaDef ( node ) ;
213
- }
214
- }
215
-
216
- return this . _cache [ typeName ] ;
206
+ _proto . getNamedType = function getNamedType ( node ) {
207
+ var name = node . name . value ;
208
+ return stdTypeMap [ name ] || this . _resolveType ( name ) ;
217
209
} ;
218
210
219
- _proto . _buildWrappedType = function _buildWrappedType ( typeNode ) {
220
- if ( typeNode . kind === _kinds . Kind . LIST_TYPE ) {
221
- return ( 0 , _definition . GraphQLList ) ( this . _buildWrappedType ( typeNode . type ) ) ;
211
+ _proto . getWrappedType = function getWrappedType ( node ) {
212
+ if ( node . kind === _kinds . Kind . LIST_TYPE ) {
213
+ return ( 0 , _definition . GraphQLList ) ( this . getWrappedType ( node . type ) ) ;
222
214
}
223
215
224
- if ( typeNode . kind === _kinds . Kind . NON_NULL_TYPE ) {
216
+ if ( node . kind === _kinds . Kind . NON_NULL_TYPE ) {
225
217
return ( 0 , _definition . GraphQLNonNull ) ( // Note: GraphQLNonNull constructor validates this type
226
- this . _buildWrappedType ( typeNode . type ) ) ;
218
+ this . getWrappedType ( node . type ) ) ;
227
219
}
228
220
229
- return this . buildType ( typeNode ) ;
221
+ return this . getNamedType ( node ) ;
230
222
} ;
231
223
232
224
_proto . buildDirective = function buildDirective ( directive ) {
@@ -254,7 +246,7 @@ function () {
254
246
// Note: While this could make assertions to get the correctly typed
255
247
// value, that would throw immediately while type system validation
256
248
// with validateSchema() will produce more actionable results.
257
- type : this . _buildWrappedType ( field . type ) ,
249
+ type : this . getWrappedType ( field . type ) ,
258
250
description : getDescription ( field , this . _options ) ,
259
251
args : keyByNameNode ( field . arguments || [ ] , function ( arg ) {
260
252
return _this2 . buildArg ( arg ) ;
@@ -267,8 +259,7 @@ function () {
267
259
_proto . buildArg = function buildArg ( value ) {
268
260
// Note: While this could make assertions to get the correctly typed
269
261
// value, that would throw immediately while type system validation
270
- var type = this . _buildWrappedType ( value . type ) ;
271
-
262
+ var type = this . getWrappedType ( value . type ) ;
272
263
return {
273
264
type : type ,
274
265
description : getDescription ( value , this . _options ) ,
@@ -280,8 +271,7 @@ function () {
280
271
_proto . buildInputField = function buildInputField ( value ) {
281
272
// Note: While this could make assertions to get the correctly typed
282
273
// value, that would throw immediately while type system validation
283
- var type = this . _buildWrappedType ( value . type ) ;
284
-
274
+ var type = this . getWrappedType ( value . type ) ;
285
275
return {
286
276
type : type ,
287
277
description : getDescription ( value , this . _options ) ,
@@ -298,7 +288,13 @@ function () {
298
288
} ;
299
289
} ;
300
290
301
- _proto . _makeSchemaDef = function _makeSchemaDef ( astNode ) {
291
+ _proto . buildType = function buildType ( astNode ) {
292
+ var name = astNode . name . value ;
293
+
294
+ if ( stdTypeMap [ name ] ) {
295
+ return stdTypeMap [ name ] ;
296
+ }
297
+
302
298
switch ( astNode . kind ) {
303
299
case _kinds . Kind . OBJECT_TYPE_DEFINITION :
304
300
return this . _makeTypeDef ( astNode ) ;
@@ -317,10 +313,12 @@ function () {
317
313
318
314
case _kinds . Kind . INPUT_OBJECT_TYPE_DEFINITION :
319
315
return this . _makeInputObjectDef ( astNode ) ;
316
+ } // Not reachable. All possible type definition nodes have been considered.
320
317
321
- default :
322
- throw new Error ( "Type kind \"" . concat ( astNode . kind , "\" not supported." ) ) ;
323
- }
318
+ /* istanbul ignore next */
319
+
320
+
321
+ throw new Error ( "Type kind \"" . concat ( astNode . kind , "\" not supported." ) ) ;
324
322
} ;
325
323
326
324
_proto . _makeTypeDef = function _makeTypeDef ( astNode ) {
@@ -333,7 +331,7 @@ function () {
333
331
334
332
var interfaces = interfaceNodes && interfaceNodes . length > 0 ? function ( ) {
335
333
return interfaceNodes . map ( function ( ref ) {
336
- return _this3 . buildType ( ref ) ;
334
+ return _this3 . getNamedType ( ref ) ;
337
335
} ) ;
338
336
} : [ ] ;
339
337
var fields = fieldNodes && fieldNodes . length > 0 ? function ( ) {
@@ -390,7 +388,7 @@ function () {
390
388
391
389
var types = typeNodes && typeNodes . length > 0 ? function ( ) {
392
390
return typeNodes . map ( function ( ref ) {
393
- return _this6 . buildType ( ref ) ;
391
+ return _this6 . getNamedType ( ref ) ;
394
392
} ) ;
395
393
} : [ ] ;
396
394
return new _definition . GraphQLUnionType ( {
0 commit comments