extendSchema.js 14 KB

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