extendSchema.mjs 13 KB

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