1 | import { __assign, __rest } from "tslib";
|
2 | import "../../utilities/globals/index.js";
|
3 | import { visit } from 'graphql';
|
4 | import { ApolloLink } from "../core/index.js";
|
5 | import { Observable, hasDirectives } from "../../utilities/index.js";
|
6 | import { serializeFetchParameter } from "./serializeFetchParameter.js";
|
7 | import { selectURI } from "./selectURI.js";
|
8 | import { handleError, readMultipartBody, readJsonBody } from "./parseAndCheckHttpResponse.js";
|
9 | import { checkFetcher } from "./checkFetcher.js";
|
10 | import { selectHttpOptionsAndBodyInternal, defaultPrinter, fallbackHttpConfig } from "./selectHttpOptionsAndBody.js";
|
11 | import { createSignalIfSupported } from "./createSignalIfSupported.js";
|
12 | import { rewriteURIForGET } from "./rewriteURIForGET.js";
|
13 | import { fromError } from "../utils/index.js";
|
14 | import { maybe } from "../../utilities/index.js";
|
15 | var backupFetch = maybe(function () { return fetch; });
|
16 | export var createHttpLink = function (linkOptions) {
|
17 | if (linkOptions === void 0) { linkOptions = {}; }
|
18 | var _a = linkOptions.uri, uri = _a === void 0 ? '/graphql' : _a, preferredFetch = linkOptions.fetch, _b = linkOptions.print, print = _b === void 0 ? defaultPrinter : _b, includeExtensions = linkOptions.includeExtensions, preserveHeaderCase = linkOptions.preserveHeaderCase, useGETForQueries = linkOptions.useGETForQueries, _c = linkOptions.includeUnusedVariables, includeUnusedVariables = _c === void 0 ? false : _c, requestOptions = __rest(linkOptions, ["uri", "fetch", "print", "includeExtensions", "preserveHeaderCase", "useGETForQueries", "includeUnusedVariables"]);
|
19 | if (__DEV__) {
|
20 | checkFetcher(preferredFetch || backupFetch);
|
21 | }
|
22 | var linkConfig = {
|
23 | http: { includeExtensions: includeExtensions, preserveHeaderCase: preserveHeaderCase },
|
24 | options: requestOptions.fetchOptions,
|
25 | credentials: requestOptions.credentials,
|
26 | headers: requestOptions.headers,
|
27 | };
|
28 | return new ApolloLink(function (operation) {
|
29 | var chosenURI = selectURI(operation, uri);
|
30 | var context = operation.getContext();
|
31 | var clientAwarenessHeaders = {};
|
32 | if (context.clientAwareness) {
|
33 | var _a = context.clientAwareness, name_1 = _a.name, version = _a.version;
|
34 | if (name_1) {
|
35 | clientAwarenessHeaders['apollographql-client-name'] = name_1;
|
36 | }
|
37 | if (version) {
|
38 | clientAwarenessHeaders['apollographql-client-version'] = version;
|
39 | }
|
40 | }
|
41 | var contextHeaders = __assign(__assign({}, clientAwarenessHeaders), context.headers);
|
42 | var contextConfig = {
|
43 | http: context.http,
|
44 | options: context.fetchOptions,
|
45 | credentials: context.credentials,
|
46 | headers: contextHeaders,
|
47 | };
|
48 | var _b = selectHttpOptionsAndBodyInternal(operation, print, fallbackHttpConfig, linkConfig, contextConfig), options = _b.options, body = _b.body;
|
49 | if (body.variables && !includeUnusedVariables) {
|
50 | var unusedNames_1 = new Set(Object.keys(body.variables));
|
51 | visit(operation.query, {
|
52 | Variable: function (node, _key, parent) {
|
53 | if (parent && parent.kind !== 'VariableDefinition') {
|
54 | unusedNames_1.delete(node.name.value);
|
55 | }
|
56 | },
|
57 | });
|
58 | if (unusedNames_1.size) {
|
59 | body.variables = __assign({}, body.variables);
|
60 | unusedNames_1.forEach(function (name) {
|
61 | delete body.variables[name];
|
62 | });
|
63 | }
|
64 | }
|
65 | var controller;
|
66 | if (!options.signal) {
|
67 | var _c = createSignalIfSupported(), _controller = _c.controller, signal = _c.signal;
|
68 | controller = _controller;
|
69 | if (controller)
|
70 | options.signal = signal;
|
71 | }
|
72 | var definitionIsMutation = function (d) {
|
73 | return d.kind === 'OperationDefinition' && d.operation === 'mutation';
|
74 | };
|
75 | if (useGETForQueries &&
|
76 | !operation.query.definitions.some(definitionIsMutation)) {
|
77 | options.method = 'GET';
|
78 | }
|
79 | if (hasDirectives(['defer'], operation.query)) {
|
80 | options.headers = options.headers || {};
|
81 | options.headers.accept = "multipart/mixed; deferSpec=20220824, application/json";
|
82 | }
|
83 | if (options.method === 'GET') {
|
84 | var _d = rewriteURIForGET(chosenURI, body), newURI = _d.newURI, parseError = _d.parseError;
|
85 | if (parseError) {
|
86 | return fromError(parseError);
|
87 | }
|
88 | chosenURI = newURI;
|
89 | }
|
90 | else {
|
91 | try {
|
92 | options.body = serializeFetchParameter(body, 'Payload');
|
93 | }
|
94 | catch (parseError) {
|
95 | return fromError(parseError);
|
96 | }
|
97 | }
|
98 | return new Observable(function (observer) {
|
99 | var currentFetch = preferredFetch || maybe(function () { return fetch; }) || backupFetch;
|
100 | currentFetch(chosenURI, options)
|
101 | .then(function (response) {
|
102 | var _a;
|
103 | operation.setContext({ response: response });
|
104 | var ctype = (_a = response.headers) === null || _a === void 0 ? void 0 : _a.get('content-type');
|
105 | if (ctype !== null && /^multipart\/mixed/i.test(ctype)) {
|
106 | return readMultipartBody(response, observer);
|
107 | }
|
108 | else {
|
109 | return readJsonBody(response, operation, observer);
|
110 | }
|
111 | })
|
112 | .catch(function (err) { return handleError(err, observer); });
|
113 | return function () {
|
114 | if (controller)
|
115 | controller.abort();
|
116 | };
|
117 | });
|
118 | });
|
119 | };
|
120 |
|
\ | No newline at end of file |