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