UNPKG

9.24 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.GraphQLSchema = undefined;
7
8var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
9
10exports.isSchema = isSchema;
11
12var _definition = require('./definition');
13
14var _directives = require('./directives');
15
16var _introspection = require('./introspection');
17
18var _find = require('../jsutils/find');
19
20var _find2 = _interopRequireDefault(_find);
21
22var _instanceOf = require('../jsutils/instanceOf');
23
24var _instanceOf2 = _interopRequireDefault(_instanceOf);
25
26var _invariant = require('../jsutils/invariant');
27
28var _invariant2 = _interopRequireDefault(_invariant);
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /**
33 * Copyright (c) 2015-present, Facebook, Inc.
34 *
35 * This source code is licensed under the MIT license found in the
36 * LICENSE file in the root directory of this source tree.
37 *
38 *
39 */
40
41// eslint-disable-next-line no-redeclare
42
43
44/**
45 * Test if the given value is a GraphQL schema.
46 */
47function isSchema(schema) {
48 return (0, _instanceOf2.default)(schema, GraphQLSchema);
49}
50
51/**
52 * Schema Definition
53 *
54 * A Schema is created by supplying the root types of each type of operation,
55 * query and mutation (optional). A schema definition is then supplied to the
56 * validator and executor.
57 *
58 * Example:
59 *
60 * const MyAppSchema = new GraphQLSchema({
61 * query: MyAppQueryRootType,
62 * mutation: MyAppMutationRootType,
63 * })
64 *
65 * Note: If an array of `directives` are provided to GraphQLSchema, that will be
66 * the exact list of directives represented and allowed. If `directives` is not
67 * provided then a default set of the specified directives (e.g. @include and
68 * @skip) will be used. If you wish to provide *additional* directives to these
69 * specified directives, you must explicitly declare them. Example:
70 *
71 * const MyAppSchema = new GraphQLSchema({
72 * ...
73 * directives: specifiedDirectives.concat([ myCustomDirective ]),
74 * })
75 *
76 */
77
78var GraphQLSchema = exports.GraphQLSchema = function () {
79 function GraphQLSchema(config) {
80 var _this = this;
81
82 _classCallCheck(this, GraphQLSchema);
83
84 // If this schema was built from a source known to be valid, then it may be
85 // marked with assumeValid to avoid an additional type system validation.
86 if (config && config.assumeValid) {
87 this.__validationErrors = [];
88 } else {
89 // Otherwise check for common mistakes during construction to produce
90 // clear and early error messages.
91 !((typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object') ? (0, _invariant2.default)(0, 'Must provide configuration object.') : void 0;
92 !(!config.types || Array.isArray(config.types)) ? (0, _invariant2.default)(0, '"types" must be Array if provided but got: ' + String(config.types) + '.') : void 0;
93 !(!config.directives || Array.isArray(config.directives)) ? (0, _invariant2.default)(0, '"directives" must be Array if provided but got: ' + (String(config.directives) + '.')) : void 0;
94 }
95
96 this._queryType = config.query;
97 this._mutationType = config.mutation;
98 this._subscriptionType = config.subscription;
99 // Provide specified directives (e.g. @include and @skip) by default.
100 this._directives = config.directives || _directives.specifiedDirectives;
101 this.astNode = config.astNode;
102
103 // Build type map now to detect any errors within this schema.
104 var initialTypes = [this.getQueryType(), this.getMutationType(), this.getSubscriptionType(), _introspection.__Schema];
105
106 var types = config.types;
107 if (types) {
108 initialTypes = initialTypes.concat(types);
109 }
110
111 this._typeMap = initialTypes.reduce(typeMapReducer, Object.create(null));
112
113 // Keep track of all implementations by interface name.
114 this._implementations = Object.create(null);
115 Object.keys(this._typeMap).forEach(function (typeName) {
116 var type = _this._typeMap[typeName];
117 if ((0, _definition.isObjectType)(type)) {
118 type.getInterfaces().forEach(function (iface) {
119 var impls = _this._implementations[iface.name];
120 if (impls) {
121 impls.push(type);
122 } else {
123 _this._implementations[iface.name] = [type];
124 }
125 });
126 }
127 });
128 }
129 // Used as a cache for validateSchema().
130
131
132 GraphQLSchema.prototype.getQueryType = function getQueryType() {
133 return this._queryType;
134 };
135
136 GraphQLSchema.prototype.getMutationType = function getMutationType() {
137 return this._mutationType;
138 };
139
140 GraphQLSchema.prototype.getSubscriptionType = function getSubscriptionType() {
141 return this._subscriptionType;
142 };
143
144 GraphQLSchema.prototype.getTypeMap = function getTypeMap() {
145 return this._typeMap;
146 };
147
148 GraphQLSchema.prototype.getType = function getType(name) {
149 return this.getTypeMap()[name];
150 };
151
152 GraphQLSchema.prototype.getPossibleTypes = function getPossibleTypes(abstractType) {
153 if ((0, _definition.isUnionType)(abstractType)) {
154 return abstractType.getTypes();
155 }
156 return this._implementations[abstractType.name];
157 };
158
159 GraphQLSchema.prototype.isPossibleType = function isPossibleType(abstractType, possibleType) {
160 var possibleTypeMap = this._possibleTypeMap;
161 if (!possibleTypeMap) {
162 this._possibleTypeMap = possibleTypeMap = Object.create(null);
163 }
164
165 if (!possibleTypeMap[abstractType.name]) {
166 var possibleTypes = this.getPossibleTypes(abstractType);
167 !Array.isArray(possibleTypes) ? (0, _invariant2.default)(0, 'Could not find possible implementing types for ' + abstractType.name + ' ' + 'in schema. Check that schema.types is defined and is an array of ' + 'all possible types in the schema.') : void 0;
168 possibleTypeMap[abstractType.name] = possibleTypes.reduce(function (map, type) {
169 return map[type.name] = true, map;
170 }, Object.create(null));
171 }
172
173 return Boolean(possibleTypeMap[abstractType.name][possibleType.name]);
174 };
175
176 GraphQLSchema.prototype.getDirectives = function getDirectives() {
177 return this._directives;
178 };
179
180 GraphQLSchema.prototype.getDirective = function getDirective(name) {
181 return (0, _find2.default)(this.getDirectives(), function (directive) {
182 return directive.name === name;
183 });
184 };
185
186 return GraphQLSchema;
187}();
188
189function typeMapReducer(map, type) {
190 if (!type) {
191 return map;
192 }
193 if ((0, _definition.isWrappingType)(type)) {
194 return typeMapReducer(map, type.ofType);
195 }
196 if (map[type.name]) {
197 !(map[type.name] === type) ? (0, _invariant2.default)(0, 'Schema must contain unique named types but contains multiple ' + ('types named "' + type.name + '".')) : void 0;
198 return map;
199 }
200 map[type.name] = type;
201
202 var reducedMap = map;
203
204 if ((0, _definition.isUnionType)(type)) {
205 reducedMap = type.getTypes().reduce(typeMapReducer, reducedMap);
206 }
207
208 if ((0, _definition.isObjectType)(type)) {
209 reducedMap = type.getInterfaces().reduce(typeMapReducer, reducedMap);
210 }
211
212 if ((0, _definition.isObjectType)(type) || (0, _definition.isInterfaceType)(type)) {
213 var fieldMap = type.getFields();
214 Object.keys(fieldMap).forEach(function (fieldName) {
215 var field = fieldMap[fieldName];
216
217 if (field.args) {
218 var fieldArgTypes = field.args.map(function (arg) {
219 return arg.type;
220 });
221 reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
222 }
223 reducedMap = typeMapReducer(reducedMap, field.type);
224 });
225 }
226
227 if ((0, _definition.isInputObjectType)(type)) {
228 var _fieldMap = type.getFields();
229 Object.keys(_fieldMap).forEach(function (fieldName) {
230 var field = _fieldMap[fieldName];
231 reducedMap = typeMapReducer(reducedMap, field.type);
232 });
233 }
234
235 return reducedMap;
236}
\No newline at end of file