makeRemoteExecutableSchema.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [op[0] & 2, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. var __spreadArrays = (this && this.__spreadArrays) || function () {
  38. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  39. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  40. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  41. r[k] = a[j];
  42. return r;
  43. };
  44. Object.defineProperty(exports, "__esModule", { value: true });
  45. var graphql_1 = require("graphql");
  46. var linkToFetcher_1 = require("./linkToFetcher");
  47. var isEmptyObject_1 = require("../isEmptyObject");
  48. var makeExecutableSchema_1 = require("../makeExecutableSchema");
  49. var schemaRecreation_1 = require("./schemaRecreation");
  50. var resolveFromParentTypename_1 = require("./resolveFromParentTypename");
  51. var defaultMergedResolver_1 = require("./defaultMergedResolver");
  52. var errors_1 = require("./errors");
  53. var observableToAsyncIterable_1 = require("./observableToAsyncIterable");
  54. function makeRemoteExecutableSchema(_a) {
  55. var _b;
  56. var schema = _a.schema, link = _a.link, fetcher = _a.fetcher, _c = _a.createResolver, customCreateResolver = _c === void 0 ? createResolver : _c, buildSchemaOptions = _a.buildSchemaOptions, _d = _a.printSchemaOptions, printSchemaOptions = _d === void 0 ? { commentDescriptions: true } : _d;
  57. if (!fetcher && link) {
  58. fetcher = linkToFetcher_1.default(link);
  59. }
  60. var typeDefs;
  61. if (typeof schema === 'string') {
  62. typeDefs = schema;
  63. schema = graphql_1.buildSchema(typeDefs, buildSchemaOptions);
  64. }
  65. else {
  66. typeDefs = graphql_1.printSchema(schema, printSchemaOptions);
  67. }
  68. // prepare query resolvers
  69. var queryResolvers = {};
  70. var queryType = schema.getQueryType();
  71. var queries = queryType.getFields();
  72. Object.keys(queries).forEach(function (key) {
  73. queryResolvers[key] = customCreateResolver(fetcher);
  74. });
  75. // prepare mutation resolvers
  76. var mutationResolvers = {};
  77. var mutationType = schema.getMutationType();
  78. if (mutationType) {
  79. var mutations = mutationType.getFields();
  80. Object.keys(mutations).forEach(function (key) {
  81. mutationResolvers[key] = customCreateResolver(fetcher);
  82. });
  83. }
  84. // prepare subscription resolvers
  85. var subscriptionResolvers = {};
  86. var subscriptionType = schema.getSubscriptionType();
  87. if (subscriptionType) {
  88. var subscriptions = subscriptionType.getFields();
  89. Object.keys(subscriptions).forEach(function (key) {
  90. subscriptionResolvers[key] = {
  91. subscribe: createSubscriptionResolver(key, link)
  92. };
  93. });
  94. }
  95. // merge resolvers into resolver map
  96. var resolvers = (_b = {}, _b[queryType.name] = queryResolvers, _b);
  97. if (!isEmptyObject_1.default(mutationResolvers)) {
  98. resolvers[mutationType.name] = mutationResolvers;
  99. }
  100. if (!isEmptyObject_1.default(subscriptionResolvers)) {
  101. resolvers[subscriptionType.name] = subscriptionResolvers;
  102. }
  103. // add missing abstract resolvers (scalar, unions, interfaces)
  104. var typeMap = schema.getTypeMap();
  105. var types = Object.keys(typeMap).map(function (name) { return typeMap[name]; });
  106. var _loop_1 = function (type) {
  107. if (type instanceof graphql_1.GraphQLInterfaceType || type instanceof graphql_1.GraphQLUnionType) {
  108. resolvers[type.name] = {
  109. __resolveType: function (parent, context, info) {
  110. return resolveFromParentTypename_1.default(parent, info.schema);
  111. }
  112. };
  113. }
  114. else if (type instanceof graphql_1.GraphQLScalarType) {
  115. if (!(type === graphql_1.GraphQLID ||
  116. type === graphql_1.GraphQLString ||
  117. type === graphql_1.GraphQLFloat ||
  118. type === graphql_1.GraphQLBoolean ||
  119. type === graphql_1.GraphQLInt)) {
  120. resolvers[type.name] = schemaRecreation_1.recreateType(type, function (name) { return null; }, false);
  121. }
  122. }
  123. else if (type instanceof graphql_1.GraphQLObjectType &&
  124. type.name.slice(0, 2) !== '__' &&
  125. type !== queryType &&
  126. type !== mutationType &&
  127. type !== subscriptionType) {
  128. var resolver_1 = {};
  129. Object.keys(type.getFields()).forEach(function (field) {
  130. resolver_1[field] = defaultMergedResolver_1.default;
  131. });
  132. resolvers[type.name] = resolver_1;
  133. }
  134. };
  135. for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
  136. var type = types_1[_i];
  137. _loop_1(type);
  138. }
  139. return makeExecutableSchema_1.makeExecutableSchema({
  140. typeDefs: typeDefs,
  141. resolvers: resolvers
  142. });
  143. }
  144. exports.default = makeRemoteExecutableSchema;
  145. function createResolver(fetcher) {
  146. var _this = this;
  147. return function (root, args, context, info) { return __awaiter(_this, void 0, void 0, function () {
  148. var fragments, document, result;
  149. return __generator(this, function (_a) {
  150. switch (_a.label) {
  151. case 0:
  152. fragments = Object.keys(info.fragments).map(function (fragment) { return info.fragments[fragment]; });
  153. document = {
  154. kind: graphql_1.Kind.DOCUMENT,
  155. definitions: __spreadArrays([info.operation], fragments)
  156. };
  157. return [4 /*yield*/, fetcher({
  158. query: document,
  159. variables: info.variableValues,
  160. context: { graphqlContext: context }
  161. })];
  162. case 1:
  163. result = _a.sent();
  164. return [2 /*return*/, errors_1.checkResultAndHandleErrors(result, info)];
  165. }
  166. });
  167. }); };
  168. }
  169. exports.createResolver = createResolver;
  170. function createSubscriptionResolver(name, link) {
  171. return function (root, args, context, info) {
  172. var fragments = Object.keys(info.fragments).map(function (fragment) { return info.fragments[fragment]; });
  173. var document = {
  174. kind: graphql_1.Kind.DOCUMENT,
  175. definitions: __spreadArrays([info.operation], fragments)
  176. };
  177. var operation = {
  178. query: document,
  179. variables: info.variableValues,
  180. context: { graphqlContext: context }
  181. };
  182. var observable = linkToFetcher_1.execute(link, operation);
  183. return observableToAsyncIterable_1.observableToAsyncIterable(observable);
  184. };
  185. }
  186. //# sourceMappingURL=makeRemoteExecutableSchema.js.map