1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var tslib = require('tslib');
|
6 | var core = require('../core');
|
7 | var utilities = require('../../utilities');
|
8 | var utils = require('../utils');
|
9 | var http = require('../http');
|
10 | var batch = require('../batch');
|
11 |
|
12 | var BatchHttpLink = (function (_super) {
|
13 | tslib.__extends(BatchHttpLink, _super);
|
14 | function BatchHttpLink(fetchParams) {
|
15 | var _this = _super.call(this) || this;
|
16 | var _a = fetchParams || {}, _b = _a.uri, uri = _b === void 0 ? '/graphql' : _b, fetcher = _a.fetch, _c = _a.print, print = _c === void 0 ? http.defaultPrinter : _c, includeExtensions = _a.includeExtensions, preserveHeaderCase = _a.preserveHeaderCase, batchInterval = _a.batchInterval, batchDebounce = _a.batchDebounce, batchMax = _a.batchMax, batchKey = _a.batchKey, requestOptions = tslib.__rest(_a, ["uri", "fetch", "print", "includeExtensions", "preserveHeaderCase", "batchInterval", "batchDebounce", "batchMax", "batchKey"]);
|
17 | http.checkFetcher(fetcher);
|
18 | if (!fetcher) {
|
19 | fetcher = fetch;
|
20 | }
|
21 | var linkConfig = {
|
22 | http: { includeExtensions: includeExtensions, preserveHeaderCase: preserveHeaderCase },
|
23 | options: requestOptions.fetchOptions,
|
24 | credentials: requestOptions.credentials,
|
25 | headers: requestOptions.headers,
|
26 | };
|
27 | _this.batchDebounce = batchDebounce;
|
28 | _this.batchInterval = batchInterval || 10;
|
29 | _this.batchMax = batchMax || 10;
|
30 | var batchHandler = function (operations) {
|
31 | var chosenURI = http.selectURI(operations[0], uri);
|
32 | var context = operations[0].getContext();
|
33 | var clientAwarenessHeaders = {};
|
34 | if (context.clientAwareness) {
|
35 | var _a = context.clientAwareness, name_1 = _a.name, version = _a.version;
|
36 | if (name_1) {
|
37 | clientAwarenessHeaders['apollographql-client-name'] = name_1;
|
38 | }
|
39 | if (version) {
|
40 | clientAwarenessHeaders['apollographql-client-version'] = version;
|
41 | }
|
42 | }
|
43 | var contextConfig = {
|
44 | http: context.http,
|
45 | options: context.fetchOptions,
|
46 | credentials: context.credentials,
|
47 | headers: tslib.__assign(tslib.__assign({}, clientAwarenessHeaders), context.headers),
|
48 | };
|
49 | var optsAndBody = operations.map(function (operation) {
|
50 | return http.selectHttpOptionsAndBodyInternal(operation, print, http.fallbackHttpConfig, linkConfig, contextConfig);
|
51 | });
|
52 | var loadedBody = optsAndBody.map(function (_a) {
|
53 | var body = _a.body;
|
54 | return body;
|
55 | });
|
56 | var options = optsAndBody[0].options;
|
57 | if (options.method === 'GET') {
|
58 | return utils.fromError(new Error('apollo-link-batch-http does not support GET requests'));
|
59 | }
|
60 | try {
|
61 | options.body = http.serializeFetchParameter(loadedBody, 'Payload');
|
62 | }
|
63 | catch (parseError) {
|
64 | return utils.fromError(parseError);
|
65 | }
|
66 | var controller;
|
67 | if (!options.signal) {
|
68 | var _b = http.createSignalIfSupported(), _controller = _b.controller, signal = _b.signal;
|
69 | controller = _controller;
|
70 | if (controller)
|
71 | options.signal = signal;
|
72 | }
|
73 | return new utilities.Observable(function (observer) {
|
74 | fetcher(chosenURI, options)
|
75 | .then(function (response) {
|
76 | operations.forEach(function (operation) { return operation.setContext({ response: response }); });
|
77 | return response;
|
78 | })
|
79 | .then(http.parseAndCheckHttpResponse(operations))
|
80 | .then(function (result) {
|
81 | observer.next(result);
|
82 | observer.complete();
|
83 | return result;
|
84 | })
|
85 | .catch(function (err) {
|
86 | if (err.name === 'AbortError')
|
87 | return;
|
88 | if (err.result && err.result.errors && err.result.data) {
|
89 | observer.next(err.result);
|
90 | }
|
91 | observer.error(err);
|
92 | });
|
93 | return function () {
|
94 | if (controller)
|
95 | controller.abort();
|
96 | };
|
97 | });
|
98 | };
|
99 | batchKey =
|
100 | batchKey ||
|
101 | (function (operation) {
|
102 | var context = operation.getContext();
|
103 | var contextConfig = {
|
104 | http: context.http,
|
105 | options: context.fetchOptions,
|
106 | credentials: context.credentials,
|
107 | headers: context.headers,
|
108 | };
|
109 | return http.selectURI(operation, uri) + JSON.stringify(contextConfig);
|
110 | });
|
111 | _this.batcher = new batch.BatchLink({
|
112 | batchDebounce: _this.batchDebounce,
|
113 | batchInterval: _this.batchInterval,
|
114 | batchMax: _this.batchMax,
|
115 | batchKey: batchKey,
|
116 | batchHandler: batchHandler,
|
117 | });
|
118 | return _this;
|
119 | }
|
120 | BatchHttpLink.prototype.request = function (operation) {
|
121 | return this.batcher.request(operation);
|
122 | };
|
123 | return BatchHttpLink;
|
124 | }(core.ApolloLink));
|
125 |
|
126 | exports.BatchHttpLink = BatchHttpLink;
|
127 |
|