buildClientSchema.js 12 KB

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