1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.extendSchema = extendSchema;
|
7 |
|
8 | var _flatMap = _interopRequireDefault(require("../polyfills/flatMap"));
|
9 |
|
10 | var _objectValues = _interopRequireDefault(require("../polyfills/objectValues"));
|
11 |
|
12 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
13 |
|
14 | var _mapValue = _interopRequireDefault(require("../jsutils/mapValue"));
|
15 |
|
16 | var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
|
17 |
|
18 | var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
|
19 |
|
20 | var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap"));
|
21 |
|
22 | var _kinds = require("../language/kinds");
|
23 |
|
24 | var _predicates = require("../language/predicates");
|
25 |
|
26 | var _validate = require("../validation/validate");
|
27 |
|
28 | var _directives = require("../type/directives");
|
29 |
|
30 | var _scalars = require("../type/scalars");
|
31 |
|
32 | var _introspection = require("../type/introspection");
|
33 |
|
34 | var _schema = require("../type/schema");
|
35 |
|
36 | var _definition = require("../type/definition");
|
37 |
|
38 | var _buildASTSchema = require("./buildASTSchema");
|
39 |
|
40 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
41 |
|
42 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
43 |
|
44 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
45 |
|
46 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function extendSchema(schema, documentAST, options) {
|
67 | (0, _schema.assertSchema)(schema);
|
68 | documentAST && documentAST.kind === _kinds.Kind.DOCUMENT || (0, _devAssert.default)(0, 'Must provide valid Document AST');
|
69 |
|
70 | if (!options || !(options.assumeValid || options.assumeValidSDL)) {
|
71 | (0, _validate.assertValidSDLExtension)(documentAST, schema);
|
72 | }
|
73 |
|
74 |
|
75 | var typeDefs = [];
|
76 | var typeExtsMap = Object.create(null);
|
77 |
|
78 |
|
79 | var directiveDefs = [];
|
80 | var schemaDef;
|
81 |
|
82 | var schemaExts = [];
|
83 |
|
84 | for (var _i2 = 0, _documentAST$definiti2 = documentAST.definitions; _i2 < _documentAST$definiti2.length; _i2++) {
|
85 | var def = _documentAST$definiti2[_i2];
|
86 |
|
87 | if (def.kind === _kinds.Kind.SCHEMA_DEFINITION) {
|
88 | schemaDef = def;
|
89 | } else if (def.kind === _kinds.Kind.SCHEMA_EXTENSION) {
|
90 | schemaExts.push(def);
|
91 | } else if ((0, _predicates.isTypeDefinitionNode)(def)) {
|
92 | typeDefs.push(def);
|
93 | } else if ((0, _predicates.isTypeExtensionNode)(def)) {
|
94 | var extendedTypeName = def.name.value;
|
95 | var existingTypeExts = typeExtsMap[extendedTypeName];
|
96 | typeExtsMap[extendedTypeName] = existingTypeExts ? existingTypeExts.concat([def]) : [def];
|
97 | } else if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
|
98 | directiveDefs.push(def);
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 | if (Object.keys(typeExtsMap).length === 0 && typeDefs.length === 0 && directiveDefs.length === 0 && schemaExts.length === 0 && !schemaDef) {
|
105 | return schema;
|
106 | }
|
107 |
|
108 | var schemaConfig = schema.toConfig();
|
109 | var astBuilder = new _buildASTSchema.ASTDefinitionBuilder(options, function (typeName) {
|
110 | var type = typeMap[typeName];
|
111 |
|
112 | if (type === undefined) {
|
113 | throw new Error("Unknown type: \"".concat(typeName, "\"."));
|
114 | }
|
115 |
|
116 | return type;
|
117 | });
|
118 | var typeMap = (0, _keyValMap.default)(typeDefs, function (node) {
|
119 | return node.name.value;
|
120 | }, function (node) {
|
121 | return astBuilder.buildType(node);
|
122 | });
|
123 |
|
124 | for (var _i4 = 0, _schemaConfig$types2 = schemaConfig.types; _i4 < _schemaConfig$types2.length; _i4++) {
|
125 | var existingType = _schemaConfig$types2[_i4];
|
126 | typeMap[existingType.name] = extendNamedType(existingType);
|
127 | }
|
128 |
|
129 |
|
130 | var operationTypes = {
|
131 | query: schemaConfig.query && schemaConfig.query.name,
|
132 | mutation: schemaConfig.mutation && schemaConfig.mutation.name,
|
133 | subscription: schemaConfig.subscription && schemaConfig.subscription.name
|
134 | };
|
135 |
|
136 | if (schemaDef) {
|
137 | for (var _i6 = 0, _schemaDef$operationT2 = schemaDef.operationTypes; _i6 < _schemaDef$operationT2.length; _i6++) {
|
138 | var _ref2 = _schemaDef$operationT2[_i6];
|
139 | var operation = _ref2.operation;
|
140 | var type = _ref2.type;
|
141 | operationTypes[operation] = type.name.value;
|
142 | }
|
143 | }
|
144 |
|
145 |
|
146 | for (var _i8 = 0; _i8 < schemaExts.length; _i8++) {
|
147 | var schemaExt = schemaExts[_i8];
|
148 |
|
149 | if (schemaExt.operationTypes) {
|
150 | for (var _i10 = 0, _schemaExt$operationT2 = schemaExt.operationTypes; _i10 < _schemaExt$operationT2.length; _i10++) {
|
151 | var _ref4 = _schemaExt$operationT2[_i10];
|
152 | var _operation = _ref4.operation;
|
153 | var _type = _ref4.type;
|
154 | operationTypes[_operation] = _type.name.value;
|
155 | }
|
156 | }
|
157 | }
|
158 |
|
159 |
|
160 | var allowedLegacyNames = schemaConfig.allowedLegacyNames.concat(options && options.allowedLegacyNames || []);
|
161 |
|
162 | return new _schema.GraphQLSchema({
|
163 |
|
164 |
|
165 |
|
166 | query: getMaybeTypeByName(operationTypes.query),
|
167 | mutation: getMaybeTypeByName(operationTypes.mutation),
|
168 | subscription: getMaybeTypeByName(operationTypes.subscription),
|
169 | types: (0, _objectValues.default)(typeMap),
|
170 | directives: getMergedDirectives(),
|
171 | astNode: schemaDef || schemaConfig.astNode,
|
172 | extensionASTNodes: schemaConfig.extensionASTNodes.concat(schemaExts),
|
173 | allowedLegacyNames: allowedLegacyNames
|
174 | });
|
175 |
|
176 |
|
177 | function replaceType(type) {
|
178 | if ((0, _definition.isListType)(type)) {
|
179 | return new _definition.GraphQLList(replaceType(type.ofType));
|
180 | } else if ((0, _definition.isNonNullType)(type)) {
|
181 | return new _definition.GraphQLNonNull(replaceType(type.ofType));
|
182 | }
|
183 |
|
184 | return replaceNamedType(type);
|
185 | }
|
186 |
|
187 | function replaceNamedType(type) {
|
188 | return typeMap[type.name];
|
189 | }
|
190 |
|
191 | function getMaybeTypeByName(typeName) {
|
192 | return typeName ? typeMap[typeName] : null;
|
193 | }
|
194 |
|
195 | function getMergedDirectives() {
|
196 | var existingDirectives = schema.getDirectives().map(extendDirective);
|
197 | existingDirectives || (0, _devAssert.default)(0, 'schema must have default directives');
|
198 | return existingDirectives.concat(directiveDefs.map(function (node) {
|
199 | return astBuilder.buildDirective(node);
|
200 | }));
|
201 | }
|
202 |
|
203 | function extendNamedType(type) {
|
204 | if ((0, _introspection.isIntrospectionType)(type) || (0, _scalars.isSpecifiedScalarType)(type)) {
|
205 |
|
206 | return type;
|
207 | } else if ((0, _definition.isScalarType)(type)) {
|
208 | return extendScalarType(type);
|
209 | } else if ((0, _definition.isObjectType)(type)) {
|
210 | return extendObjectType(type);
|
211 | } else if ((0, _definition.isInterfaceType)(type)) {
|
212 | return extendInterfaceType(type);
|
213 | } else if ((0, _definition.isUnionType)(type)) {
|
214 | return extendUnionType(type);
|
215 | } else if ((0, _definition.isEnumType)(type)) {
|
216 | return extendEnumType(type);
|
217 | } else if ((0, _definition.isInputObjectType)(type)) {
|
218 | return extendInputObjectType(type);
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 | (0, _invariant.default)(false, 'Unexpected type: ' + (0, _inspect.default)(type));
|
224 | }
|
225 |
|
226 | function extendDirective(directive) {
|
227 | var config = directive.toConfig();
|
228 | return new _directives.GraphQLDirective(_objectSpread({}, config, {
|
229 | args: (0, _mapValue.default)(config.args, extendArg)
|
230 | }));
|
231 | }
|
232 |
|
233 | function extendInputObjectType(type) {
|
234 | var config = type.toConfig();
|
235 | var extensions = typeExtsMap[config.name] || [];
|
236 | var fieldNodes = (0, _flatMap.default)(extensions, function (node) {
|
237 | return node.fields || [];
|
238 | });
|
239 | return new _definition.GraphQLInputObjectType(_objectSpread({}, config, {
|
240 | fields: function fields() {
|
241 | return _objectSpread({}, (0, _mapValue.default)(config.fields, function (field) {
|
242 | return _objectSpread({}, field, {
|
243 | type: replaceType(field.type)
|
244 | });
|
245 | }), {}, (0, _keyValMap.default)(fieldNodes, function (field) {
|
246 | return field.name.value;
|
247 | }, function (field) {
|
248 | return astBuilder.buildInputField(field);
|
249 | }));
|
250 | },
|
251 | extensionASTNodes: config.extensionASTNodes.concat(extensions)
|
252 | }));
|
253 | }
|
254 |
|
255 | function extendEnumType(type) {
|
256 | var config = type.toConfig();
|
257 | var extensions = typeExtsMap[type.name] || [];
|
258 | var valueNodes = (0, _flatMap.default)(extensions, function (node) {
|
259 | return node.values || [];
|
260 | });
|
261 | return new _definition.GraphQLEnumType(_objectSpread({}, config, {
|
262 | values: _objectSpread({}, config.values, {}, (0, _keyValMap.default)(valueNodes, function (value) {
|
263 | return value.name.value;
|
264 | }, function (value) {
|
265 | return astBuilder.buildEnumValue(value);
|
266 | })),
|
267 | extensionASTNodes: config.extensionASTNodes.concat(extensions)
|
268 | }));
|
269 | }
|
270 |
|
271 | function extendScalarType(type) {
|
272 | var config = type.toConfig();
|
273 | var extensions = typeExtsMap[config.name] || [];
|
274 | return new _definition.GraphQLScalarType(_objectSpread({}, config, {
|
275 | extensionASTNodes: config.extensionASTNodes.concat(extensions)
|
276 | }));
|
277 | }
|
278 |
|
279 | function extendObjectType(type) {
|
280 | var config = type.toConfig();
|
281 | var extensions = typeExtsMap[config.name] || [];
|
282 | var interfaceNodes = (0, _flatMap.default)(extensions, function (node) {
|
283 | return node.interfaces || [];
|
284 | });
|
285 | var fieldNodes = (0, _flatMap.default)(extensions, function (node) {
|
286 | return node.fields || [];
|
287 | });
|
288 | return new _definition.GraphQLObjectType(_objectSpread({}, config, {
|
289 | interfaces: function interfaces() {
|
290 | return [].concat(type.getInterfaces().map(replaceNamedType), interfaceNodes.map(function (node) {
|
291 | return astBuilder.getNamedType(node);
|
292 | }));
|
293 | },
|
294 | fields: function fields() {
|
295 | return _objectSpread({}, (0, _mapValue.default)(config.fields, extendField), {}, (0, _keyValMap.default)(fieldNodes, function (node) {
|
296 | return node.name.value;
|
297 | }, function (node) {
|
298 | return astBuilder.buildField(node);
|
299 | }));
|
300 | },
|
301 | extensionASTNodes: config.extensionASTNodes.concat(extensions)
|
302 | }));
|
303 | }
|
304 |
|
305 | function extendInterfaceType(type) {
|
306 | var config = type.toConfig();
|
307 | var extensions = typeExtsMap[config.name] || [];
|
308 | var fieldNodes = (0, _flatMap.default)(extensions, function (node) {
|
309 | return node.fields || [];
|
310 | });
|
311 | return new _definition.GraphQLInterfaceType(_objectSpread({}, config, {
|
312 | fields: function fields() {
|
313 | return _objectSpread({}, (0, _mapValue.default)(config.fields, extendField), {}, (0, _keyValMap.default)(fieldNodes, function (node) {
|
314 | return node.name.value;
|
315 | }, function (node) {
|
316 | return astBuilder.buildField(node);
|
317 | }));
|
318 | },
|
319 | extensionASTNodes: config.extensionASTNodes.concat(extensions)
|
320 | }));
|
321 | }
|
322 |
|
323 | function extendUnionType(type) {
|
324 | var config = type.toConfig();
|
325 | var extensions = typeExtsMap[config.name] || [];
|
326 | var typeNodes = (0, _flatMap.default)(extensions, function (node) {
|
327 | return node.types || [];
|
328 | });
|
329 | return new _definition.GraphQLUnionType(_objectSpread({}, config, {
|
330 | types: function types() {
|
331 | return [].concat(type.getTypes().map(replaceNamedType), typeNodes.map(function (node) {
|
332 | return astBuilder.getNamedType(node);
|
333 | }));
|
334 | },
|
335 | extensionASTNodes: config.extensionASTNodes.concat(extensions)
|
336 | }));
|
337 | }
|
338 |
|
339 | function extendField(field) {
|
340 | return _objectSpread({}, field, {
|
341 | type: replaceType(field.type),
|
342 | args: (0, _mapValue.default)(field.args, extendArg)
|
343 | });
|
344 | }
|
345 |
|
346 | function extendArg(arg) {
|
347 | return _objectSpread({}, arg, {
|
348 | type: replaceType(arg.type)
|
349 | });
|
350 | }
|
351 | }
|