extendSchema.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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"));
  9. var _keyMap = _interopRequireDefault(require("../jsutils/keyMap"));
  10. var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
  11. var _mapValue = _interopRequireDefault(require("../jsutils/mapValue"));
  12. var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
  13. var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
  14. var _kinds = require("../language/kinds");
  15. var _tokenKind = require("../language/tokenKind");
  16. var _blockString = require("../language/blockString");
  17. var _predicates = require("../language/predicates");
  18. var _validate = require("../validation/validate");
  19. var _values = require("../execution/values");
  20. var _schema = require("../type/schema");
  21. var _scalars = require("../type/scalars");
  22. var _introspection = require("../type/introspection");
  23. var _directives = require("../type/directives");
  24. var _definition = require("../type/definition");
  25. var _valueFromAST = require("./valueFromAST");
  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. return new _definition.GraphQLList(replaceType(type.ofType));
  121. } else if ((0, _definition.isNonNullType)(type)) {
  122. return new _definition.GraphQLNonNull(replaceType(type.ofType));
  123. }
  124. return replaceNamedType(type);
  125. }
  126. function replaceNamedType(type) {
  127. // Note: While this could make early assertions to get the correctly
  128. // typed values, that would throw immediately while type system
  129. // validation with validateSchema() will produce more actionable results.
  130. return typeMap[type.name];
  131. }
  132. function replaceDirective(directive) {
  133. var config = directive.toConfig();
  134. return new _directives.GraphQLDirective(_objectSpread(_objectSpread({}, config), {}, {
  135. args: (0, _mapValue.default)(config.args, extendArg)
  136. }));
  137. }
  138. function extendNamedType(type) {
  139. if ((0, _introspection.isIntrospectionType)(type) || (0, _scalars.isSpecifiedScalarType)(type)) {
  140. // Builtin types are not extended.
  141. return type;
  142. }
  143. if ((0, _definition.isScalarType)(type)) {
  144. return extendScalarType(type);
  145. }
  146. if ((0, _definition.isObjectType)(type)) {
  147. return extendObjectType(type);
  148. }
  149. if ((0, _definition.isInterfaceType)(type)) {
  150. return extendInterfaceType(type);
  151. }
  152. if ((0, _definition.isUnionType)(type)) {
  153. return extendUnionType(type);
  154. }
  155. if ((0, _definition.isEnumType)(type)) {
  156. return extendEnumType(type);
  157. } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  158. if ((0, _definition.isInputObjectType)(type)) {
  159. return extendInputObjectType(type);
  160. } // istanbul ignore next (Not reachable. All possible types have been considered)
  161. false || (0, _invariant.default)(0, 'Unexpected type: ' + (0, _inspect.default)(type));
  162. }
  163. function extendInputObjectType(type) {
  164. var _typeExtensionsMap$co;
  165. var config = type.toConfig();
  166. var extensions = (_typeExtensionsMap$co = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co !== void 0 ? _typeExtensionsMap$co : [];
  167. return new _definition.GraphQLInputObjectType(_objectSpread(_objectSpread({}, config), {}, {
  168. fields: function fields() {
  169. return _objectSpread(_objectSpread({}, (0, _mapValue.default)(config.fields, function (field) {
  170. return _objectSpread(_objectSpread({}, field), {}, {
  171. type: replaceType(field.type)
  172. });
  173. })), buildInputFieldMap(extensions));
  174. },
  175. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  176. }));
  177. }
  178. function extendEnumType(type) {
  179. var _typeExtensionsMap$ty;
  180. var config = type.toConfig();
  181. var extensions = (_typeExtensionsMap$ty = typeExtensionsMap[type.name]) !== null && _typeExtensionsMap$ty !== void 0 ? _typeExtensionsMap$ty : [];
  182. return new _definition.GraphQLEnumType(_objectSpread(_objectSpread({}, config), {}, {
  183. values: _objectSpread(_objectSpread({}, config.values), buildEnumValueMap(extensions)),
  184. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  185. }));
  186. }
  187. function extendScalarType(type) {
  188. var _typeExtensionsMap$co2;
  189. var config = type.toConfig();
  190. var extensions = (_typeExtensionsMap$co2 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co2 !== void 0 ? _typeExtensionsMap$co2 : [];
  191. var specifiedByUrl = config.specifiedByUrl;
  192. for (var _i8 = 0; _i8 < extensions.length; _i8++) {
  193. var _getSpecifiedByUrl;
  194. var extensionNode = extensions[_i8];
  195. specifiedByUrl = (_getSpecifiedByUrl = getSpecifiedByUrl(extensionNode)) !== null && _getSpecifiedByUrl !== void 0 ? _getSpecifiedByUrl : specifiedByUrl;
  196. }
  197. return new _definition.GraphQLScalarType(_objectSpread(_objectSpread({}, config), {}, {
  198. specifiedByUrl: specifiedByUrl,
  199. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  200. }));
  201. }
  202. function extendObjectType(type) {
  203. var _typeExtensionsMap$co3;
  204. var config = type.toConfig();
  205. var extensions = (_typeExtensionsMap$co3 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co3 !== void 0 ? _typeExtensionsMap$co3 : [];
  206. return new _definition.GraphQLObjectType(_objectSpread(_objectSpread({}, config), {}, {
  207. interfaces: function interfaces() {
  208. return [].concat(type.getInterfaces().map(replaceNamedType), buildInterfaces(extensions));
  209. },
  210. fields: function fields() {
  211. return _objectSpread(_objectSpread({}, (0, _mapValue.default)(config.fields, extendField)), buildFieldMap(extensions));
  212. },
  213. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  214. }));
  215. }
  216. function extendInterfaceType(type) {
  217. var _typeExtensionsMap$co4;
  218. var config = type.toConfig();
  219. var extensions = (_typeExtensionsMap$co4 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co4 !== void 0 ? _typeExtensionsMap$co4 : [];
  220. return new _definition.GraphQLInterfaceType(_objectSpread(_objectSpread({}, config), {}, {
  221. interfaces: function interfaces() {
  222. return [].concat(type.getInterfaces().map(replaceNamedType), buildInterfaces(extensions));
  223. },
  224. fields: function fields() {
  225. return _objectSpread(_objectSpread({}, (0, _mapValue.default)(config.fields, extendField)), buildFieldMap(extensions));
  226. },
  227. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  228. }));
  229. }
  230. function extendUnionType(type) {
  231. var _typeExtensionsMap$co5;
  232. var config = type.toConfig();
  233. var extensions = (_typeExtensionsMap$co5 = typeExtensionsMap[config.name]) !== null && _typeExtensionsMap$co5 !== void 0 ? _typeExtensionsMap$co5 : [];
  234. return new _definition.GraphQLUnionType(_objectSpread(_objectSpread({}, config), {}, {
  235. types: function types() {
  236. return [].concat(type.getTypes().map(replaceNamedType), buildUnionTypes(extensions));
  237. },
  238. extensionASTNodes: config.extensionASTNodes.concat(extensions)
  239. }));
  240. }
  241. function extendField(field) {
  242. return _objectSpread(_objectSpread({}, field), {}, {
  243. type: replaceType(field.type),
  244. args: (0, _mapValue.default)(field.args, extendArg)
  245. });
  246. }
  247. function extendArg(arg) {
  248. return _objectSpread(_objectSpread({}, arg), {}, {
  249. type: replaceType(arg.type)
  250. });
  251. }
  252. function getOperationTypes(nodes) {
  253. var opTypes = {};
  254. for (var _i10 = 0; _i10 < nodes.length; _i10++) {
  255. var _node$operationTypes;
  256. var node = nodes[_i10];
  257. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  258. var operationTypesNodes = (_node$operationTypes = node.operationTypes) !== null && _node$operationTypes !== void 0 ? _node$operationTypes : [];
  259. for (var _i12 = 0; _i12 < operationTypesNodes.length; _i12++) {
  260. var operationType = operationTypesNodes[_i12];
  261. opTypes[operationType.operation] = getNamedType(operationType.type);
  262. }
  263. } // Note: While this could make early assertions to get the correctly
  264. // typed values below, that would throw immediately while type system
  265. // validation with validateSchema() will produce more actionable results.
  266. return opTypes;
  267. }
  268. function getNamedType(node) {
  269. var _stdTypeMap$name2;
  270. var name = node.name.value;
  271. var type = (_stdTypeMap$name2 = stdTypeMap[name]) !== null && _stdTypeMap$name2 !== void 0 ? _stdTypeMap$name2 : typeMap[name];
  272. if (type === undefined) {
  273. throw new Error("Unknown type: \"".concat(name, "\"."));
  274. }
  275. return type;
  276. }
  277. function getWrappedType(node) {
  278. if (node.kind === _kinds.Kind.LIST_TYPE) {
  279. return new _definition.GraphQLList(getWrappedType(node.type));
  280. }
  281. if (node.kind === _kinds.Kind.NON_NULL_TYPE) {
  282. return new _definition.GraphQLNonNull(getWrappedType(node.type));
  283. }
  284. return getNamedType(node);
  285. }
  286. function buildDirective(node) {
  287. var locations = node.locations.map(function (_ref) {
  288. var value = _ref.value;
  289. return value;
  290. });
  291. return new _directives.GraphQLDirective({
  292. name: node.name.value,
  293. description: getDescription(node, options),
  294. locations: locations,
  295. isRepeatable: node.repeatable,
  296. args: buildArgumentMap(node.arguments),
  297. astNode: node
  298. });
  299. }
  300. function buildFieldMap(nodes) {
  301. var fieldConfigMap = Object.create(null);
  302. for (var _i14 = 0; _i14 < nodes.length; _i14++) {
  303. var _node$fields;
  304. var node = nodes[_i14];
  305. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  306. var nodeFields = (_node$fields = node.fields) !== null && _node$fields !== void 0 ? _node$fields : [];
  307. for (var _i16 = 0; _i16 < nodeFields.length; _i16++) {
  308. var field = nodeFields[_i16];
  309. fieldConfigMap[field.name.value] = {
  310. // Note: While this could make assertions to get the correctly typed
  311. // value, that would throw immediately while type system validation
  312. // with validateSchema() will produce more actionable results.
  313. type: getWrappedType(field.type),
  314. description: getDescription(field, options),
  315. args: buildArgumentMap(field.arguments),
  316. deprecationReason: getDeprecationReason(field),
  317. astNode: field
  318. };
  319. }
  320. }
  321. return fieldConfigMap;
  322. }
  323. function buildArgumentMap(args) {
  324. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  325. var argsNodes = args !== null && args !== void 0 ? args : [];
  326. var argConfigMap = Object.create(null);
  327. for (var _i18 = 0; _i18 < argsNodes.length; _i18++) {
  328. var arg = argsNodes[_i18];
  329. // Note: While this could make assertions to get the correctly typed
  330. // value, that would throw immediately while type system validation
  331. // with validateSchema() will produce more actionable results.
  332. var type = getWrappedType(arg.type);
  333. argConfigMap[arg.name.value] = {
  334. type: type,
  335. description: getDescription(arg, options),
  336. defaultValue: (0, _valueFromAST.valueFromAST)(arg.defaultValue, type),
  337. astNode: arg
  338. };
  339. }
  340. return argConfigMap;
  341. }
  342. function buildInputFieldMap(nodes) {
  343. var inputFieldMap = Object.create(null);
  344. for (var _i20 = 0; _i20 < nodes.length; _i20++) {
  345. var _node$fields2;
  346. var node = nodes[_i20];
  347. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  348. var fieldsNodes = (_node$fields2 = node.fields) !== null && _node$fields2 !== void 0 ? _node$fields2 : [];
  349. for (var _i22 = 0; _i22 < fieldsNodes.length; _i22++) {
  350. var field = fieldsNodes[_i22];
  351. // Note: While this could make assertions to get the correctly typed
  352. // value, that would throw immediately while type system validation
  353. // with validateSchema() will produce more actionable results.
  354. var type = getWrappedType(field.type);
  355. inputFieldMap[field.name.value] = {
  356. type: type,
  357. description: getDescription(field, options),
  358. defaultValue: (0, _valueFromAST.valueFromAST)(field.defaultValue, type),
  359. astNode: field
  360. };
  361. }
  362. }
  363. return inputFieldMap;
  364. }
  365. function buildEnumValueMap(nodes) {
  366. var enumValueMap = Object.create(null);
  367. for (var _i24 = 0; _i24 < nodes.length; _i24++) {
  368. var _node$values;
  369. var node = nodes[_i24];
  370. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  371. var valuesNodes = (_node$values = node.values) !== null && _node$values !== void 0 ? _node$values : [];
  372. for (var _i26 = 0; _i26 < valuesNodes.length; _i26++) {
  373. var value = valuesNodes[_i26];
  374. enumValueMap[value.name.value] = {
  375. description: getDescription(value, options),
  376. deprecationReason: getDeprecationReason(value),
  377. astNode: value
  378. };
  379. }
  380. }
  381. return enumValueMap;
  382. }
  383. function buildInterfaces(nodes) {
  384. var interfaces = [];
  385. for (var _i28 = 0; _i28 < nodes.length; _i28++) {
  386. var _node$interfaces;
  387. var node = nodes[_i28];
  388. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  389. var interfacesNodes = (_node$interfaces = node.interfaces) !== null && _node$interfaces !== void 0 ? _node$interfaces : [];
  390. for (var _i30 = 0; _i30 < interfacesNodes.length; _i30++) {
  391. var type = interfacesNodes[_i30];
  392. // Note: While this could make assertions to get the correctly typed
  393. // values below, that would throw immediately while type system
  394. // validation with validateSchema() will produce more actionable
  395. // results.
  396. interfaces.push(getNamedType(type));
  397. }
  398. }
  399. return interfaces;
  400. }
  401. function buildUnionTypes(nodes) {
  402. var types = [];
  403. for (var _i32 = 0; _i32 < nodes.length; _i32++) {
  404. var _node$types;
  405. var node = nodes[_i32];
  406. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  407. var typeNodes = (_node$types = node.types) !== null && _node$types !== void 0 ? _node$types : [];
  408. for (var _i34 = 0; _i34 < typeNodes.length; _i34++) {
  409. var type = typeNodes[_i34];
  410. // Note: While this could make assertions to get the correctly typed
  411. // values below, that would throw immediately while type system
  412. // validation with validateSchema() will produce more actionable
  413. // results.
  414. types.push(getNamedType(type));
  415. }
  416. }
  417. return types;
  418. }
  419. function buildType(astNode) {
  420. var _typeExtensionsMap$na;
  421. var name = astNode.name.value;
  422. var description = getDescription(astNode, options);
  423. var extensionNodes = (_typeExtensionsMap$na = typeExtensionsMap[name]) !== null && _typeExtensionsMap$na !== void 0 ? _typeExtensionsMap$na : [];
  424. switch (astNode.kind) {
  425. case _kinds.Kind.OBJECT_TYPE_DEFINITION:
  426. {
  427. var extensionASTNodes = extensionNodes;
  428. var allNodes = [astNode].concat(extensionASTNodes);
  429. return new _definition.GraphQLObjectType({
  430. name: name,
  431. description: description,
  432. interfaces: function interfaces() {
  433. return buildInterfaces(allNodes);
  434. },
  435. fields: function fields() {
  436. return buildFieldMap(allNodes);
  437. },
  438. astNode: astNode,
  439. extensionASTNodes: extensionASTNodes
  440. });
  441. }
  442. case _kinds.Kind.INTERFACE_TYPE_DEFINITION:
  443. {
  444. var _extensionASTNodes = extensionNodes;
  445. var _allNodes = [astNode].concat(_extensionASTNodes);
  446. return new _definition.GraphQLInterfaceType({
  447. name: name,
  448. description: description,
  449. interfaces: function interfaces() {
  450. return buildInterfaces(_allNodes);
  451. },
  452. fields: function fields() {
  453. return buildFieldMap(_allNodes);
  454. },
  455. astNode: astNode,
  456. extensionASTNodes: _extensionASTNodes
  457. });
  458. }
  459. case _kinds.Kind.ENUM_TYPE_DEFINITION:
  460. {
  461. var _extensionASTNodes2 = extensionNodes;
  462. var _allNodes2 = [astNode].concat(_extensionASTNodes2);
  463. return new _definition.GraphQLEnumType({
  464. name: name,
  465. description: description,
  466. values: buildEnumValueMap(_allNodes2),
  467. astNode: astNode,
  468. extensionASTNodes: _extensionASTNodes2
  469. });
  470. }
  471. case _kinds.Kind.UNION_TYPE_DEFINITION:
  472. {
  473. var _extensionASTNodes3 = extensionNodes;
  474. var _allNodes3 = [astNode].concat(_extensionASTNodes3);
  475. return new _definition.GraphQLUnionType({
  476. name: name,
  477. description: description,
  478. types: function types() {
  479. return buildUnionTypes(_allNodes3);
  480. },
  481. astNode: astNode,
  482. extensionASTNodes: _extensionASTNodes3
  483. });
  484. }
  485. case _kinds.Kind.SCALAR_TYPE_DEFINITION:
  486. {
  487. var _extensionASTNodes4 = extensionNodes;
  488. return new _definition.GraphQLScalarType({
  489. name: name,
  490. description: description,
  491. specifiedByUrl: getSpecifiedByUrl(astNode),
  492. astNode: astNode,
  493. extensionASTNodes: _extensionASTNodes4
  494. });
  495. }
  496. case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION:
  497. {
  498. var _extensionASTNodes5 = extensionNodes;
  499. var _allNodes4 = [astNode].concat(_extensionASTNodes5);
  500. return new _definition.GraphQLInputObjectType({
  501. name: name,
  502. description: description,
  503. fields: function fields() {
  504. return buildInputFieldMap(_allNodes4);
  505. },
  506. astNode: astNode,
  507. extensionASTNodes: _extensionASTNodes5
  508. });
  509. }
  510. } // istanbul ignore next (Not reachable. All possible type definition nodes have been considered)
  511. false || (0, _invariant.default)(0, 'Unexpected type definition node: ' + (0, _inspect.default)(astNode));
  512. }
  513. }
  514. var stdTypeMap = (0, _keyMap.default)(_scalars.specifiedScalarTypes.concat(_introspection.introspectionTypes), function (type) {
  515. return type.name;
  516. });
  517. /**
  518. * Given a field or enum value node, returns the string value for the
  519. * deprecation reason.
  520. */
  521. function getDeprecationReason(node) {
  522. var deprecated = (0, _values.getDirectiveValues)(_directives.GraphQLDeprecatedDirective, node);
  523. return deprecated === null || deprecated === void 0 ? void 0 : deprecated.reason;
  524. }
  525. /**
  526. * Given a scalar node, returns the string value for the specifiedByUrl.
  527. */
  528. function getSpecifiedByUrl(node) {
  529. var specifiedBy = (0, _values.getDirectiveValues)(_directives.GraphQLSpecifiedByDirective, node);
  530. return specifiedBy === null || specifiedBy === void 0 ? void 0 : specifiedBy.url;
  531. }
  532. /**
  533. * Given an ast node, returns its string description.
  534. * @deprecated: provided to ease adoption and will be removed in v16.
  535. *
  536. * Accepts options as a second argument:
  537. *
  538. * - commentDescriptions:
  539. * Provide true to use preceding comments as the description.
  540. *
  541. */
  542. function getDescription(node, options) {
  543. if (node.description) {
  544. return node.description.value;
  545. }
  546. if ((options === null || options === void 0 ? void 0 : options.commentDescriptions) === true) {
  547. var rawValue = getLeadingCommentBlock(node);
  548. if (rawValue !== undefined) {
  549. return (0, _blockString.dedentBlockStringValue)('\n' + rawValue);
  550. }
  551. }
  552. }
  553. function getLeadingCommentBlock(node) {
  554. var loc = node.loc;
  555. if (!loc) {
  556. return;
  557. }
  558. var comments = [];
  559. var token = loc.startToken.prev;
  560. while (token != null && token.kind === _tokenKind.TokenKind.COMMENT && token.next && token.prev && token.line + 1 === token.next.line && token.line !== token.prev.line) {
  561. var value = String(token.value);
  562. comments.push(value);
  563. token = token.prev;
  564. }
  565. return comments.length > 0 ? comments.reverse().join('\n') : undefined;
  566. }