UNPKG

14.4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.isSchema = isSchema;
7exports.assertSchema = assertSchema;
8exports.GraphQLSchema = void 0;
9
10var _find = _interopRequireDefault(require("../polyfills/find.js"));
11
12var _arrayFrom3 = _interopRequireDefault(require("../polyfills/arrayFrom.js"));
13
14var _objectValues5 = _interopRequireDefault(require("../polyfills/objectValues.js"));
15
16var _symbols = require("../polyfills/symbols.js");
17
18var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
19
20var _toObjMap = _interopRequireDefault(require("../jsutils/toObjMap.js"));
21
22var _devAssert = _interopRequireDefault(require("../jsutils/devAssert.js"));
23
24var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf.js"));
25
26var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
27
28var _introspection = require("./introspection.js");
29
30var _directives = require("./directives.js");
31
32var _definition = require("./definition.js");
33
34function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
36function _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
38function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
39
40// eslint-disable-next-line no-redeclare
41function isSchema(schema) {
42 return (0, _instanceOf.default)(schema, GraphQLSchema);
43}
44
45function 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 * Schema Definition
54 *
55 * A Schema is created by supplying the root types of each type of operation,
56 * query and mutation (optional). A schema definition is then supplied to the
57 * validator and executor.
58 *
59 * Example:
60 *
61 * const MyAppSchema = new GraphQLSchema({
62 * query: MyAppQueryRootType,
63 * mutation: MyAppMutationRootType,
64 * })
65 *
66 * Note: When the schema is constructed, by default only the types that are
67 * reachable by traversing the root types are included, other types must be
68 * explicitly referenced.
69 *
70 * Example:
71 *
72 * const characterInterface = new GraphQLInterfaceType({
73 * name: 'Character',
74 * ...
75 * });
76 *
77 * const humanType = new GraphQLObjectType({
78 * name: 'Human',
79 * interfaces: [characterInterface],
80 * ...
81 * });
82 *
83 * const droidType = new GraphQLObjectType({
84 * name: 'Droid',
85 * interfaces: [characterInterface],
86 * ...
87 * });
88 *
89 * const schema = new GraphQLSchema({
90 * query: new GraphQLObjectType({
91 * name: 'Query',
92 * fields: {
93 * hero: { type: characterInterface, ... },
94 * }
95 * }),
96 * ...
97 * // Since this schema references only the `Character` interface it's
98 * // necessary to explicitly list the types that implement it if
99 * // you want them to be included in the final schema.
100 * types: [humanType, droidType],
101 * })
102 *
103 * Note: If an array of `directives` are provided to GraphQLSchema, that will be
104 * the exact list of directives represented and allowed. If `directives` is not
105 * provided then a default set of the specified directives (e.g. @include and
106 * @skip) will be used. If you wish to provide *additional* directives to these
107 * specified directives, you must explicitly declare them. Example:
108 *
109 * const MyAppSchema = new GraphQLSchema({
110 * ...
111 * directives: specifiedDirectives.concat([ myCustomDirective ]),
112 * })
113 *
114 */
115
116
117var GraphQLSchema = /*#__PURE__*/function () {
118 // Used as a cache for validateSchema().
119 function GraphQLSchema(config) {
120 var _config$directives;
121
122 // If this schema was built from a source known to be valid, then it may be
123 // marked with assumeValid to avoid an additional type system validation.
124 this.__validationErrors = config.assumeValid === true ? [] : undefined; // Check for common mistakes during construction to produce early errors.
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; // Provide specified directives (e.g. @include and @skip) by default.
136
137 this._directives = (_config$directives = config.directives) !== null && _config$directives !== void 0 ? _config$directives : _directives.specifiedDirectives; // To preserve order of user-provided types, we add first to add them to
138 // the set of "collected" types, so `collectReferencedTypes` ignore them.
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 // When we ready to process this type, we remove it from "collected" types
146 // and then add it together with all dependent types in the correct position.
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 // Directives are not validated until validateSchema() is called.
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); // Storing the resulting map for reference by the schema.
177
178 this._typeMap = Object.create(null);
179 this._subTypeMap = Object.create(null); // Keep track of all implementations by interface name.
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 // Store implementations by interface.
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 // Store implementations by objects.
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 } // @deprecated: use isSubType instead - will be removed in v16.
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 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
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
347exports.GraphQLSchema = GraphQLSchema;
348
349function 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}