Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba9bded

Browse files
author
Travis CI
committed
Deploy afc6b7c to NPM branch
1 parent 4f59762 commit ba9bded

File tree

6 files changed

+525
-507
lines changed

6 files changed

+525
-507
lines changed

utilities/buildASTSchema.js

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function buildASTSchema(documentAST, options) {
7777
}
7878

7979
var schemaDef;
80-
var nodeMap = Object.create(null);
80+
var typeDefs = [];
8181
var directiveDefs = [];
8282
var _iteratorNormalCompletion = true;
8383
var _didIteratorError = false;
@@ -90,7 +90,7 @@ function buildASTSchema(documentAST, options) {
9090
if (def.kind === _kinds.Kind.SCHEMA_DEFINITION) {
9191
schemaDef = def;
9292
} else if ((0, _predicates.isTypeDefinitionNode)(def)) {
93-
nodeMap[def.name.value] = def;
93+
typeDefs.push(def);
9494
} else if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
9595
directiveDefs.push(def);
9696
}
@@ -110,16 +110,21 @@ function buildASTSchema(documentAST, options) {
110110
}
111111
}
112112

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+
});
113121
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'
117125
};
118-
var definitionBuilder = new ASTDefinitionBuilder(nodeMap, options, function (typeName) {
119-
throw new Error("Type \"".concat(typeName, "\" not found in document."));
120-
});
121126
var directives = directiveDefs.map(function (def) {
122-
return definitionBuilder.buildDirective(def);
127+
return astBuilder.buildDirective(def);
123128
}); // If specified directives were not explicitly declared, add them.
124129

125130
if (!directives.some(function (directive) {
@@ -138,18 +143,16 @@ function buildASTSchema(documentAST, options) {
138143
return directive.name === 'deprecated';
139144
})) {
140145
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+
}
145147

146148
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),
153156
directives: directives,
154157
astNode: schemaDef,
155158
assumeValid: options && options.assumeValid,
@@ -165,7 +168,7 @@ function buildASTSchema(documentAST, options) {
165168
try {
166169
for (var _iterator2 = schema.operationTypes[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
167170
var operationType = _step2.value;
168-
opTypes[operationType.operation] = operationType.type;
171+
opTypes[operationType.operation] = operationType.type.name.value;
169172
}
170173
} catch (err) {
171174
_didIteratorError2 = true;
@@ -186,47 +189,36 @@ function buildASTSchema(documentAST, options) {
186189
}
187190
}
188191

192+
var stdTypeMap = (0, _keyMap.default)(_scalars.specifiedScalarTypes.concat(_introspection.introspectionTypes), function (type) {
193+
return type.name;
194+
});
195+
189196
var ASTDefinitionBuilder =
190197
/*#__PURE__*/
191198
function () {
192-
function ASTDefinitionBuilder(typeDefinitionsMap, options, resolveType) {
193-
this._typeDefinitionsMap = typeDefinitionsMap;
199+
function ASTDefinitionBuilder(options, resolveType) {
194200
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;
200202
}
201203

202204
var _proto = ASTDefinitionBuilder.prototype;
203205

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);
217209
};
218210

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));
222214
}
223215

224-
if (typeNode.kind === _kinds.Kind.NON_NULL_TYPE) {
216+
if (node.kind === _kinds.Kind.NON_NULL_TYPE) {
225217
return (0, _definition.GraphQLNonNull)( // Note: GraphQLNonNull constructor validates this type
226-
this._buildWrappedType(typeNode.type));
218+
this.getWrappedType(node.type));
227219
}
228220

229-
return this.buildType(typeNode);
221+
return this.getNamedType(node);
230222
};
231223

232224
_proto.buildDirective = function buildDirective(directive) {
@@ -254,7 +246,7 @@ function () {
254246
// Note: While this could make assertions to get the correctly typed
255247
// value, that would throw immediately while type system validation
256248
// with validateSchema() will produce more actionable results.
257-
type: this._buildWrappedType(field.type),
249+
type: this.getWrappedType(field.type),
258250
description: getDescription(field, this._options),
259251
args: keyByNameNode(field.arguments || [], function (arg) {
260252
return _this2.buildArg(arg);
@@ -267,8 +259,7 @@ function () {
267259
_proto.buildArg = function buildArg(value) {
268260
// Note: While this could make assertions to get the correctly typed
269261
// value, that would throw immediately while type system validation
270-
var type = this._buildWrappedType(value.type);
271-
262+
var type = this.getWrappedType(value.type);
272263
return {
273264
type: type,
274265
description: getDescription(value, this._options),
@@ -280,8 +271,7 @@ function () {
280271
_proto.buildInputField = function buildInputField(value) {
281272
// Note: While this could make assertions to get the correctly typed
282273
// value, that would throw immediately while type system validation
283-
var type = this._buildWrappedType(value.type);
284-
274+
var type = this.getWrappedType(value.type);
285275
return {
286276
type: type,
287277
description: getDescription(value, this._options),
@@ -298,7 +288,13 @@ function () {
298288
};
299289
};
300290

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+
302298
switch (astNode.kind) {
303299
case _kinds.Kind.OBJECT_TYPE_DEFINITION:
304300
return this._makeTypeDef(astNode);
@@ -317,10 +313,12 @@ function () {
317313

318314
case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION:
319315
return this._makeInputObjectDef(astNode);
316+
} // Not reachable. All possible type definition nodes have been considered.
320317

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."));
324322
};
325323

326324
_proto._makeTypeDef = function _makeTypeDef(astNode) {
@@ -333,7 +331,7 @@ function () {
333331

334332
var interfaces = interfaceNodes && interfaceNodes.length > 0 ? function () {
335333
return interfaceNodes.map(function (ref) {
336-
return _this3.buildType(ref);
334+
return _this3.getNamedType(ref);
337335
});
338336
} : [];
339337
var fields = fieldNodes && fieldNodes.length > 0 ? function () {
@@ -390,7 +388,7 @@ function () {
390388

391389
var types = typeNodes && typeNodes.length > 0 ? function () {
392390
return typeNodes.map(function (ref) {
393-
return _this6.buildType(ref);
391+
return _this6.getNamedType(ref);
394392
});
395393
} : [];
396394
return new _definition.GraphQLUnionType({

0 commit comments

Comments
 (0)