1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.GraphQLSchema = undefined;
|
7 |
|
8 | var _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 |
|
10 | exports.isSchema = isSchema;
|
11 |
|
12 | var _definition = require('./definition');
|
13 |
|
14 | var _directives = require('./directives');
|
15 |
|
16 | var _introspection = require('./introspection');
|
17 |
|
18 | var _find = require('../jsutils/find');
|
19 |
|
20 | var _find2 = _interopRequireDefault(_find);
|
21 |
|
22 | var _instanceOf = require('../jsutils/instanceOf');
|
23 |
|
24 | var _instanceOf2 = _interopRequireDefault(_instanceOf);
|
25 |
|
26 | var _invariant = require('../jsutils/invariant');
|
27 |
|
28 | var _invariant2 = _interopRequireDefault(_invariant);
|
29 |
|
30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
31 |
|
32 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function isSchema(schema) {
|
48 | return (0, _instanceOf2.default)(schema, GraphQLSchema);
|
49 | }
|
50 |
|
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 | var GraphQLSchema = exports.GraphQLSchema = function () {
|
79 | function GraphQLSchema(config) {
|
80 | var _this = this;
|
81 |
|
82 | _classCallCheck(this, GraphQLSchema);
|
83 |
|
84 |
|
85 |
|
86 | if (config && config.assumeValid) {
|
87 | this.__validationErrors = [];
|
88 | } else {
|
89 |
|
90 |
|
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 |
|
100 | this._directives = config.directives || _directives.specifiedDirectives;
|
101 | this.astNode = config.astNode;
|
102 |
|
103 |
|
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 |
|
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 |
|
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 |
|
189 | function 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 |