UNPKG

6.36 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || Object.assign || function(t) {
3 for (var s, i = 1, n = arguments.length; i < n; i++) {
4 s = arguments[i];
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6 t[p] = s[p];
7 }
8 return t;
9};
10var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
11 return new (P || (P = Promise))(function (resolve, reject) {
12 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
13 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
14 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
15 step((generator = generator.apply(thisArg, _arguments || [])).next());
16 });
17};
18var __generator = (this && this.__generator) || function (thisArg, body) {
19 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
20 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
21 function verb(n) { return function (v) { return step([n, v]); }; }
22 function step(op) {
23 if (f) throw new TypeError("Generator is already executing.");
24 while (_) try {
25 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
26 if (y = 0, t) op = [0, t.value];
27 switch (op[0]) {
28 case 0: case 1: t = op; break;
29 case 4: _.label++; return { value: op[1], done: false };
30 case 5: _.label++; y = op[1]; op = [0]; continue;
31 case 7: op = _.ops.pop(); _.trys.pop(); continue;
32 default:
33 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
34 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
35 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
36 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
37 if (t[2]) _.ops.pop();
38 _.trys.pop(); continue;
39 }
40 op = body.call(thisArg, _);
41 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
42 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
43 }
44};
45Object.defineProperty(exports, "__esModule", { value: true });
46require("cross-fetch/polyfill");
47var DataLoader = require("dataloader");
48var ClientError_1 = require("./ClientError");
49var BatchedGraphQLClient = /** @class */ (function () {
50 function BatchedGraphQLClient(uri, options) {
51 var _this = this;
52 this.load = function (keys) { return __awaiter(_this, void 0, void 0, function () {
53 var requests, body, response, results, errorIndex, result, errorResult;
54 return __generator(this, function (_a) {
55 switch (_a.label) {
56 case 0:
57 requests = keys.map(function (k) { return JSON.parse(k); });
58 body = JSON.stringify(requests);
59 return [4 /*yield*/, fetch(this.uri, __assign({ method: 'POST' }, this.options, { headers: Object.assign({ 'Content-Type': 'application/json' }, this.options.headers), body: body }))];
60 case 1:
61 response = _a.sent();
62 return [4 /*yield*/, getResults(response)
63 // if it is not an array, there must be an error
64 ];
65 case 2:
66 results = _a.sent();
67 // if it is not an array, there must be an error
68 if (!Array.isArray(results)) {
69 throw new ClientError_1.ClientError(__assign({}, results, { status: response.status }));
70 }
71 // check if there was an error in one of the responses
72 if (!response.ok ||
73 results.some(function (r) { return r.errors !== undefined || r.data === undefined; })) {
74 errorIndex = results.findIndex(function (r) { return r.errors !== undefined || r.data === undefined; });
75 result = results[errorIndex];
76 errorResult = typeof result === 'string' ? { errors: [{ message: result }] } : result;
77 throw new ClientError_1.ClientError(__assign({}, errorResult, { status: response.status }));
78 }
79 return [2 /*return*/, results.map(function (r) { return r.data; })];
80 }
81 });
82 }); };
83 this.uri = uri;
84 var cache = options && typeof options.cacheResults !== 'undefined'
85 ? options.cacheResults
86 : false;
87 if (options && typeof options.cacheResults !== 'undefined') {
88 delete options.cacheResults;
89 }
90 this.options = options || {};
91 this.dataloader = new DataLoader(this.load, { cache: cache });
92 }
93 BatchedGraphQLClient.prototype.request = function (query, variables, operationName) {
94 return __awaiter(this, void 0, void 0, function () {
95 var body;
96 return __generator(this, function (_a) {
97 body = JSON.stringify({
98 query: query,
99 variables: variables ? variables : undefined,
100 operationName: operationName ? operationName : undefined,
101 });
102 return [2 /*return*/, this.dataloader.load(body)];
103 });
104 });
105 };
106 return BatchedGraphQLClient;
107}());
108exports.BatchedGraphQLClient = BatchedGraphQLClient;
109function getResults(response) {
110 var contentType = response.headers.get('Content-Type');
111 if (contentType && contentType.startsWith('application/json')) {
112 return response.json();
113 }
114 else {
115 return response.text();
116 }
117}
118//# sourceMappingURL=BatchedGraphQLClient.js.map
\No newline at end of file