extendSchema.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.extendSchema = extendSchema;
  6. exports.extendSchemaImpl = extendSchemaImpl;
  7. exports.getDescription = getDescription;
  8. var _objectValues = _interopRequireDefault(require("../polyfills/objectValues.js"));
  9. var _keyMap = _interopRequireDefault(require("../jsutils/keyMap.js"));
  10. var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
  11. var _mapValue = _interopRequireDefault(require("../jsutils/mapValue.js"));
  12. var _invariant = _interopRequireDefault(require("../jsutils/invariant.js"));
  13. var _devAssert = _interopRequireDefault(require("../jsutils/devAssert.js"));
  14. var _kinds = require("../language/kinds.js");
  15. var _tokenKind = require("../language/tokenKind.js");
  16. var _blockString = require("../language/blockString.js");
  17. var _predicates = require("../language/predicates.js");
  18. var _validate = require("../validation/validate.js");
  19. var _values = require("../execution/values.js");
  20. var _schema = require("../type/schema.js");
  21. var _scalars = require("../type/scalars.js");
  22. var _introspection = require("../type/introspection.js");
  23. var _directives = require("../type/directives.js");
  24. var _definition = require("../type/definition.js");
  25. var _valueFromAST = require("./valueFromAST.js");
  26. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  27. 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; }
  28. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  29. 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; }
  30. /**
  31. * Produces a new schema given an existing schema and a document which may
  32. * contain GraphQL type extensions and definitions. The original schema will
  33. * remain unaltered.
  34. *
  35. * Because a schema represents a graph of references, a schema cannot be
  36. * extended without effectively making an entire copy. We do not know until it's
  37. * too late if subgraphs remain unchanged.
  38. *
  39. * This algorithm copies the provided schema, applying extensions while
  40. * producing the copy. The original schema remains unaltered.
  41. *
  42. * Accepts options as a third argument:
  43. *
  44. * - commentDescriptions:
  45. * Provide true to use preceding comments as the description.
  46. *
  47. */
  48. function extendSchema(schema, documentAST, options) {
  49. (0, _schema.assertSchema)(schema);
  50. documentAST != null && documentAST.kind === _kinds.Kind.DOCUMENT || (0, _devAssert.default)(0, 'Must provide valid Document AST.');
  51. if ((options === null || options === void 0 ? void 0 : options.assumeValid) !== true && (options === null || options === void 0 ? void 0 : options.assumeValidSDL) !== true) {
  52. (0, _validate.assertValidSDLExtension)(documentAST, schema);
  53. }
  54. var schemaConfig = schema.toConfig();
  55. var extendedConfig = extendSchemaImpl(schemaConfig, documentAST, options);
  56. return schemaConfig === extendedConfig ? schema : new _schema.GraphQLSchema(extendedConfig);
  57. }
  58. /**
  59. * @internal
  60. */
  61. function extendSchemaImpl(schemaConfig, documentAST, options) {
  62. var _schemaDef, _schemaDef$descriptio, _schemaDef2, _options$assumeValid;
  63. // Collect the type definitions and extensions found in the document.
  64. var typeDefs = [];
  65. var typeExtensionsMap = Object.create(null); // New directives and types are separate because a directives and types can
  66. // have the same name. For example, a type named "skip".
  67. var directiveDefs = [];
  68. var schemaDef; // Schema extensions are collected which may add additional operation types.
  69. var schemaExtensions = [];
  70. for (var _i2 = 0, _documentAST$definiti2 = documentAST.definitions; _i2 < _documentAST$definiti2.length; _i2++) {
  71. var def = _documentAST$definiti2[_i2];
  72. if (def.kind === _kinds.Kind.SCHEMA_DEFINITION) {
  73. schemaDef = def;
  74. } else if (def.kind === _kinds.Kind.SCHEMA_EXTENSION) {
  75. schemaExtensions.push(def);
  76. } else if ((0, _predicates.isTypeDefinitionNode)(def)) {
  77. typeDefs.push(def);
  78. } else if ((0, _predicates.isTypeExtensionNode)(def)) {
  79. var extendedTypeName = def.name.value;
  80. var existingTypeExtensions = typeExtensionsMap[extendedTypeName];
  81. typeExtensionsMap[extendedTypeName] = existingTypeExtensions ? existingTypeExtensions.concat([def]) : [def];
  82. } else if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  83. directiveDefs.push(def);
  84. }
  85. } // If this document contains no new types, extensions, or directives then
  86. // return the same unmodified GraphQLSchema instance.
  87. if (Object.keys(typeExtensionsMap).length === 0 && typeDefs.length === 0 && directiveDefs.length === 0 && schemaExtensions.length === 0 && schemaDef == null) {
  88. return schemaConfig;
  89. }
  90. var typeMap = Object.create(null);
  91. for (var _i4 = 0, _schemaConfig$types2 = schemaConfig.types; _i4 < _schemaConfig$types2.length; _i4++) {
  92. var existingType = _schemaConfig$types2[_i4];
  93. typeMap[existingType.name] = extendNamedType(existingType);
  94. }
  95. for (var _i6 = 0; _i6 < typeDefs.length; _i6++) {
  96. var _stdTypeMap$name;
  97. var typeNode = typeDefs[_i6];
  98. var name = typeNode.name.value;
  99. typeMap[name] = (_stdTypeMap$name = stdTypeMap[name]) !== null && _stdTypeMap$name !== void 0 ? _stdTypeMap$name : buildType(typeNode);
  100. }
  101. var operationTypes = _objectSpread(_objectSpread({
  102. // Get the extended root operation types.
  103. query: schemaConfig.query && replaceNamedType(schemaConfig.query),
  104. mutation: schemaConfig.mutation && replaceNamedType(schemaConfig.mutation),
  105. subscription: schemaConfig.subscription && replaceNamedType(schemaConfig.subscription)
  106. }, schemaDef && getOperationTypes([schemaDef])), getOperationTypes(schemaExtensions)); // Then produce and return a Schema config with these types.
  107. return _objectSpread(_objectSpread({
  108. description: (_schemaDef = schemaDef) === null || _schemaDef === void 0 ? void 0 : (_schemaDef$descriptio = _schemaDef.description) === null || _schemaDef$descriptio === void 0 ? void 0 : _schemaDef$descriptio.value
  109. }, operationTypes), {}, {
  110. types: (0, _objectValues.default)(typeMap),
  111. directives: [].concat(schemaConfig.directives.map(replaceDirective), directiveDefs.map(buildDirective)),
  112. extensions: undefined,
  113. astNode: (_schemaDef2 = schemaDef) !== null && _schemaDef2 !== void 0 ? _schemaDef2 : schemaConfig.astNode,
  114. extensionASTNodes: schemaConfig.extensionASTNodes.concat(schemaExtensions),
  115. assumeValid: (_options$assumeValid = options === null || options === void 0 ? void 0 : options.assumeValid) !== null && _options$assumeValid !== void 0 ? _options$assumeValid : false
  116. }); // Below are functions used for producing this schema that have closed over
  117. // this scope and have access to the schema, cache, and newly defined types.
  118. function replaceType(type) {
  119. if ((0, _definition.isListType)(type)) {
  120. // $FlowFixMe[incompatible-return]
  121. return new _definition.GraphQLList(replaceType(type.ofType));
  122. }
  123. if ((0, _definition.isNonNullType)(type)) {
  124. // $FlowFixMe[incompatible-return]
  125. return new _definition.GraphQLNonNull(replaceType(type.ofType));
  126. }
  127. return replaceNamedType(type);
  128. }
  129. function replaceNamedType(type) {
  130. // Note: While this could make early assertions to get the correctly
  131. // typed values, that would throw immediately while type system
  132. // validation with validateSchema() will produce more actionable results.
  133. return typeMap[type.name];
  134. }
  135. function replaceDirective(directive) {
  136. var config = directive.toConfig();
  137. return new _directives.GraphQLDirective(_objectSpread(_objectSpread({}, config), {}, {
  138. args: (0, _mapValue.default)(config.args, extendArg)
  139. }));
  140. }
  141. function extendNamedType(type) {
  142. if ((0, _introspection.isIntrospectionType)(type) || (0, _scalars.isSpecifiedScalarType)(type)) {
  143. // Builtin types are not extended.
  144. return type;
  145. }
  146. if ((0, _definition.isScalarType)(type)) {
  147. return extendScalarType(type);
  148. }
  149. if ((0, _definition.isObjectType)(type)) {
  150. return extendObjectType(type);
  151. }
  152. if ((0, _definition.isInterfaceType)(type)) {
  153. return extendInterfaceType(type);
  154. }
  155. if ((0, _definition.isUnionType)(type)) {
  156. return extendUnionType(type);
  157. }
  158. if ((0, _definition.isEnumType)(type)) {
  159. return extendEnumType(type);
  160. } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  161. if ((0, _definition.isInputObjectType)(type)) {
  162. return extendInputObjectType(type);
  163. } // istanbul ignore next (Not reachable. All possible types have been considered)
  164. false || (0, _invariant.default)(0, 'Unexpected type: ' + (0, _inspect.default)(type));
  165. }
  166. function extendInputObjectType(type) {
  167. var _typeExtensionsMap$co;
  168. var config = type.toConfig();
  169. var extensions = (_typeExtensionsMap$co = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co !== void 0 ? _typeExtensionsMap$co : [];
  170. return new _definition.GraphQLInputObjectType(_objectSpread(_objectSpread({}, config), {}, {
  171. fields: function fields() {
  172. return _objectSpread(_objectSpread({}, (0, _mapValue.default)(config.fields, function (field) {
  173. return _objectSpread(_objectSpread({}, field), {}, {
  174. type: replaceType(field.type)
  175. });
  176. })), buildInputFieldMap(extensions));
  177. },
  178. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  179. }));
  180. }
  181. function extendEnumType(type) {
  182. var _typeExtensionsMap$ty;
  183. var config = type.toConfig();
  184. var extensions = (_typeExtensionsMap$ty = typeExtensionsMap[type.name]) !== null && _typeExtensionsMap$ty !== void 0 ? _typeExtensionsMap$ty : [];
  185. return new _definition.GraphQLEnumType(_objectSpread(_objectSpread({}, config), {}, {
  186. values: _objectSpread(_objectSpread({}, config.values), buildEnumValueMap(extensions)),
  187. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  188. }));
  189. }
  190. function extendScalarType(type) {
  191. var _typeExtensionsMap$co2;
  192. var config = type.toConfig();
  193. var extensions = (_typeExtensionsMap$co2 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co2 !== void 0 ? _typeExtensionsMap$co2 : [];
  194. var specifiedByUrl = config.specifiedByUrl;
  195. for (var _i8 = 0; _i8 < extensions.length; _i8++) {
  196. var _getSpecifiedByUrl;
  197. var extensionNode = extensions[_i8];
  198. specifiedByUrl = (_getSpecifiedByUrl = getSpecifiedByUrl(extensionNode)) !== null && _getSpecifiedByUrl !== void 0 ? _getSpecifiedByUrl : specifiedByUrl;
  199. }
  200. return new _definition.GraphQLScalarType(_objectSpread(_objectSpread({}, config), {}, {
  201. specifiedByUrl: specifiedByUrl,
  202. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  203. }));
  204. }
  205. function extendObjectType(type) {
  206. var _typeExtensionsMap$co3;
  207. var config = type.toConfig();
  208. var extensions = (_typeExtensionsMap$co3 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co3 !== void 0 ? _typeExtensionsMap$co3 : [];
  209. return new _definition.GraphQLObjectType(_objectSpread(_objectSpread({}, config), {}, {
  210. interfaces: function interfaces() {
  211. return [].concat(type.getInterfaces().map(replaceNamedType), buildInterfaces(extensions));
  212. },
  213. fields: function fields() {
  214. return _objectSpread(_objectSpread({}, (0, _mapValue.default)(config.fields, extendField)), buildFieldMap(extensions));
  215. },
  216. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  217. }));
  218. }
  219. function extendInterfaceType(type) {
  220. var _typeExtensionsMap$co4;
  221. var config = type.toConfig();
  222. var extensions = (_typeExtensionsMap$co4 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co4 !== void 0 ? _typeExtensionsMap$co4 : [];
  223. return new _definition.GraphQLInterfaceType(_objectSpread(_objectSpread({}, config), {}, {
  224. interfaces: function interfaces() {
  225. return [].concat(type.getInterfaces().map(replaceNamedType), buildInterfaces(extensions));
  226. },
  227. fields: function fields() {
  228. return _objectSpread(_objectSpread({}, (0, _mapValue.default)(config.fields, extendField)), buildFieldMap(extensions));
  229. },
  230. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  231. }));
  232. }
  233. function extendUnionType(type) {
  234. var _typeExtensionsMap$co5;
  235. var config = type.toConfig();
  236. var extensions = (_typeExtensionsMap$co5 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co5 !== void 0 ? _typeExtensionsMap$co5 : [];
  237. return new _definition.GraphQLUnionType(_objectSpread(_objectSpread({}, config), {}, {
  238. types: function types() {
  239. return [].concat(type.getTypes().map(replaceNamedType), buildUnionTypes(extensions));
  240. },
  241. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  242. }));
  243. }
  244. function extendField(field) {
  245. return _objectSpread(_objectSpread({}, field), {}, {
  246. type: replaceType(field.type),
  247. // $FlowFixMe[incompatible-call]
  248. args: (0, _mapValue.default)(field.args, extendArg)
  249. });
  250. }
  251. function extendArg(arg) {
  252. return _objectSpread(_objectSpread({}, arg), {}, {
  253. type: replaceType(arg.type)
  254. });
  255. }
  256. function getOperationTypes(nodes) {
  257. var opTypes = {};
  258. for (var _i10 = 0; _i10 < nodes.length; _i10++) {
  259. var _node$operationTypes;
  260. var node = nodes[_i10];
  261. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  262. var operationTypesNodes = (_node$operationTypes = node.operationTypes) !== null && _node$operationTypes !== void 0 ? _node$operationTypes : [];
  263. for (var _i12 = 0; _i12 < operationTypesNodes.length; _i12++) {
  264. var operationType = operationTypesNodes[_i12];
  265. opTypes[operationType.operation] = getNamedType(operationType.type);
  266. }
  267. } // Note: While this could make early assertions to get the correctly
  268. // typed values below, that would throw immediately while type system
  269. // validation with validateSchema() will produce more actionable results.
  270. return opTypes;
  271. }
  272. function getNamedType(node) {
  273. var _stdTypeMap$name2;
  274. var name = node.name.value;
  275. var type = (_stdTypeMap$name2 = stdTypeMap[name]) !== null && _stdTypeMap$name2 !== void 0 ? _stdTypeMap$name2 : typeMap[name];
  276. if (type === undefined) {
  277. throw new Error("Unknown type: \"".concat(name, "\"."));
  278. }
  279. return type;
  280. }
  281. function getWrappedType(node) {
  282. if (node.kind === _kinds.Kind.LIST_TYPE) {
  283. return new _definition.GraphQLList(getWrappedType(node.type));
  284. }
  285. if (node.kind === _kinds.Kind.NON_NULL_TYPE) {
  286. return new _definition.GraphQLNonNull(getWrappedType(node.type));
  287. }
  288. return getNamedType(node);
  289. }
  290. function buildDirective(node) {
  291. var locations = node.locations.map(function (_ref) {
  292. var value = _ref.value;
  293. return value;
  294. });
  295. return new _directives.GraphQLDirective({
  296. name: node.name.value,
  297. description: getDescription(node, options),
  298. locations: locations,
  299. isRepeatable: node.repeatable,
  300. args: buildArgumentMap(node.arguments),
  301. astNode: node
  302. });
  303. }
  304. function buildFieldMap(nodes) {
  305. var fieldConfigMap = Object.create(null);
  306. for (var _i14 = 0; _i14 < nodes.length; _i14++) {
  307. var _node$fields;
  308. var node = nodes[_i14];
  309. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  310. var nodeFields = (_node$fields = node.fields) !== null && _node$fields !== void 0 ? _node$fields : [];
  311. for (var _i16 = 0; _i16 < nodeFields.length; _i16++) {
  312. var field = nodeFields[_i16];
  313. fieldConfigMap[field.name.value] = {
  314. // Note: While this could make assertions to get the correctly typed
  315. // value, that would throw immediately while type system validation
  316. // with validateSchema() will produce more actionable results.
  317. type: getWrappedType(field.type),
  318. description: getDescription(field, options),
  319. args: buildArgumentMap(field.arguments),
  320. deprecationReason: getDeprecationReason(field),
  321. astNode: field
  322. };
  323. }
  324. }
  325. return fieldConfigMap;
  326. }
  327. function buildArgumentMap(args) {
  328. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  329. var argsNodes = args !== null && args !== void 0 ? args : [];
  330. var argConfigMap = Object.create(null);
  331. for (var _i18 = 0; _i18 < argsNodes.length; _i18++) {
  332. var arg = argsNodes[_i18];
  333. // Note: While this could make assertions to get the correctly typed
  334. // value, that would throw immediately while type system validation
  335. // with validateSchema() will produce more actionable results.
  336. var type = getWrappedType(arg.type);
  337. argConfigMap[arg.name.value] = {
  338. type: type,
  339. description: getDescription(arg, options),
  340. defaultValue: (0, _valueFromAST.valueFromAST)(arg.defaultValue, type),
  341. deprecationReason: getDeprecationReason(arg),
  342. astNode: arg
  343. };
  344. }
  345. return argConfigMap;
  346. }
  347. function buildInputFieldMap(nodes) {
  348. var inputFieldMap = Object.create(null);
  349. for (var _i20 = 0; _i20 < nodes.length; _i20++) {
  350. var _node$fields2;
  351. var node = nodes[_i20];
  352. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  353. var fieldsNodes = (_node$fields2 = node.fields) !== null && _node$fields2 !== void 0 ? _node$fields2 : [];
  354. for (var _i22 = 0; _i22 < fieldsNodes.length; _i22++) {
  355. var field = fieldsNodes[_i22];
  356. // Note: While this could make assertions to get the correctly typed
  357. // value, that would throw immediately while type system validation
  358. // with validateSchema() will produce more actionable results.
  359. var type = getWrappedType(field.type);
  360. inputFieldMap[field.name.value] = {
  361. type: type,
  362. description: getDescription(field, options),
  363. defaultValue: (0, _valueFromAST.valueFromAST)(field.defaultValue, type),
  364. deprecationReason: getDeprecationReason(field),
  365. astNode: field
  366. };
  367. }
  368. }
  369. return inputFieldMap;
  370. }
  371. function buildEnumValueMap(nodes) {
  372. var enumValueMap = Object.create(null);
  373. for (var _i24 = 0; _i24 < nodes.length; _i24++) {
  374. var _node$values;
  375. var node = nodes[_i24];
  376. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  377. var valuesNodes = (_node$values = node.values) !== null && _node$values !== void 0 ? _node$values : [];
  378. for (var _i26 = 0; _i26 < valuesNodes.length; _i26++) {
  379. var value = valuesNodes[_i26];
  380. enumValueMap[value.name.value] = {
  381. description: getDescription(value, options),
  382. deprecationReason: getDeprecationReason(value),
  383. astNode: value
  384. };
  385. }
  386. }
  387. return enumValueMap;
  388. }
  389. function buildInterfaces(nodes) {
  390. var interfaces = [];
  391. for (var _i28 = 0; _i28 < nodes.length; _i28++) {
  392. var _node$interfaces;
  393. var node = nodes[_i28];
  394. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  395. var interfacesNodes = (_node$interfaces = node.interfaces) !== null && _node$interfaces !== void 0 ? _node$interfaces : [];
  396. for (var _i30 = 0; _i30 < interfacesNodes.length; _i30++) {
  397. var type = interfacesNodes[_i30];
  398. // Note: While this could make assertions to get the correctly typed
  399. // values below, that would throw immediately while type system
  400. // validation with validateSchema() will produce more actionable
  401. // results.
  402. interfaces.push(getNamedType(type));
  403. }
  404. }
  405. return interfaces;
  406. }
  407. function buildUnionTypes(nodes) {
  408. var types = [];
  409. for (var _i32 = 0; _i32 < nodes.length; _i32++) {
  410. var _node$types;
  411. var node = nodes[_i32];
  412. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  413. var typeNodes = (_node$types = node.types) !== null && _node$types !== void 0 ? _node$types : [];
  414. for (var _i34 = 0; _i34 < typeNodes.length; _i34++) {
  415. var type = typeNodes[_i34];
  416. // Note: While this could make assertions to get the correctly typed
  417. // values below, that would throw immediately while type system
  418. // validation with validateSchema() will produce more actionable
  419. // results.
  420. types.push(getNamedType(type));
  421. }
  422. }
  423. return types;
  424. }
  425. function buildType(astNode) {
  426. var _typeExtensionsMap$na;
  427. var name = astNode.name.value;
  428. var description = getDescription(astNode, options);
  429. var extensionNodes = (_typeExtensionsMap$na = typeExtensionsMap[name]) !== null && _typeExtensionsMap$na !== void 0 ? _typeExtensionsMap$na : [];
  430. switch (astNode.kind) {
  431. case _kinds.Kind.OBJECT_TYPE_DEFINITION:
  432. {
  433. var extensionASTNodes = extensionNodes;
  434. var allNodes = [astNode].concat(extensionASTNodes);
  435. return new _definition.GraphQLObjectType({
  436. name: name,
  437. description: description,
  438. interfaces: function interfaces() {
  439. return buildInterfaces(allNodes);
  440. },
  441. fields: function fields() {
  442. return buildFieldMap(allNodes);
  443. },
  444. astNode: astNode,
  445. extensionASTNodes: extensionASTNodes
  446. });
  447. }
  448. case _kinds.Kind.INTERFACE_TYPE_DEFINITION:
  449. {
  450. var _extensionASTNodes = extensionNodes;
  451. var _allNodes = [astNode].concat(_extensionASTNodes);
  452. return new _definition.GraphQLInterfaceType({
  453. name: name,
  454. description: description,
  455. interfaces: function interfaces() {
  456. return buildInterfaces(_allNodes);
  457. },
  458. fields: function fields() {
  459. return buildFieldMap(_allNodes);
  460. },
  461. astNode: astNode,
  462. extensionASTNodes: _extensionASTNodes
  463. });
  464. }
  465. case _kinds.Kind.ENUM_TYPE_DEFINITION:
  466. {
  467. var _extensionASTNodes2 = extensionNodes;
  468. var _allNodes2 = [astNode].concat(_extensionASTNodes2);
  469. return new _definition.GraphQLEnumType({
  470. name: name,
  471. description: description,
  472. values: buildEnumValueMap(_allNodes2),
  473. astNode: astNode,
  474. extensionASTNodes: _extensionASTNodes2
  475. });
  476. }
  477. case _kinds.Kind.UNION_TYPE_DEFINITION:
  478. {
  479. var _extensionASTNodes3 = extensionNodes;
  480. var _allNodes3 = [astNode].concat(_extensionASTNodes3);
  481. return new _definition.GraphQLUnionType({
  482. name: name,
  483. description: description,
  484. types: function types() {
  485. return buildUnionTypes(_allNodes3);
  486. },
  487. astNode: astNode,
  488. extensionASTNodes: _extensionASTNodes3
  489. });
  490. }
  491. case _kinds.Kind.SCALAR_TYPE_DEFINITION:
  492. {
  493. var _extensionASTNodes4 = extensionNodes;
  494. return new _definition.GraphQLScalarType({
  495. name: name,
  496. description: description,
  497. specifiedByUrl: getSpecifiedByUrl(astNode),
  498. astNode: astNode,
  499. extensionASTNodes: _extensionASTNodes4
  500. });
  501. }
  502. case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION:
  503. {
  504. var _extensionASTNodes5 = extensionNodes;
  505. var _allNodes4 = [astNode].concat(_extensionASTNodes5);
  506. return new _definition.GraphQLInputObjectType({
  507. name: name,
  508. description: description,
  509. fields: function fields() {
  510. return buildInputFieldMap(_allNodes4);
  511. },
  512. astNode: astNode,
  513. extensionASTNodes: _extensionASTNodes5
  514. });
  515. }
  516. } // istanbul ignore next (Not reachable. All possible type definition nodes have been considered)
  517. false || (0, _invariant.default)(0, 'Unexpected type definition node: ' + (0, _inspect.default)(astNode));
  518. }
  519. }
  520. var stdTypeMap = (0, _keyMap.default)(_scalars.specifiedScalarTypes.concat(_introspection.introspectionTypes), function (type) {
  521. return type.name;
  522. });
  523. /**
  524. * Given a field or enum value node, returns the string value for the
  525. * deprecation reason.
  526. */
  527. function getDeprecationReason(node) {
  528. var deprecated = (0, _values.getDirectiveValues)(_directives.GraphQLDeprecatedDirective, node);
  529. return deprecated === null || deprecated === void 0 ? void 0 : deprecated.reason;
  530. }
  531. /**
  532. * Given a scalar node, returns the string value for the specifiedByUrl.
  533. */
  534. function getSpecifiedByUrl(node) {
  535. var specifiedBy = (0, _values.getDirectiveValues)(_directives.GraphQLSpecifiedByDirective, node);
  536. return specifiedBy === null || specifiedBy === void 0 ? void 0 : specifiedBy.url;
  537. }
  538. /**
  539. * Given an ast node, returns its string description.
  540. * @deprecated: provided to ease adoption and will be removed in v16.
  541. *
  542. * Accepts options as a second argument:
  543. *
  544. * - commentDescriptions:
  545. * Provide true to use preceding comments as the description.
  546. *
  547. */
  548. function getDescription(node, options) {
  549. if (node.description) {
  550. return node.description.value;
  551. }
  552. if ((options === null || options === void 0 ? void 0 : options.commentDescriptions) === true) {
  553. var rawValue = getLeadingCommentBlock(node);
  554. if (rawValue !== undefined) {
  555. return (0, _blockString.dedentBlockStringValue)('\n' + rawValue);
  556. }
  557. }
  558. }
  559. function getLeadingCommentBlock(node) {
  560. var loc = node.loc;
  561. if (!loc) {
  562. return;
  563. }
  564. var comments = [];
  565. var token = loc.startToken.prev;
  566. while (token != null && token.kind === _tokenKind.TokenKind.COMMENT && token.next && token.prev && token.line + 1 === token.next.line && token.line !== token.prev.line) {
  567. var value = String(token.value);
  568. comments.push(value);
  569. token = token.prev;
  570. }
  571. return comments.length > 0 ? comments.reverse().join('\n') : undefined;
  572. }