extendSchema.mjs 25 KB

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