1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.isSchema = isSchema;
|
7 | exports.assertSchema = assertSchema;
|
8 | exports.GraphQLSchema = void 0;
|
9 |
|
10 | var _find = _interopRequireDefault(require("../polyfills/find.js"));
|
11 |
|
12 | var _arrayFrom3 = _interopRequireDefault(require("../polyfills/arrayFrom.js"));
|
13 |
|
14 | var _objectValues5 = _interopRequireDefault(require("../polyfills/objectValues.js"));
|
15 |
|
16 | var _symbols = require("../polyfills/symbols.js");
|
17 |
|
18 | var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
|
19 |
|
20 | var _toObjMap = _interopRequireDefault(require("../jsutils/toObjMap.js"));
|
21 |
|
22 | var _devAssert = _interopRequireDefault(require("../jsutils/devAssert.js"));
|
23 |
|
24 | var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf.js"));
|
25 |
|
26 | var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
|
27 |
|
28 | var _introspection = require("./introspection.js");
|
29 |
|
30 | var _directives = require("./directives.js");
|
31 |
|
32 | var _definition = require("./definition.js");
|
33 |
|
34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
35 |
|
36 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
37 |
|
38 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
39 |
|
40 |
|
41 | function isSchema(schema) {
|
42 | return (0, _instanceOf.default)(schema, GraphQLSchema);
|
43 | }
|
44 |
|
45 | function assertSchema(schema) {
|
46 | if (!isSchema(schema)) {
|
47 | throw new Error("Expected ".concat((0, _inspect.default)(schema), " to be a GraphQL schema."));
|
48 | }
|
49 |
|
50 | return schema;
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | var GraphQLSchema = function () {
|
118 |
|
119 | function GraphQLSchema(config) {
|
120 | var _config$directives;
|
121 |
|
122 |
|
123 |
|
124 | this.__validationErrors = config.assumeValid === true ? [] : undefined;
|
125 |
|
126 | (0, _isObjectLike.default)(config) || (0, _devAssert.default)(0, 'Must provide configuration object.');
|
127 | !config.types || Array.isArray(config.types) || (0, _devAssert.default)(0, "\"types\" must be Array if provided but got: ".concat((0, _inspect.default)(config.types), "."));
|
128 | !config.directives || Array.isArray(config.directives) || (0, _devAssert.default)(0, '"directives" must be Array if provided but got: ' + "".concat((0, _inspect.default)(config.directives), "."));
|
129 | this.description = config.description;
|
130 | this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
|
131 | this.astNode = config.astNode;
|
132 | this.extensionASTNodes = config.extensionASTNodes;
|
133 | this._queryType = config.query;
|
134 | this._mutationType = config.mutation;
|
135 | this._subscriptionType = config.subscription;
|
136 |
|
137 | this._directives = (_config$directives = config.directives) !== null && _config$directives !== void 0 ? _config$directives : _directives.specifiedDirectives;
|
138 |
|
139 |
|
140 | var allReferencedTypes = new Set(config.types);
|
141 |
|
142 | if (config.types != null) {
|
143 | for (var _i2 = 0, _config$types2 = config.types; _i2 < _config$types2.length; _i2++) {
|
144 | var type = _config$types2[_i2];
|
145 |
|
146 |
|
147 | allReferencedTypes.delete(type);
|
148 | collectReferencedTypes(type, allReferencedTypes);
|
149 | }
|
150 | }
|
151 |
|
152 | if (this._queryType != null) {
|
153 | collectReferencedTypes(this._queryType, allReferencedTypes);
|
154 | }
|
155 |
|
156 | if (this._mutationType != null) {
|
157 | collectReferencedTypes(this._mutationType, allReferencedTypes);
|
158 | }
|
159 |
|
160 | if (this._subscriptionType != null) {
|
161 | collectReferencedTypes(this._subscriptionType, allReferencedTypes);
|
162 | }
|
163 |
|
164 | for (var _i4 = 0, _this$_directives2 = this._directives; _i4 < _this$_directives2.length; _i4++) {
|
165 | var directive = _this$_directives2[_i4];
|
166 |
|
167 |
|
168 | if ((0, _directives.isDirective)(directive)) {
|
169 | for (var _i6 = 0, _directive$args2 = directive.args; _i6 < _directive$args2.length; _i6++) {
|
170 | var arg = _directive$args2[_i6];
|
171 | collectReferencedTypes(arg.type, allReferencedTypes);
|
172 | }
|
173 | }
|
174 | }
|
175 |
|
176 | collectReferencedTypes(_introspection.__Schema, allReferencedTypes);
|
177 |
|
178 | this._typeMap = Object.create(null);
|
179 | this._subTypeMap = Object.create(null);
|
180 |
|
181 | this._implementationsMap = Object.create(null);
|
182 |
|
183 | for (var _i8 = 0, _arrayFrom2 = (0, _arrayFrom3.default)(allReferencedTypes); _i8 < _arrayFrom2.length; _i8++) {
|
184 | var namedType = _arrayFrom2[_i8];
|
185 |
|
186 | if (namedType == null) {
|
187 | continue;
|
188 | }
|
189 |
|
190 | var typeName = namedType.name;
|
191 | typeName || (0, _devAssert.default)(0, 'One of the provided types for building the Schema is missing a name.');
|
192 |
|
193 | if (this._typeMap[typeName] !== undefined) {
|
194 | throw new Error("Schema must contain uniquely named types but contains multiple types named \"".concat(typeName, "\"."));
|
195 | }
|
196 |
|
197 | this._typeMap[typeName] = namedType;
|
198 |
|
199 | if ((0, _definition.isInterfaceType)(namedType)) {
|
200 |
|
201 | for (var _i10 = 0, _namedType$getInterfa2 = namedType.getInterfaces(); _i10 < _namedType$getInterfa2.length; _i10++) {
|
202 | var iface = _namedType$getInterfa2[_i10];
|
203 |
|
204 | if ((0, _definition.isInterfaceType)(iface)) {
|
205 | var implementations = this._implementationsMap[iface.name];
|
206 |
|
207 | if (implementations === undefined) {
|
208 | implementations = this._implementationsMap[iface.name] = {
|
209 | objects: [],
|
210 | interfaces: []
|
211 | };
|
212 | }
|
213 |
|
214 | implementations.interfaces.push(namedType);
|
215 | }
|
216 | }
|
217 | } else if ((0, _definition.isObjectType)(namedType)) {
|
218 |
|
219 | for (var _i12 = 0, _namedType$getInterfa4 = namedType.getInterfaces(); _i12 < _namedType$getInterfa4.length; _i12++) {
|
220 | var _iface = _namedType$getInterfa4[_i12];
|
221 |
|
222 | if ((0, _definition.isInterfaceType)(_iface)) {
|
223 | var _implementations = this._implementationsMap[_iface.name];
|
224 |
|
225 | if (_implementations === undefined) {
|
226 | _implementations = this._implementationsMap[_iface.name] = {
|
227 | objects: [],
|
228 | interfaces: []
|
229 | };
|
230 | }
|
231 |
|
232 | _implementations.objects.push(namedType);
|
233 | }
|
234 | }
|
235 | }
|
236 | }
|
237 | }
|
238 |
|
239 | var _proto = GraphQLSchema.prototype;
|
240 |
|
241 | _proto.getQueryType = function getQueryType() {
|
242 | return this._queryType;
|
243 | };
|
244 |
|
245 | _proto.getMutationType = function getMutationType() {
|
246 | return this._mutationType;
|
247 | };
|
248 |
|
249 | _proto.getSubscriptionType = function getSubscriptionType() {
|
250 | return this._subscriptionType;
|
251 | };
|
252 |
|
253 | _proto.getTypeMap = function getTypeMap() {
|
254 | return this._typeMap;
|
255 | };
|
256 |
|
257 | _proto.getType = function getType(name) {
|
258 | return this.getTypeMap()[name];
|
259 | };
|
260 |
|
261 | _proto.getPossibleTypes = function getPossibleTypes(abstractType) {
|
262 | return (0, _definition.isUnionType)(abstractType) ? abstractType.getTypes() : this.getImplementations(abstractType).objects;
|
263 | };
|
264 |
|
265 | _proto.getImplementations = function getImplementations(interfaceType) {
|
266 | var implementations = this._implementationsMap[interfaceType.name];
|
267 | return implementations !== null && implementations !== void 0 ? implementations : {
|
268 | objects: [],
|
269 | interfaces: []
|
270 | };
|
271 | }
|
272 | ;
|
273 |
|
274 | _proto.isPossibleType = function isPossibleType(abstractType, possibleType) {
|
275 | return this.isSubType(abstractType, possibleType);
|
276 | };
|
277 |
|
278 | _proto.isSubType = function isSubType(abstractType, maybeSubType) {
|
279 | var map = this._subTypeMap[abstractType.name];
|
280 |
|
281 | if (map === undefined) {
|
282 | map = Object.create(null);
|
283 |
|
284 | if ((0, _definition.isUnionType)(abstractType)) {
|
285 | for (var _i14 = 0, _abstractType$getType2 = abstractType.getTypes(); _i14 < _abstractType$getType2.length; _i14++) {
|
286 | var type = _abstractType$getType2[_i14];
|
287 | map[type.name] = true;
|
288 | }
|
289 | } else {
|
290 | var implementations = this.getImplementations(abstractType);
|
291 |
|
292 | for (var _i16 = 0, _implementations$obje2 = implementations.objects; _i16 < _implementations$obje2.length; _i16++) {
|
293 | var _type = _implementations$obje2[_i16];
|
294 | map[_type.name] = true;
|
295 | }
|
296 |
|
297 | for (var _i18 = 0, _implementations$inte2 = implementations.interfaces; _i18 < _implementations$inte2.length; _i18++) {
|
298 | var _type2 = _implementations$inte2[_i18];
|
299 | map[_type2.name] = true;
|
300 | }
|
301 | }
|
302 |
|
303 | this._subTypeMap[abstractType.name] = map;
|
304 | }
|
305 |
|
306 | return map[maybeSubType.name] !== undefined;
|
307 | };
|
308 |
|
309 | _proto.getDirectives = function getDirectives() {
|
310 | return this._directives;
|
311 | };
|
312 |
|
313 | _proto.getDirective = function getDirective(name) {
|
314 | return (0, _find.default)(this.getDirectives(), function (directive) {
|
315 | return directive.name === name;
|
316 | });
|
317 | };
|
318 |
|
319 | _proto.toConfig = function toConfig() {
|
320 | var _this$extensionASTNod;
|
321 |
|
322 | return {
|
323 | description: this.description,
|
324 | query: this.getQueryType(),
|
325 | mutation: this.getMutationType(),
|
326 | subscription: this.getSubscriptionType(),
|
327 | types: (0, _objectValues5.default)(this.getTypeMap()),
|
328 | directives: this.getDirectives().slice(),
|
329 | extensions: this.extensions,
|
330 | astNode: this.astNode,
|
331 | extensionASTNodes: (_this$extensionASTNod = this.extensionASTNodes) !== null && _this$extensionASTNod !== void 0 ? _this$extensionASTNod : [],
|
332 | assumeValid: this.__validationErrors !== undefined
|
333 | };
|
334 | }
|
335 | ;
|
336 |
|
337 | _createClass(GraphQLSchema, [{
|
338 | key: _symbols.SYMBOL_TO_STRING_TAG,
|
339 | get: function get() {
|
340 | return 'GraphQLSchema';
|
341 | }
|
342 | }]);
|
343 |
|
344 | return GraphQLSchema;
|
345 | }();
|
346 |
|
347 | exports.GraphQLSchema = GraphQLSchema;
|
348 |
|
349 | function collectReferencedTypes(type, typeSet) {
|
350 | var namedType = (0, _definition.getNamedType)(type);
|
351 |
|
352 | if (!typeSet.has(namedType)) {
|
353 | typeSet.add(namedType);
|
354 |
|
355 | if ((0, _definition.isUnionType)(namedType)) {
|
356 | for (var _i20 = 0, _namedType$getTypes2 = namedType.getTypes(); _i20 < _namedType$getTypes2.length; _i20++) {
|
357 | var memberType = _namedType$getTypes2[_i20];
|
358 | collectReferencedTypes(memberType, typeSet);
|
359 | }
|
360 | } else if ((0, _definition.isObjectType)(namedType) || (0, _definition.isInterfaceType)(namedType)) {
|
361 | for (var _i22 = 0, _namedType$getInterfa6 = namedType.getInterfaces(); _i22 < _namedType$getInterfa6.length; _i22++) {
|
362 | var interfaceType = _namedType$getInterfa6[_i22];
|
363 | collectReferencedTypes(interfaceType, typeSet);
|
364 | }
|
365 |
|
366 | for (var _i24 = 0, _objectValues2 = (0, _objectValues5.default)(namedType.getFields()); _i24 < _objectValues2.length; _i24++) {
|
367 | var field = _objectValues2[_i24];
|
368 | collectReferencedTypes(field.type, typeSet);
|
369 |
|
370 | for (var _i26 = 0, _field$args2 = field.args; _i26 < _field$args2.length; _i26++) {
|
371 | var arg = _field$args2[_i26];
|
372 | collectReferencedTypes(arg.type, typeSet);
|
373 | }
|
374 | }
|
375 | } else if ((0, _definition.isInputObjectType)(namedType)) {
|
376 | for (var _i28 = 0, _objectValues4 = (0, _objectValues5.default)(namedType.getFields()); _i28 < _objectValues4.length; _i28++) {
|
377 | var _field = _objectValues4[_i28];
|
378 | collectReferencedTypes(_field.type, typeSet);
|
379 | }
|
380 | }
|
381 | }
|
382 |
|
383 | return typeSet;
|
384 | }
|