relay-link-batch-http
Version:
Batch HTTP transport layer for Relay
111 lines • 5.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchHttpLink = void 0;
var tslib_1 = require("tslib");
var relay_link_1 = require("relay-link");
var relay_link_batch_1 = require("relay-link-batch");
var relay_link_http_common_1 = require("relay-link-http-common");
var relay_runtime_1 = require("relay-runtime");
var BatchHttpLink = (function (_super) {
tslib_1.__extends(BatchHttpLink, _super);
function BatchHttpLink(fetchParams) {
var _this = _super.call(this) || this;
var _a = fetchParams || {}, _b = _a.uri, uri = _b === void 0 ? '/graphql' : _b, fetcher = _a.fetch, batchInterval = _a.batchInterval, batchMax = _a.batchMax, batchKey = _a.batchKey, requestOptions = tslib_1.__rest(_a, ["uri", "fetch", "batchInterval", "batchMax", "batchKey"]);
relay_link_http_common_1.checkFetcher(fetcher);
if (!fetcher) {
fetcher = fetch;
}
var linkConfig = {
options: requestOptions.fetchOptions,
credentials: requestOptions.credentials,
headers: requestOptions.headers,
};
_this.batchInterval = batchInterval || 10;
_this.batchMax = batchMax || 10;
var batchHandler = function (operations) {
var chosenURI = relay_link_http_common_1.selectURI(operations[0], uri);
var context = operations[0].getContext();
var contextConfig = {
http: context.http,
options: context.fetchOptions,
credentials: context.credentials,
headers: tslib_1.__assign({}, context.headers),
};
var optsAndBody = operations.map(function (operation) {
return relay_link_http_common_1.selectHttpOptionsAndBody(operation, relay_link_http_common_1.fallbackHttpConfig, linkConfig, contextConfig);
});
var loadedBody = optsAndBody.map(function (_a) {
var body = _a.body;
return body;
});
var options = optsAndBody[0].options;
if (options.method === 'GET') {
return relay_link_1.fromError(new Error('relay-link-batch-http does not support GET requests'));
}
try {
;
options.body = relay_link_http_common_1.serializeFetchParameter(loadedBody, 'Payload');
}
catch (parseError) {
return relay_link_1.fromError(parseError);
}
var controller;
if (!options.signal) {
var _a = relay_link_http_common_1.createSignalIfSupported(), _controller = _a.controller, signal = _a.signal;
controller = _controller;
if (controller)
options.signal = signal;
}
return relay_runtime_1.Observable.create(function (sink) {
fetcher(chosenURI, options)
.then(function (response) {
operations.forEach(function (operation) { return operation.setContext({ response: response }); });
return response;
})
.then(relay_link_http_common_1.parseAndCheckHttpResponse(operations, operations[0].getContext().bodyParser || relay_link_http_common_1.readResponseBody))
.then(function (result) {
sink.next(result);
sink.complete();
return result;
})
.catch(function (err) {
if (err.name === 'AbortError')
return;
if (err.result && err.result.errors && err.result.data) {
sink.next(err.result);
}
sink.error(err);
});
return function () {
if (controller)
controller.abort();
};
});
};
batchKey =
batchKey ||
(function (operation) {
var context = operation.getContext();
var contextConfig = {
http: context.http,
options: context.fetchOptions,
credentials: context.credentials,
headers: context.headers,
};
return relay_link_http_common_1.selectURI(operation, uri) + JSON.stringify(contextConfig);
});
_this.batcher = new relay_link_batch_1.BatchLink({
batchInterval: _this.batchInterval,
batchMax: _this.batchMax,
batchKey: batchKey,
batchHandler: batchHandler,
});
return _this;
}
BatchHttpLink.prototype.request = function (operation) {
return this.batcher.request(operation);
};
return BatchHttpLink;
}(relay_link_1.RelayLink));
exports.BatchHttpLink = BatchHttpLink;
//# sourceMappingURL=batchHttpLink.js.map