relay-link-batch-http
Version:
Batch HTTP transport layer for Relay
115 lines (110 loc) • 4.95 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var relayLink = require('relay-link');
var relayLinkBatch = require('relay-link-batch');
var relayLinkHttpCommon = require('relay-link-http-common');
var relayRuntime = require('relay-runtime');
var BatchHttpLink = (function (_super) {
tslib.__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.__rest(_a, ["uri", "fetch", "batchInterval", "batchMax", "batchKey"]);
relayLinkHttpCommon.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 = relayLinkHttpCommon.selectURI(operations[0], uri);
var context = operations[0].getContext();
var contextConfig = {
http: context.http,
options: context.fetchOptions,
credentials: context.credentials,
headers: tslib.__assign({}, context.headers),
};
var optsAndBody = operations.map(function (operation) {
return relayLinkHttpCommon.selectHttpOptionsAndBody(operation, relayLinkHttpCommon.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 relayLink.fromError(new Error('relay-link-batch-http does not support GET requests'));
}
try {
;
options.body = relayLinkHttpCommon.serializeFetchParameter(loadedBody, 'Payload');
}
catch (parseError) {
return relayLink.fromError(parseError);
}
var controller;
if (!options.signal) {
var _a = relayLinkHttpCommon.createSignalIfSupported(), _controller = _a.controller, signal = _a.signal;
controller = _controller;
if (controller)
options.signal = signal;
}
return relayRuntime.Observable.create(function (sink) {
fetcher(chosenURI, options)
.then(function (response) {
operations.forEach(function (operation) { return operation.setContext({ response: response }); });
return response;
})
.then(relayLinkHttpCommon.parseAndCheckHttpResponse(operations, operations[0].getContext().bodyParser || relayLinkHttpCommon.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 relayLinkHttpCommon.selectURI(operation, uri) + JSON.stringify(contextConfig);
});
_this.batcher = new relayLinkBatch.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;
}(relayLink.RelayLink));
exports.BatchHttpLink = BatchHttpLink;
//# sourceMappingURL=bundle.cjs.js.map