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 } from "../../utilities/index.js";
|
6 | import { serializeFetchParameter } from "./serializeFetchParameter.js";
|
7 | import { selectURI } from "./selectURI.js";
|
8 | import { parseAndCheckHttpResponse } 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, useGETForQueries = linkOptions.useGETForQueries, _c = linkOptions.includeUnusedVariables, includeUnusedVariables = _c === void 0 ? false : _c, requestOptions = __rest(linkOptions, ["uri", "fetch", "print", "includeExtensions", "useGETForQueries", "includeUnusedVariables"]);
|
19 | if (__DEV__) {
|
20 | checkFetcher(preferredFetch || backupFetch);
|
21 | }
|
22 | var linkConfig = {
|
23 | http: { includeExtensions: includeExtensions },
|
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 (options.method === 'GET') {
|
80 | var _d = rewriteURIForGET(chosenURI, body), newURI = _d.newURI, parseError = _d.parseError;
|
81 | if (parseError) {
|
82 | return fromError(parseError);
|
83 | }
|
84 | chosenURI = newURI;
|
85 | }
|
86 | else {
|
87 | try {
|
88 | options.body = serializeFetchParameter(body, 'Payload');
|
89 | }
|
90 | catch (parseError) {
|
91 | return fromError(parseError);
|
92 | }
|
93 | }
|
94 | return new Observable(function (observer) {
|
95 | var currentFetch = preferredFetch || maybe(function () { return fetch; }) || backupFetch;
|
96 | currentFetch(chosenURI, options)
|
97 | .then(function (response) {
|
98 | operation.setContext({ response: response });
|
99 | return response;
|
100 | })
|
101 | .then(parseAndCheckHttpResponse(operation))
|
102 | .then(function (result) {
|
103 | observer.next(result);
|
104 | observer.complete();
|
105 | return result;
|
106 | })
|
107 | .catch(function (err) {
|
108 | if (err.name === 'AbortError')
|
109 | return;
|
110 | if (err.result && err.result.errors && err.result.data) {
|
111 | observer.next(err.result);
|
112 | }
|
113 | observer.error(err);
|
114 | });
|
115 | return function () {
|
116 | if (controller)
|
117 | controller.abort();
|
118 | };
|
119 | });
|
120 | });
|
121 | };
|
122 |
|
\ | No newline at end of file |