buildClientSchema.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.buildClientSchema = buildClientSchema;
  6. var _devAssert = require('../jsutils/devAssert.js');
  7. var _inspect = require('../jsutils/inspect.js');
  8. var _isObjectLike = require('../jsutils/isObjectLike.js');
  9. var _keyValMap = require('../jsutils/keyValMap.js');
  10. var _parser = require('../language/parser.js');
  11. var _definition = require('../type/definition.js');
  12. var _directives = require('../type/directives.js');
  13. var _introspection = require('../type/introspection.js');
  14. var _scalars = require('../type/scalars.js');
  15. var _schema = require('../type/schema.js');
  16. var _valueFromAST = require('./valueFromAST.js');
  17. /**
  18. * Build a GraphQLSchema for use by client tools.
  19. *
  20. * Given the result of a client running the introspection query, creates and
  21. * returns a GraphQLSchema instance which can be then used with all graphql-js
  22. * tools, but cannot be used to execute a query, as introspection does not
  23. * represent the "resolver", "parse" or "serialize" functions or any other
  24. * server-internal mechanisms.
  25. *
  26. * This function expects a complete introspection result. Don't forget to check
  27. * the "errors" field of a server response before calling this function.
  28. */
  29. function buildClientSchema(introspection, options) {
  30. ((0, _isObjectLike.isObjectLike)(introspection) &&
  31. (0, _isObjectLike.isObjectLike)(introspection.__schema)) ||
  32. (0, _devAssert.devAssert)(
  33. false,
  34. `Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ${(0,
  35. _inspect.inspect)(introspection)}.`,
  36. ); // Get the schema from the introspection result.
  37. const schemaIntrospection = introspection.__schema; // Iterate through all types, getting the type definition for each.
  38. const typeMap = (0, _keyValMap.keyValMap)(
  39. schemaIntrospection.types,
  40. (typeIntrospection) => typeIntrospection.name,
  41. (typeIntrospection) => buildType(typeIntrospection),
  42. ); // Include standard types only if they are used.
  43. for (const stdType of [
  44. ..._scalars.specifiedScalarTypes,
  45. ..._introspection.introspectionTypes,
  46. ]) {
  47. if (typeMap[stdType.name]) {
  48. typeMap[stdType.name] = stdType;
  49. }
  50. } // Get the root Query, Mutation, and Subscription types.
  51. const queryType = schemaIntrospection.queryType
  52. ? getObjectType(schemaIntrospection.queryType)
  53. : null;
  54. const mutationType = schemaIntrospection.mutationType
  55. ? getObjectType(schemaIntrospection.mutationType)
  56. : null;
  57. const subscriptionType = schemaIntrospection.subscriptionType
  58. ? getObjectType(schemaIntrospection.subscriptionType)
  59. : null; // Get the directives supported by Introspection, assuming empty-set if
  60. // directives were not queried for.
  61. const directives = schemaIntrospection.directives
  62. ? schemaIntrospection.directives.map(buildDirective)
  63. : []; // Then produce and return a Schema with these types.
  64. return new _schema.GraphQLSchema({
  65. description: schemaIntrospection.description,
  66. query: queryType,
  67. mutation: mutationType,
  68. subscription: subscriptionType,
  69. types: Object.values(typeMap),
  70. directives,
  71. assumeValid:
  72. options === null || options === void 0 ? void 0 : options.assumeValid,
  73. }); // Given a type reference in introspection, return the GraphQLType instance.
  74. // preferring cached instances before building new instances.
  75. function getType(typeRef) {
  76. if (typeRef.kind === _introspection.TypeKind.LIST) {
  77. const itemRef = typeRef.ofType;
  78. if (!itemRef) {
  79. throw new Error('Decorated type deeper than introspection query.');
  80. }
  81. return new _definition.GraphQLList(getType(itemRef));
  82. }
  83. if (typeRef.kind === _introspection.TypeKind.NON_NULL) {
  84. const nullableRef = typeRef.ofType;
  85. if (!nullableRef) {
  86. throw new Error('Decorated type deeper than introspection query.');
  87. }
  88. const nullableType = getType(nullableRef);
  89. return new _definition.GraphQLNonNull(
  90. (0, _definition.assertNullableType)(nullableType),
  91. );
  92. }
  93. return getNamedType(typeRef);
  94. }
  95. function getNamedType(typeRef) {
  96. const typeName = typeRef.name;
  97. if (!typeName) {
  98. throw new Error(
  99. `Unknown type reference: ${(0, _inspect.inspect)(typeRef)}.`,
  100. );
  101. }
  102. const type = typeMap[typeName];
  103. if (!type) {
  104. throw new Error(
  105. `Invalid or incomplete schema, unknown type: ${typeName}. Ensure that a full introspection query is used in order to build a client schema.`,
  106. );
  107. }
  108. return type;
  109. }
  110. function getObjectType(typeRef) {
  111. return (0, _definition.assertObjectType)(getNamedType(typeRef));
  112. }
  113. function getInterfaceType(typeRef) {
  114. return (0, _definition.assertInterfaceType)(getNamedType(typeRef));
  115. } // Given a type's introspection result, construct the correct
  116. // GraphQLType instance.
  117. function buildType(type) {
  118. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  119. if (type != null && type.name != null && type.kind != null) {
  120. // FIXME: Properly type IntrospectionType, it's a breaking change so fix in v17
  121. // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
  122. switch (type.kind) {
  123. case _introspection.TypeKind.SCALAR:
  124. return buildScalarDef(type);
  125. case _introspection.TypeKind.OBJECT:
  126. return buildObjectDef(type);
  127. case _introspection.TypeKind.INTERFACE:
  128. return buildInterfaceDef(type);
  129. case _introspection.TypeKind.UNION:
  130. return buildUnionDef(type);
  131. case _introspection.TypeKind.ENUM:
  132. return buildEnumDef(type);
  133. case _introspection.TypeKind.INPUT_OBJECT:
  134. return buildInputObjectDef(type);
  135. }
  136. }
  137. const typeStr = (0, _inspect.inspect)(type);
  138. throw new Error(
  139. `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${typeStr}.`,
  140. );
  141. }
  142. function buildScalarDef(scalarIntrospection) {
  143. return new _definition.GraphQLScalarType({
  144. name: scalarIntrospection.name,
  145. description: scalarIntrospection.description,
  146. specifiedByURL: scalarIntrospection.specifiedByURL,
  147. });
  148. }
  149. function buildImplementationsList(implementingIntrospection) {
  150. // TODO: Temporary workaround until GraphQL ecosystem will fully support
  151. // 'interfaces' on interface types.
  152. if (
  153. implementingIntrospection.interfaces === null &&
  154. implementingIntrospection.kind === _introspection.TypeKind.INTERFACE
  155. ) {
  156. return [];
  157. }
  158. if (!implementingIntrospection.interfaces) {
  159. const implementingIntrospectionStr = (0, _inspect.inspect)(
  160. implementingIntrospection,
  161. );
  162. throw new Error(
  163. `Introspection result missing interfaces: ${implementingIntrospectionStr}.`,
  164. );
  165. }
  166. return implementingIntrospection.interfaces.map(getInterfaceType);
  167. }
  168. function buildObjectDef(objectIntrospection) {
  169. return new _definition.GraphQLObjectType({
  170. name: objectIntrospection.name,
  171. description: objectIntrospection.description,
  172. interfaces: () => buildImplementationsList(objectIntrospection),
  173. fields: () => buildFieldDefMap(objectIntrospection),
  174. });
  175. }
  176. function buildInterfaceDef(interfaceIntrospection) {
  177. return new _definition.GraphQLInterfaceType({
  178. name: interfaceIntrospection.name,
  179. description: interfaceIntrospection.description,
  180. interfaces: () => buildImplementationsList(interfaceIntrospection),
  181. fields: () => buildFieldDefMap(interfaceIntrospection),
  182. });
  183. }
  184. function buildUnionDef(unionIntrospection) {
  185. if (!unionIntrospection.possibleTypes) {
  186. const unionIntrospectionStr = (0, _inspect.inspect)(unionIntrospection);
  187. throw new Error(
  188. `Introspection result missing possibleTypes: ${unionIntrospectionStr}.`,
  189. );
  190. }
  191. return new _definition.GraphQLUnionType({
  192. name: unionIntrospection.name,
  193. description: unionIntrospection.description,
  194. types: () => unionIntrospection.possibleTypes.map(getObjectType),
  195. });
  196. }
  197. function buildEnumDef(enumIntrospection) {
  198. if (!enumIntrospection.enumValues) {
  199. const enumIntrospectionStr = (0, _inspect.inspect)(enumIntrospection);
  200. throw new Error(
  201. `Introspection result missing enumValues: ${enumIntrospectionStr}.`,
  202. );
  203. }
  204. return new _definition.GraphQLEnumType({
  205. name: enumIntrospection.name,
  206. description: enumIntrospection.description,
  207. values: (0, _keyValMap.keyValMap)(
  208. enumIntrospection.enumValues,
  209. (valueIntrospection) => valueIntrospection.name,
  210. (valueIntrospection) => ({
  211. description: valueIntrospection.description,
  212. deprecationReason: valueIntrospection.deprecationReason,
  213. }),
  214. ),
  215. });
  216. }
  217. function buildInputObjectDef(inputObjectIntrospection) {
  218. if (!inputObjectIntrospection.inputFields) {
  219. const inputObjectIntrospectionStr = (0, _inspect.inspect)(
  220. inputObjectIntrospection,
  221. );
  222. throw new Error(
  223. `Introspection result missing inputFields: ${inputObjectIntrospectionStr}.`,
  224. );
  225. }
  226. return new _definition.GraphQLInputObjectType({
  227. name: inputObjectIntrospection.name,
  228. description: inputObjectIntrospection.description,
  229. fields: () => buildInputValueDefMap(inputObjectIntrospection.inputFields),
  230. });
  231. }
  232. function buildFieldDefMap(typeIntrospection) {
  233. if (!typeIntrospection.fields) {
  234. throw new Error(
  235. `Introspection result missing fields: ${(0, _inspect.inspect)(
  236. typeIntrospection,
  237. )}.`,
  238. );
  239. }
  240. return (0, _keyValMap.keyValMap)(
  241. typeIntrospection.fields,
  242. (fieldIntrospection) => fieldIntrospection.name,
  243. buildField,
  244. );
  245. }
  246. function buildField(fieldIntrospection) {
  247. const type = getType(fieldIntrospection.type);
  248. if (!(0, _definition.isOutputType)(type)) {
  249. const typeStr = (0, _inspect.inspect)(type);
  250. throw new Error(
  251. `Introspection must provide output type for fields, but received: ${typeStr}.`,
  252. );
  253. }
  254. if (!fieldIntrospection.args) {
  255. const fieldIntrospectionStr = (0, _inspect.inspect)(fieldIntrospection);
  256. throw new Error(
  257. `Introspection result missing field args: ${fieldIntrospectionStr}.`,
  258. );
  259. }
  260. return {
  261. description: fieldIntrospection.description,
  262. deprecationReason: fieldIntrospection.deprecationReason,
  263. type,
  264. args: buildInputValueDefMap(fieldIntrospection.args),
  265. };
  266. }
  267. function buildInputValueDefMap(inputValueIntrospections) {
  268. return (0, _keyValMap.keyValMap)(
  269. inputValueIntrospections,
  270. (inputValue) => inputValue.name,
  271. buildInputValue,
  272. );
  273. }
  274. function buildInputValue(inputValueIntrospection) {
  275. const type = getType(inputValueIntrospection.type);
  276. if (!(0, _definition.isInputType)(type)) {
  277. const typeStr = (0, _inspect.inspect)(type);
  278. throw new Error(
  279. `Introspection must provide input type for arguments, but received: ${typeStr}.`,
  280. );
  281. }
  282. const defaultValue =
  283. inputValueIntrospection.defaultValue != null
  284. ? (0, _valueFromAST.valueFromAST)(
  285. (0, _parser.parseValue)(inputValueIntrospection.defaultValue),
  286. type,
  287. )
  288. : undefined;
  289. return {
  290. description: inputValueIntrospection.description,
  291. type,
  292. defaultValue,
  293. deprecationReason: inputValueIntrospection.deprecationReason,
  294. };
  295. }
  296. function buildDirective(directiveIntrospection) {
  297. if (!directiveIntrospection.args) {
  298. const directiveIntrospectionStr = (0, _inspect.inspect)(
  299. directiveIntrospection,
  300. );
  301. throw new Error(
  302. `Introspection result missing directive args: ${directiveIntrospectionStr}.`,
  303. );
  304. }
  305. if (!directiveIntrospection.locations) {
  306. const directiveIntrospectionStr = (0, _inspect.inspect)(
  307. directiveIntrospection,
  308. );
  309. throw new Error(
  310. `Introspection result missing directive locations: ${directiveIntrospectionStr}.`,
  311. );
  312. }
  313. return new _directives.GraphQLDirective({
  314. name: directiveIntrospection.name,
  315. description: directiveIntrospection.description,
  316. isRepeatable: directiveIntrospection.isRepeatable,
  317. locations: directiveIntrospection.locations.slice(),
  318. args: buildInputValueDefMap(directiveIntrospection.args),
  319. });
  320. }
  321. }