UNPKG

13.2 kBJavaScriptView Raw
1function 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; }
2
3function _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; }
4
5function _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; }
6
7import flatMap from '../polyfills/flatMap';
8import objectValues from '../polyfills/objectValues';
9import inspect from '../jsutils/inspect';
10import mapValue from '../jsutils/mapValue';
11import invariant from '../jsutils/invariant';
12import devAssert from '../jsutils/devAssert';
13import keyValMap from '../jsutils/keyValMap';
14import { Kind } from '../language/kinds';
15import { isTypeDefinitionNode, isTypeExtensionNode } from '../language/predicates';
16import { assertValidSDLExtension } from '../validation/validate';
17import { GraphQLDirective } from '../type/directives';
18import { isSpecifiedScalarType } from '../type/scalars';
19import { isIntrospectionType } from '../type/introspection';
20import { assertSchema, GraphQLSchema } from '../type/schema';
21import { isScalarType, isObjectType, isInterfaceType, isUnionType, isListType, isNonNullType, isEnumType, isInputObjectType, GraphQLList, GraphQLNonNull, GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType } from '../type/definition';
22import { ASTDefinitionBuilder } from './buildASTSchema';
23
24/**
25 * Produces a new schema given an existing schema and a document which may
26 * contain GraphQL type extensions and definitions. The original schema will
27 * remain unaltered.
28 *
29 * Because a schema represents a graph of references, a schema cannot be
30 * extended without effectively making an entire copy. We do not know until it's
31 * too late if subgraphs remain unchanged.
32 *
33 * This algorithm copies the provided schema, applying extensions while
34 * producing the copy. The original schema remains unaltered.
35 *
36 * Accepts options as a third argument:
37 *
38 * - commentDescriptions:
39 * Provide true to use preceding comments as the description.
40 *
41 */
42export function extendSchema(schema, documentAST, options) {
43 assertSchema(schema);
44 documentAST && documentAST.kind === Kind.DOCUMENT || devAssert(0, 'Must provide valid Document AST');
45
46 if (!options || !(options.assumeValid || options.assumeValidSDL)) {
47 assertValidSDLExtension(documentAST, schema);
48 } // Collect the type definitions and extensions found in the document.
49
50
51 var typeDefs = [];
52 var typeExtsMap = Object.create(null); // New directives and types are separate because a directives and types can
53 // have the same name. For example, a type named "skip".
54
55 var directiveDefs = [];
56 var schemaDef; // Schema extensions are collected which may add additional operation types.
57
58 var schemaExts = [];
59
60 for (var _i2 = 0, _documentAST$definiti2 = documentAST.definitions; _i2 < _documentAST$definiti2.length; _i2++) {
61 var def = _documentAST$definiti2[_i2];
62
63 if (def.kind === Kind.SCHEMA_DEFINITION) {
64 schemaDef = def;
65 } else if (def.kind === Kind.SCHEMA_EXTENSION) {
66 schemaExts.push(def);
67 } else if (isTypeDefinitionNode(def)) {
68 typeDefs.push(def);
69 } else if (isTypeExtensionNode(def)) {
70 var extendedTypeName = def.name.value;
71 var existingTypeExts = typeExtsMap[extendedTypeName];
72 typeExtsMap[extendedTypeName] = existingTypeExts ? existingTypeExts.concat([def]) : [def];
73 } else if (def.kind === Kind.DIRECTIVE_DEFINITION) {
74 directiveDefs.push(def);
75 }
76 } // If this document contains no new types, extensions, or directives then
77 // return the same unmodified GraphQLSchema instance.
78
79
80 if (Object.keys(typeExtsMap).length === 0 && typeDefs.length === 0 && directiveDefs.length === 0 && schemaExts.length === 0 && !schemaDef) {
81 return schema;
82 }
83
84 var schemaConfig = schema.toConfig();
85 var astBuilder = new ASTDefinitionBuilder(options, function (typeName) {
86 var type = typeMap[typeName];
87
88 if (type === undefined) {
89 throw new Error("Unknown type: \"".concat(typeName, "\"."));
90 }
91
92 return type;
93 });
94 var typeMap = keyValMap(typeDefs, function (node) {
95 return node.name.value;
96 }, function (node) {
97 return astBuilder.buildType(node);
98 });
99
100 for (var _i4 = 0, _schemaConfig$types2 = schemaConfig.types; _i4 < _schemaConfig$types2.length; _i4++) {
101 var existingType = _schemaConfig$types2[_i4];
102 typeMap[existingType.name] = extendNamedType(existingType);
103 } // Get the extended root operation types.
104
105
106 var operationTypes = {
107 query: schemaConfig.query && schemaConfig.query.name,
108 mutation: schemaConfig.mutation && schemaConfig.mutation.name,
109 subscription: schemaConfig.subscription && schemaConfig.subscription.name
110 };
111
112 if (schemaDef) {
113 for (var _i6 = 0, _schemaDef$operationT2 = schemaDef.operationTypes; _i6 < _schemaDef$operationT2.length; _i6++) {
114 var _ref2 = _schemaDef$operationT2[_i6];
115 var operation = _ref2.operation;
116 var type = _ref2.type;
117 operationTypes[operation] = type.name.value;
118 }
119 } // Then, incorporate schema definition and all schema extensions.
120
121
122 for (var _i8 = 0; _i8 < schemaExts.length; _i8++) {
123 var schemaExt = schemaExts[_i8];
124
125 if (schemaExt.operationTypes) {
126 for (var _i10 = 0, _schemaExt$operationT2 = schemaExt.operationTypes; _i10 < _schemaExt$operationT2.length; _i10++) {
127 var _ref4 = _schemaExt$operationT2[_i10];
128 var _operation = _ref4.operation;
129 var _type = _ref4.type;
130 operationTypes[_operation] = _type.name.value;
131 }
132 }
133 } // Support both original legacy names and extended legacy names.
134
135
136 var allowedLegacyNames = schemaConfig.allowedLegacyNames.concat(options && options.allowedLegacyNames || []); // Then produce and return a Schema with these types.
137
138 return new GraphQLSchema({
139 // Note: While this could make early assertions to get the correctly
140 // typed values, that would throw immediately while type system
141 // validation with validateSchema() will produce more actionable results.
142 query: getMaybeTypeByName(operationTypes.query),
143 mutation: getMaybeTypeByName(operationTypes.mutation),
144 subscription: getMaybeTypeByName(operationTypes.subscription),
145 types: objectValues(typeMap),
146 directives: getMergedDirectives(),
147 astNode: schemaDef || schemaConfig.astNode,
148 extensionASTNodes: schemaConfig.extensionASTNodes.concat(schemaExts),
149 allowedLegacyNames: allowedLegacyNames
150 }); // Below are functions used for producing this schema that have closed over
151 // this scope and have access to the schema, cache, and newly defined types.
152
153 function replaceType(type) {
154 if (isListType(type)) {
155 return new GraphQLList(replaceType(type.ofType));
156 } else if (isNonNullType(type)) {
157 return new GraphQLNonNull(replaceType(type.ofType));
158 }
159
160 return replaceNamedType(type);
161 }
162
163 function replaceNamedType(type) {
164 return typeMap[type.name];
165 }
166
167 function getMaybeTypeByName(typeName) {
168 return typeName ? typeMap[typeName] : null;
169 }
170
171 function getMergedDirectives() {
172 var existingDirectives = schema.getDirectives().map(extendDirective);
173 existingDirectives || devAssert(0, 'schema must have default directives');
174 return existingDirectives.concat(directiveDefs.map(function (node) {
175 return astBuilder.buildDirective(node);
176 }));
177 }
178
179 function extendNamedType(type) {
180 if (isIntrospectionType(type) || isSpecifiedScalarType(type)) {
181 // Builtin types are not extended.
182 return type;
183 } else if (isScalarType(type)) {
184 return extendScalarType(type);
185 } else if (isObjectType(type)) {
186 return extendObjectType(type);
187 } else if (isInterfaceType(type)) {
188 return extendInterfaceType(type);
189 } else if (isUnionType(type)) {
190 return extendUnionType(type);
191 } else if (isEnumType(type)) {
192 return extendEnumType(type);
193 } else if (isInputObjectType(type)) {
194 return extendInputObjectType(type);
195 } // Not reachable. All possible types have been considered.
196
197
198 /* istanbul ignore next */
199 invariant(false, 'Unexpected type: ' + inspect(type));
200 }
201
202 function extendDirective(directive) {
203 var config = directive.toConfig();
204 return new GraphQLDirective(_objectSpread({}, config, {
205 args: mapValue(config.args, extendArg)
206 }));
207 }
208
209 function extendInputObjectType(type) {
210 var config = type.toConfig();
211 var extensions = typeExtsMap[config.name] || [];
212 var fieldNodes = flatMap(extensions, function (node) {
213 return node.fields || [];
214 });
215 return new GraphQLInputObjectType(_objectSpread({}, config, {
216 fields: function fields() {
217 return _objectSpread({}, mapValue(config.fields, function (field) {
218 return _objectSpread({}, field, {
219 type: replaceType(field.type)
220 });
221 }), {}, keyValMap(fieldNodes, function (field) {
222 return field.name.value;
223 }, function (field) {
224 return astBuilder.buildInputField(field);
225 }));
226 },
227 extensionASTNodes: config.extensionASTNodes.concat(extensions)
228 }));
229 }
230
231 function extendEnumType(type) {
232 var config = type.toConfig();
233 var extensions = typeExtsMap[type.name] || [];
234 var valueNodes = flatMap(extensions, function (node) {
235 return node.values || [];
236 });
237 return new GraphQLEnumType(_objectSpread({}, config, {
238 values: _objectSpread({}, config.values, {}, keyValMap(valueNodes, function (value) {
239 return value.name.value;
240 }, function (value) {
241 return astBuilder.buildEnumValue(value);
242 })),
243 extensionASTNodes: config.extensionASTNodes.concat(extensions)
244 }));
245 }
246
247 function extendScalarType(type) {
248 var config = type.toConfig();
249 var extensions = typeExtsMap[config.name] || [];
250 return new GraphQLScalarType(_objectSpread({}, config, {
251 extensionASTNodes: config.extensionASTNodes.concat(extensions)
252 }));
253 }
254
255 function extendObjectType(type) {
256 var config = type.toConfig();
257 var extensions = typeExtsMap[config.name] || [];
258 var interfaceNodes = flatMap(extensions, function (node) {
259 return node.interfaces || [];
260 });
261 var fieldNodes = flatMap(extensions, function (node) {
262 return node.fields || [];
263 });
264 return new GraphQLObjectType(_objectSpread({}, config, {
265 interfaces: function interfaces() {
266 return [].concat(type.getInterfaces().map(replaceNamedType), interfaceNodes.map(function (node) {
267 return astBuilder.getNamedType(node);
268 }));
269 },
270 fields: function fields() {
271 return _objectSpread({}, mapValue(config.fields, extendField), {}, keyValMap(fieldNodes, function (node) {
272 return node.name.value;
273 }, function (node) {
274 return astBuilder.buildField(node);
275 }));
276 },
277 extensionASTNodes: config.extensionASTNodes.concat(extensions)
278 }));
279 }
280
281 function extendInterfaceType(type) {
282 var config = type.toConfig();
283 var extensions = typeExtsMap[config.name] || [];
284 var fieldNodes = flatMap(extensions, function (node) {
285 return node.fields || [];
286 });
287 return new GraphQLInterfaceType(_objectSpread({}, config, {
288 fields: function fields() {
289 return _objectSpread({}, mapValue(config.fields, extendField), {}, keyValMap(fieldNodes, function (node) {
290 return node.name.value;
291 }, function (node) {
292 return astBuilder.buildField(node);
293 }));
294 },
295 extensionASTNodes: config.extensionASTNodes.concat(extensions)
296 }));
297 }
298
299 function extendUnionType(type) {
300 var config = type.toConfig();
301 var extensions = typeExtsMap[config.name] || [];
302 var typeNodes = flatMap(extensions, function (node) {
303 return node.types || [];
304 });
305 return new GraphQLUnionType(_objectSpread({}, config, {
306 types: function types() {
307 return [].concat(type.getTypes().map(replaceNamedType), typeNodes.map(function (node) {
308 return astBuilder.getNamedType(node);
309 }));
310 },
311 extensionASTNodes: config.extensionASTNodes.concat(extensions)
312 }));
313 }
314
315 function extendField(field) {
316 return _objectSpread({}, field, {
317 type: replaceType(field.type),
318 args: mapValue(field.args, extendArg)
319 });
320 }
321
322 function extendArg(arg) {
323 return _objectSpread({}, arg, {
324 type: replaceType(arg.type)
325 });
326 }
327}