extendSchema.mjs 26 KB

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