UNPKG

14 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.extendSchema = extendSchema;
7
8var _flatMap = _interopRequireDefault(require("../polyfills/flatMap"));
9
10var _objectValues = _interopRequireDefault(require("../polyfills/objectValues"));
11
12var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
13
14var _mapValue = _interopRequireDefault(require("../jsutils/mapValue"));
15
16var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
17
18var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
19
20var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap"));
21
22var _kinds = require("../language/kinds");
23
24var _predicates = require("../language/predicates");
25
26var _validate = require("../validation/validate");
27
28var _directives = require("../type/directives");
29
30var _scalars = require("../type/scalars");
31
32var _introspection = require("../type/introspection");
33
34var _schema = require("../type/schema");
35
36var _definition = require("../type/definition");
37
38var _buildASTSchema = require("./buildASTSchema");
39
40function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41
42function 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
44function _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
46function _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 * Produces a new schema given an existing schema and a document which may
50 * contain GraphQL type extensions and definitions. The original schema will
51 * remain unaltered.
52 *
53 * Because a schema represents a graph of references, a schema cannot be
54 * extended without effectively making an entire copy. We do not know until it's
55 * too late if subgraphs remain unchanged.
56 *
57 * This algorithm copies the provided schema, applying extensions while
58 * producing the copy. The original schema remains unaltered.
59 *
60 * Accepts options as a third argument:
61 *
62 * - commentDescriptions:
63 * Provide true to use preceding comments as the description.
64 *
65 */
66function 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 } // Collect the type definitions and extensions found in the document.
73
74
75 var typeDefs = [];
76 var typeExtsMap = Object.create(null); // New directives and types are separate because a directives and types can
77 // have the same name. For example, a type named "skip".
78
79 var directiveDefs = [];
80 var schemaDef; // Schema extensions are collected which may add additional operation types.
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 } // If this document contains no new types, extensions, or directives then
101 // return the same unmodified GraphQLSchema instance.
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 } // Get the extended root operation types.
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 } // Then, incorporate schema definition and all schema extensions.
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 } // Support both original legacy names and extended legacy names.
158
159
160 var allowedLegacyNames = schemaConfig.allowedLegacyNames.concat(options && options.allowedLegacyNames || []); // Then produce and return a Schema with these types.
161
162 return new _schema.GraphQLSchema({
163 // Note: While this could make early assertions to get the correctly
164 // typed values, that would throw immediately while type system
165 // validation with validateSchema() will produce more actionable results.
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 }); // Below are functions used for producing this schema that have closed over
175 // this scope and have access to the schema, cache, and newly defined types.
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 // Builtin types are not extended.
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 } // Not reachable. All possible types have been considered.
220
221
222 /* istanbul ignore next */
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}