@apollo/client 3.5.10 → 3.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +5 -5
  3. package/apollo-client.cjs +563 -375
  4. package/apollo-client.cjs.map +1 -1
  5. package/apollo-client.min.cjs +1 -1
  6. package/cache/cache.cjs +10 -11
  7. package/cache/cache.cjs.map +1 -1
  8. package/cache/cache.cjs.native.js +2288 -0
  9. package/cache/inmemory/policies.js +1 -1
  10. package/cache/inmemory/policies.js.map +1 -1
  11. package/cache/inmemory/readFromStore.d.ts.map +1 -1
  12. package/cache/inmemory/readFromStore.js +10 -11
  13. package/cache/inmemory/readFromStore.js.map +1 -1
  14. package/core/ApolloClient.d.ts +2 -3
  15. package/core/ApolloClient.d.ts.map +1 -1
  16. package/core/ApolloClient.js +4 -8
  17. package/core/ApolloClient.js.map +1 -1
  18. package/core/ObservableQuery.d.ts +10 -4
  19. package/core/ObservableQuery.d.ts.map +1 -1
  20. package/core/ObservableQuery.js +101 -46
  21. package/core/ObservableQuery.js.map +1 -1
  22. package/core/QueryInfo.d.ts.map +1 -1
  23. package/core/QueryInfo.js +4 -2
  24. package/core/QueryInfo.js.map +1 -1
  25. package/core/QueryManager.d.ts +5 -2
  26. package/core/QueryManager.d.ts.map +1 -1
  27. package/core/QueryManager.js +36 -27
  28. package/core/QueryManager.js.map +1 -1
  29. package/core/core.cjs +148 -90
  30. package/core/core.cjs.map +1 -1
  31. package/core/core.cjs.native.js +2146 -0
  32. package/core/index.d.ts +1 -1
  33. package/core/index.d.ts.map +1 -1
  34. package/core/index.js +1 -1
  35. package/core/index.js.map +1 -1
  36. package/core/watchQueryOptions.d.ts +9 -1
  37. package/core/watchQueryOptions.d.ts.map +1 -1
  38. package/core/watchQueryOptions.js.map +1 -1
  39. package/errors/errors.cjs.native.js +48 -0
  40. package/invariantErrorCodes.js +1 -1
  41. package/link/batch/batch.cjs +46 -37
  42. package/link/batch/batch.cjs.map +1 -1
  43. package/link/batch/batch.cjs.native.js +160 -0
  44. package/link/batch/batching.d.ts +2 -6
  45. package/link/batch/batching.d.ts.map +1 -1
  46. package/link/batch/batching.js +46 -37
  47. package/link/batch/batching.js.map +1 -1
  48. package/link/batch-http/batch-http.cjs.native.js +127 -0
  49. package/link/context/context.cjs.native.js +38 -0
  50. package/link/core/core.cjs.native.js +121 -0
  51. package/link/error/error.cjs.native.js +90 -0
  52. package/link/http/http.cjs.native.js +320 -0
  53. package/link/http/selectHttpOptionsAndBody.d.ts.map +1 -1
  54. package/link/http/selectHttpOptionsAndBody.js.map +1 -1
  55. package/link/persisted-queries/index.d.ts +2 -1
  56. package/link/persisted-queries/index.d.ts.map +1 -1
  57. package/link/persisted-queries/index.js +26 -13
  58. package/link/persisted-queries/index.js.map +1 -1
  59. package/link/persisted-queries/persisted-queries.cjs +25 -12
  60. package/link/persisted-queries/persisted-queries.cjs.map +1 -1
  61. package/link/persisted-queries/persisted-queries.cjs.native.js +174 -0
  62. package/link/retry/retry.cjs.native.js +170 -0
  63. package/link/schema/schema.cjs.native.js +56 -0
  64. package/link/subscriptions/subscriptions.cjs.native.js +45 -0
  65. package/link/utils/utils.cjs.native.js +115 -0
  66. package/link/ws/ws.cjs.native.js +28 -0
  67. package/main.cjs.native.js +16 -0
  68. package/package.json +30 -27
  69. package/react/components/components.cjs.native.js +79 -0
  70. package/react/context/ApolloConsumer.js +2 -2
  71. package/react/context/ApolloProvider.js +2 -2
  72. package/react/context/context.cjs +4 -4
  73. package/react/context/context.cjs.map +1 -1
  74. package/react/context/context.cjs.native.js +67 -0
  75. package/react/hoc/hoc.cjs.native.js +325 -0
  76. package/react/hooks/hooks.cjs +376 -253
  77. package/react/hooks/hooks.cjs.map +1 -1
  78. package/react/hooks/hooks.cjs.native.js +623 -0
  79. package/react/hooks/index.d.ts +1 -1
  80. package/react/hooks/index.d.ts.map +1 -1
  81. package/react/hooks/index.js +1 -1
  82. package/react/hooks/index.js.map +1 -1
  83. package/react/hooks/useLazyQuery.d.ts +2 -2
  84. package/react/hooks/useLazyQuery.d.ts.map +1 -1
  85. package/react/hooks/useLazyQuery.js +26 -21
  86. package/react/hooks/useLazyQuery.js.map +1 -1
  87. package/react/hooks/useMutation.d.ts.map +1 -1
  88. package/react/hooks/useMutation.js +13 -10
  89. package/react/hooks/useMutation.js.map +1 -1
  90. package/react/hooks/useQuery.d.ts +37 -2
  91. package/react/hooks/useQuery.d.ts.map +1 -1
  92. package/react/hooks/useQuery.js +256 -206
  93. package/react/hooks/useQuery.js.map +1 -1
  94. package/react/hooks/useSubscription.d.ts.map +1 -1
  95. package/react/hooks/useSubscription.js +17 -7
  96. package/react/hooks/useSubscription.js.map +1 -1
  97. package/react/hooks/useSyncExternalStore.d.ts +4 -0
  98. package/react/hooks/useSyncExternalStore.d.ts.map +1 -0
  99. package/react/hooks/useSyncExternalStore.js +48 -0
  100. package/react/hooks/useSyncExternalStore.js.map +1 -0
  101. package/react/parser/index.d.ts.map +1 -1
  102. package/react/parser/index.js +24 -10
  103. package/react/parser/index.js.map +1 -1
  104. package/react/parser/parser.cjs +24 -10
  105. package/react/parser/parser.cjs.map +1 -1
  106. package/react/parser/parser.cjs.native.js +103 -0
  107. package/react/react.cjs.native.js +22 -0
  108. package/react/ssr/RenderPromises.d.ts +3 -2
  109. package/react/ssr/RenderPromises.d.ts.map +1 -1
  110. package/react/ssr/RenderPromises.js +25 -3
  111. package/react/ssr/RenderPromises.js.map +1 -1
  112. package/react/ssr/ssr.cjs +25 -3
  113. package/react/ssr/ssr.cjs.map +1 -1
  114. package/react/ssr/ssr.cjs.native.js +150 -0
  115. package/react/types/types.d.ts +10 -11
  116. package/react/types/types.d.ts.map +1 -1
  117. package/react/types/types.js.map +1 -1
  118. package/testing/core/core.cjs.native.js +288 -0
  119. package/testing/core/mocking/mockFetch.js +1 -1
  120. package/testing/core/mocking/mockFetch.js.map +1 -1
  121. package/testing/core/mocking/mockQueryManager.js +1 -1
  122. package/testing/core/mocking/mockWatchQuery.js +1 -1
  123. package/testing/core/wrap.js +1 -1
  124. package/testing/testing.cjs.native.js +58 -0
  125. package/utilities/common/canUse.d.ts +2 -0
  126. package/utilities/common/canUse.d.ts.map +1 -1
  127. package/utilities/common/canUse.js +6 -2
  128. package/utilities/common/canUse.js.map +1 -1
  129. package/utilities/common/mergeDeep.d.ts.map +1 -1
  130. package/utilities/common/mergeDeep.js +8 -11
  131. package/utilities/common/mergeDeep.js.map +1 -1
  132. package/utilities/common/mergeOptions.d.ts +5 -0
  133. package/utilities/common/mergeOptions.d.ts.map +1 -0
  134. package/utilities/common/mergeOptions.js +8 -0
  135. package/utilities/common/mergeOptions.js.map +1 -0
  136. package/utilities/globals/global.js +1 -2
  137. package/utilities/globals/globals.cjs.native.js +56 -0
  138. package/utilities/graphql/transform.d.ts +2 -2
  139. package/utilities/graphql/transform.d.ts.map +1 -1
  140. package/utilities/graphql/transform.js +1 -1
  141. package/utilities/graphql/transform.js.map +1 -1
  142. package/utilities/index.d.ts +1 -0
  143. package/utilities/index.d.ts.map +1 -1
  144. package/utilities/index.js +1 -0
  145. package/utilities/index.js.map +1 -1
  146. package/utilities/observables/Concast.d.ts +1 -0
  147. package/utilities/observables/Concast.d.ts.map +1 -1
  148. package/utilities/observables/Concast.js +5 -2
  149. package/utilities/observables/Concast.js.map +1 -1
  150. package/utilities/policies/pagination.d.ts.map +1 -1
  151. package/utilities/policies/pagination.js +9 -7
  152. package/utilities/policies/pagination.js.map +1 -1
  153. package/utilities/utilities.cjs +37 -23
  154. package/utilities/utilities.cjs.map +1 -1
  155. package/utilities/utilities.cjs.native.js +1281 -0
  156. package/version.js +1 -1
@@ -0,0 +1,2146 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var globals = require('../utilities/globals');
6
+ var tslib = require('tslib');
7
+ var core = require('../link/core');
8
+ var http = require('../link/http');
9
+ var equality = require('@wry/equality');
10
+ var cache = require('../cache');
11
+ var utilities = require('../utilities');
12
+ var errors = require('../errors');
13
+ var graphql = require('graphql');
14
+ var utils = require('../link/utils');
15
+ var tsInvariant = require('ts-invariant');
16
+ var graphqlTag = require('graphql-tag');
17
+
18
+ var version = '3.6.9';
19
+
20
+ exports.NetworkStatus = void 0;
21
+ (function (NetworkStatus) {
22
+ NetworkStatus[NetworkStatus["loading"] = 1] = "loading";
23
+ NetworkStatus[NetworkStatus["setVariables"] = 2] = "setVariables";
24
+ NetworkStatus[NetworkStatus["fetchMore"] = 3] = "fetchMore";
25
+ NetworkStatus[NetworkStatus["refetch"] = 4] = "refetch";
26
+ NetworkStatus[NetworkStatus["poll"] = 6] = "poll";
27
+ NetworkStatus[NetworkStatus["ready"] = 7] = "ready";
28
+ NetworkStatus[NetworkStatus["error"] = 8] = "error";
29
+ })(exports.NetworkStatus || (exports.NetworkStatus = {}));
30
+ function isNetworkRequestInFlight(networkStatus) {
31
+ return networkStatus ? networkStatus < 7 : false;
32
+ }
33
+
34
+ var assign = Object.assign, hasOwnProperty$1 = Object.hasOwnProperty;
35
+ var ObservableQuery = (function (_super) {
36
+ tslib.__extends(ObservableQuery, _super);
37
+ function ObservableQuery(_a) {
38
+ var queryManager = _a.queryManager, queryInfo = _a.queryInfo, options = _a.options;
39
+ var _this = _super.call(this, function (observer) {
40
+ try {
41
+ var subObserver = observer._subscription._observer;
42
+ if (subObserver && !subObserver.error) {
43
+ subObserver.error = defaultSubscriptionObserverErrorCallback;
44
+ }
45
+ }
46
+ catch (_a) { }
47
+ var first = !_this.observers.size;
48
+ _this.observers.add(observer);
49
+ var last = _this.last;
50
+ if (last && last.error) {
51
+ observer.error && observer.error(last.error);
52
+ }
53
+ else if (last && last.result) {
54
+ observer.next && observer.next(last.result);
55
+ }
56
+ if (first) {
57
+ _this.reobserve().catch(function () { });
58
+ }
59
+ return function () {
60
+ if (_this.observers.delete(observer) && !_this.observers.size) {
61
+ _this.tearDownQuery();
62
+ }
63
+ };
64
+ }) || this;
65
+ _this.observers = new Set();
66
+ _this.subscriptions = new Set();
67
+ _this.queryInfo = queryInfo;
68
+ _this.queryManager = queryManager;
69
+ _this.isTornDown = false;
70
+ var _b = queryManager.defaultOptions.watchQuery, _c = _b === void 0 ? {} : _b, _d = _c.fetchPolicy, defaultFetchPolicy = _d === void 0 ? "cache-first" : _d;
71
+ var _e = options.fetchPolicy, fetchPolicy = _e === void 0 ? defaultFetchPolicy : _e, _f = options.initialFetchPolicy, initialFetchPolicy = _f === void 0 ? (fetchPolicy === "standby" ? defaultFetchPolicy : fetchPolicy) : _f;
72
+ _this.options = tslib.__assign(tslib.__assign({}, options), { initialFetchPolicy: initialFetchPolicy, fetchPolicy: fetchPolicy });
73
+ _this.queryId = queryInfo.queryId || queryManager.generateQueryId();
74
+ var opDef = utilities.getOperationDefinition(_this.query);
75
+ _this.queryName = opDef && opDef.name && opDef.name.value;
76
+ return _this;
77
+ }
78
+ Object.defineProperty(ObservableQuery.prototype, "query", {
79
+ get: function () {
80
+ return this.queryManager.transform(this.options.query).document;
81
+ },
82
+ enumerable: false,
83
+ configurable: true
84
+ });
85
+ Object.defineProperty(ObservableQuery.prototype, "variables", {
86
+ get: function () {
87
+ return this.options.variables;
88
+ },
89
+ enumerable: false,
90
+ configurable: true
91
+ });
92
+ ObservableQuery.prototype.result = function () {
93
+ var _this = this;
94
+ return new Promise(function (resolve, reject) {
95
+ var observer = {
96
+ next: function (result) {
97
+ resolve(result);
98
+ _this.observers.delete(observer);
99
+ if (!_this.observers.size) {
100
+ _this.queryManager.removeQuery(_this.queryId);
101
+ }
102
+ setTimeout(function () {
103
+ subscription.unsubscribe();
104
+ }, 0);
105
+ },
106
+ error: reject,
107
+ };
108
+ var subscription = _this.subscribe(observer);
109
+ });
110
+ };
111
+ ObservableQuery.prototype.getCurrentResult = function (saveAsLastResult) {
112
+ if (saveAsLastResult === void 0) { saveAsLastResult = true; }
113
+ var lastResult = this.getLastResult(true);
114
+ var networkStatus = this.queryInfo.networkStatus ||
115
+ (lastResult && lastResult.networkStatus) ||
116
+ exports.NetworkStatus.ready;
117
+ var result = tslib.__assign(tslib.__assign({}, lastResult), { loading: isNetworkRequestInFlight(networkStatus), networkStatus: networkStatus });
118
+ var _a = this.options.fetchPolicy, fetchPolicy = _a === void 0 ? "cache-first" : _a;
119
+ if (fetchPolicy === 'network-only' ||
120
+ fetchPolicy === 'no-cache' ||
121
+ fetchPolicy === 'standby' ||
122
+ this.queryManager.transform(this.options.query).hasForcedResolvers) ;
123
+ else {
124
+ var diff = this.queryInfo.getDiff();
125
+ if (diff.complete || this.options.returnPartialData) {
126
+ result.data = diff.result;
127
+ }
128
+ if (equality.equal(result.data, {})) {
129
+ result.data = void 0;
130
+ }
131
+ if (diff.complete) {
132
+ delete result.partial;
133
+ if (diff.complete &&
134
+ result.networkStatus === exports.NetworkStatus.loading &&
135
+ (fetchPolicy === 'cache-first' ||
136
+ fetchPolicy === 'cache-only')) {
137
+ result.networkStatus = exports.NetworkStatus.ready;
138
+ result.loading = false;
139
+ }
140
+ }
141
+ else {
142
+ result.partial = true;
143
+ }
144
+ if (__DEV__ &&
145
+ !diff.complete &&
146
+ !this.options.partialRefetch &&
147
+ !result.loading &&
148
+ !result.data &&
149
+ !result.error) {
150
+ logMissingFieldErrors(diff.missing);
151
+ }
152
+ }
153
+ if (saveAsLastResult) {
154
+ this.updateLastResult(result);
155
+ }
156
+ return result;
157
+ };
158
+ ObservableQuery.prototype.isDifferentFromLastResult = function (newResult) {
159
+ return !this.last || !equality.equal(this.last.result, newResult);
160
+ };
161
+ ObservableQuery.prototype.getLast = function (key, variablesMustMatch) {
162
+ var last = this.last;
163
+ if (last &&
164
+ last[key] &&
165
+ (!variablesMustMatch || equality.equal(last.variables, this.variables))) {
166
+ return last[key];
167
+ }
168
+ };
169
+ ObservableQuery.prototype.getLastResult = function (variablesMustMatch) {
170
+ return this.getLast("result", variablesMustMatch);
171
+ };
172
+ ObservableQuery.prototype.getLastError = function (variablesMustMatch) {
173
+ return this.getLast("error", variablesMustMatch);
174
+ };
175
+ ObservableQuery.prototype.resetLastResults = function () {
176
+ delete this.last;
177
+ this.isTornDown = false;
178
+ };
179
+ ObservableQuery.prototype.resetQueryStoreErrors = function () {
180
+ this.queryManager.resetErrors(this.queryId);
181
+ };
182
+ ObservableQuery.prototype.refetch = function (variables) {
183
+ var _a;
184
+ var reobserveOptions = {
185
+ pollInterval: 0,
186
+ };
187
+ var fetchPolicy = this.options.fetchPolicy;
188
+ if (fetchPolicy === 'cache-and-network') {
189
+ reobserveOptions.fetchPolicy = fetchPolicy;
190
+ }
191
+ else if (fetchPolicy === 'no-cache') {
192
+ reobserveOptions.fetchPolicy = 'no-cache';
193
+ }
194
+ else {
195
+ reobserveOptions.fetchPolicy = 'network-only';
196
+ }
197
+ if (__DEV__ && variables && hasOwnProperty$1.call(variables, "variables")) {
198
+ var queryDef = utilities.getQueryDefinition(this.query);
199
+ var vars = queryDef.variableDefinitions;
200
+ if (!vars || !vars.some(function (v) { return v.variable.name.value === "variables"; })) {
201
+ __DEV__ && globals.invariant.warn("Called refetch(".concat(JSON.stringify(variables), ") for query ").concat(((_a = queryDef.name) === null || _a === void 0 ? void 0 : _a.value) || JSON.stringify(queryDef), ", which does not declare a $variables variable.\nDid you mean to call refetch(variables) instead of refetch({ variables })?"));
202
+ }
203
+ }
204
+ if (variables && !equality.equal(this.options.variables, variables)) {
205
+ reobserveOptions.variables = this.options.variables = tslib.__assign(tslib.__assign({}, this.options.variables), variables);
206
+ }
207
+ this.queryInfo.resetLastWrite();
208
+ return this.reobserve(reobserveOptions, exports.NetworkStatus.refetch);
209
+ };
210
+ ObservableQuery.prototype.fetchMore = function (fetchMoreOptions) {
211
+ var _this = this;
212
+ var combinedOptions = tslib.__assign(tslib.__assign({}, (fetchMoreOptions.query ? fetchMoreOptions : tslib.__assign(tslib.__assign(tslib.__assign(tslib.__assign({}, this.options), { query: this.query }), fetchMoreOptions), { variables: tslib.__assign(tslib.__assign({}, this.options.variables), fetchMoreOptions.variables) }))), { fetchPolicy: "no-cache" });
213
+ var qid = this.queryManager.generateQueryId();
214
+ var queryInfo = this.queryInfo;
215
+ var originalNetworkStatus = queryInfo.networkStatus;
216
+ queryInfo.networkStatus = exports.NetworkStatus.fetchMore;
217
+ if (combinedOptions.notifyOnNetworkStatusChange) {
218
+ this.observe();
219
+ }
220
+ var updatedQuerySet = new Set();
221
+ return this.queryManager.fetchQuery(qid, combinedOptions, exports.NetworkStatus.fetchMore).then(function (fetchMoreResult) {
222
+ _this.queryManager.removeQuery(qid);
223
+ if (queryInfo.networkStatus === exports.NetworkStatus.fetchMore) {
224
+ queryInfo.networkStatus = originalNetworkStatus;
225
+ }
226
+ _this.queryManager.cache.batch({
227
+ update: function (cache) {
228
+ var updateQuery = fetchMoreOptions.updateQuery;
229
+ if (updateQuery) {
230
+ cache.updateQuery({
231
+ query: _this.query,
232
+ variables: _this.variables,
233
+ returnPartialData: true,
234
+ optimistic: false,
235
+ }, function (previous) { return updateQuery(previous, {
236
+ fetchMoreResult: fetchMoreResult.data,
237
+ variables: combinedOptions.variables,
238
+ }); });
239
+ }
240
+ else {
241
+ cache.writeQuery({
242
+ query: combinedOptions.query,
243
+ variables: combinedOptions.variables,
244
+ data: fetchMoreResult.data,
245
+ });
246
+ }
247
+ },
248
+ onWatchUpdated: function (watch) {
249
+ updatedQuerySet.add(watch.query);
250
+ },
251
+ });
252
+ return fetchMoreResult;
253
+ }).finally(function () {
254
+ if (!updatedQuerySet.has(_this.query)) {
255
+ reobserveCacheFirst(_this);
256
+ }
257
+ });
258
+ };
259
+ ObservableQuery.prototype.subscribeToMore = function (options) {
260
+ var _this = this;
261
+ var subscription = this.queryManager
262
+ .startGraphQLSubscription({
263
+ query: options.document,
264
+ variables: options.variables,
265
+ context: options.context,
266
+ })
267
+ .subscribe({
268
+ next: function (subscriptionData) {
269
+ var updateQuery = options.updateQuery;
270
+ if (updateQuery) {
271
+ _this.updateQuery(function (previous, _a) {
272
+ var variables = _a.variables;
273
+ return updateQuery(previous, {
274
+ subscriptionData: subscriptionData,
275
+ variables: variables,
276
+ });
277
+ });
278
+ }
279
+ },
280
+ error: function (err) {
281
+ if (options.onError) {
282
+ options.onError(err);
283
+ return;
284
+ }
285
+ __DEV__ && globals.invariant.error('Unhandled GraphQL subscription error', err);
286
+ },
287
+ });
288
+ this.subscriptions.add(subscription);
289
+ return function () {
290
+ if (_this.subscriptions.delete(subscription)) {
291
+ subscription.unsubscribe();
292
+ }
293
+ };
294
+ };
295
+ ObservableQuery.prototype.setOptions = function (newOptions) {
296
+ return this.reobserve(newOptions);
297
+ };
298
+ ObservableQuery.prototype.setVariables = function (variables) {
299
+ if (equality.equal(this.variables, variables)) {
300
+ return this.observers.size
301
+ ? this.result()
302
+ : Promise.resolve();
303
+ }
304
+ this.options.variables = variables;
305
+ if (!this.observers.size) {
306
+ return Promise.resolve();
307
+ }
308
+ return this.reobserve({
309
+ fetchPolicy: this.options.initialFetchPolicy,
310
+ variables: variables,
311
+ }, exports.NetworkStatus.setVariables);
312
+ };
313
+ ObservableQuery.prototype.updateQuery = function (mapFn) {
314
+ var queryManager = this.queryManager;
315
+ var result = queryManager.cache.diff({
316
+ query: this.options.query,
317
+ variables: this.variables,
318
+ returnPartialData: true,
319
+ optimistic: false,
320
+ }).result;
321
+ var newResult = mapFn(result, {
322
+ variables: this.variables,
323
+ });
324
+ if (newResult) {
325
+ queryManager.cache.writeQuery({
326
+ query: this.options.query,
327
+ data: newResult,
328
+ variables: this.variables,
329
+ });
330
+ queryManager.broadcastQueries();
331
+ }
332
+ };
333
+ ObservableQuery.prototype.startPolling = function (pollInterval) {
334
+ this.options.pollInterval = pollInterval;
335
+ this.updatePolling();
336
+ };
337
+ ObservableQuery.prototype.stopPolling = function () {
338
+ this.options.pollInterval = 0;
339
+ this.updatePolling();
340
+ };
341
+ ObservableQuery.prototype.applyNextFetchPolicy = function (reason, options) {
342
+ if (options.nextFetchPolicy) {
343
+ var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? "cache-first" : _a, _b = options.initialFetchPolicy, initialFetchPolicy = _b === void 0 ? fetchPolicy : _b;
344
+ if (fetchPolicy === "standby") ;
345
+ else if (typeof options.nextFetchPolicy === "function") {
346
+ options.fetchPolicy = options.nextFetchPolicy(fetchPolicy, {
347
+ reason: reason,
348
+ options: options,
349
+ observable: this,
350
+ initialFetchPolicy: initialFetchPolicy,
351
+ });
352
+ }
353
+ else if (reason === "variables-changed") {
354
+ options.fetchPolicy = initialFetchPolicy;
355
+ }
356
+ else {
357
+ options.fetchPolicy = options.nextFetchPolicy;
358
+ }
359
+ }
360
+ return options.fetchPolicy;
361
+ };
362
+ ObservableQuery.prototype.fetch = function (options, newNetworkStatus) {
363
+ this.queryManager.setObservableQuery(this);
364
+ return this.queryManager.fetchQueryObservable(this.queryId, options, newNetworkStatus);
365
+ };
366
+ ObservableQuery.prototype.updatePolling = function () {
367
+ var _this = this;
368
+ if (this.queryManager.ssrMode) {
369
+ return;
370
+ }
371
+ var _a = this, pollingInfo = _a.pollingInfo, pollInterval = _a.options.pollInterval;
372
+ if (!pollInterval) {
373
+ if (pollingInfo) {
374
+ clearTimeout(pollingInfo.timeout);
375
+ delete this.pollingInfo;
376
+ }
377
+ return;
378
+ }
379
+ if (pollingInfo &&
380
+ pollingInfo.interval === pollInterval) {
381
+ return;
382
+ }
383
+ __DEV__ ? globals.invariant(pollInterval, 'Attempted to start a polling query without a polling interval.') : globals.invariant(pollInterval, 10);
384
+ var info = pollingInfo || (this.pollingInfo = {});
385
+ info.interval = pollInterval;
386
+ var maybeFetch = function () {
387
+ if (_this.pollingInfo) {
388
+ if (!isNetworkRequestInFlight(_this.queryInfo.networkStatus)) {
389
+ _this.reobserve({
390
+ fetchPolicy: "network-only",
391
+ }, exports.NetworkStatus.poll).then(poll, poll);
392
+ }
393
+ else {
394
+ poll();
395
+ }
396
+ }
397
+ };
398
+ var poll = function () {
399
+ var info = _this.pollingInfo;
400
+ if (info) {
401
+ clearTimeout(info.timeout);
402
+ info.timeout = setTimeout(maybeFetch, info.interval);
403
+ }
404
+ };
405
+ poll();
406
+ };
407
+ ObservableQuery.prototype.updateLastResult = function (newResult, variables) {
408
+ if (variables === void 0) { variables = this.variables; }
409
+ this.last = tslib.__assign(tslib.__assign({}, this.last), { result: this.queryManager.assumeImmutableResults
410
+ ? newResult
411
+ : utilities.cloneDeep(newResult), variables: variables });
412
+ if (!utilities.isNonEmptyArray(newResult.errors)) {
413
+ delete this.last.error;
414
+ }
415
+ return this.last;
416
+ };
417
+ ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
418
+ var _this = this;
419
+ this.isTornDown = false;
420
+ var useDisposableConcast = newNetworkStatus === exports.NetworkStatus.refetch ||
421
+ newNetworkStatus === exports.NetworkStatus.fetchMore ||
422
+ newNetworkStatus === exports.NetworkStatus.poll;
423
+ var oldVariables = this.options.variables;
424
+ var oldFetchPolicy = this.options.fetchPolicy;
425
+ var mergedOptions = utilities.compact(this.options, newOptions || {});
426
+ var options = useDisposableConcast
427
+ ? mergedOptions
428
+ : assign(this.options, mergedOptions);
429
+ if (!useDisposableConcast) {
430
+ this.updatePolling();
431
+ if (newOptions &&
432
+ newOptions.variables &&
433
+ !equality.equal(newOptions.variables, oldVariables) &&
434
+ options.fetchPolicy !== "standby" &&
435
+ options.fetchPolicy === oldFetchPolicy) {
436
+ this.applyNextFetchPolicy("variables-changed", options);
437
+ if (newNetworkStatus === void 0) {
438
+ newNetworkStatus = exports.NetworkStatus.setVariables;
439
+ }
440
+ }
441
+ }
442
+ var variables = options.variables && tslib.__assign({}, options.variables);
443
+ var concast = this.fetch(options, newNetworkStatus);
444
+ var observer = {
445
+ next: function (result) {
446
+ _this.reportResult(result, variables);
447
+ },
448
+ error: function (error) {
449
+ _this.reportError(error, variables);
450
+ },
451
+ };
452
+ if (!useDisposableConcast) {
453
+ if (this.concast && this.observer) {
454
+ this.concast.removeObserver(this.observer);
455
+ }
456
+ this.concast = concast;
457
+ this.observer = observer;
458
+ }
459
+ concast.addObserver(observer);
460
+ return concast.promise;
461
+ };
462
+ ObservableQuery.prototype.observe = function () {
463
+ this.reportResult(this.getCurrentResult(false), this.variables);
464
+ };
465
+ ObservableQuery.prototype.reportResult = function (result, variables) {
466
+ var lastError = this.getLastError();
467
+ if (lastError || this.isDifferentFromLastResult(result)) {
468
+ if (lastError || !result.partial || this.options.returnPartialData) {
469
+ this.updateLastResult(result, variables);
470
+ }
471
+ utilities.iterateObserversSafely(this.observers, 'next', result);
472
+ }
473
+ };
474
+ ObservableQuery.prototype.reportError = function (error, variables) {
475
+ var errorResult = tslib.__assign(tslib.__assign({}, this.getLastResult()), { error: error, errors: error.graphQLErrors, networkStatus: exports.NetworkStatus.error, loading: false });
476
+ this.updateLastResult(errorResult, variables);
477
+ utilities.iterateObserversSafely(this.observers, 'error', this.last.error = error);
478
+ };
479
+ ObservableQuery.prototype.hasObservers = function () {
480
+ return this.observers.size > 0;
481
+ };
482
+ ObservableQuery.prototype.tearDownQuery = function () {
483
+ if (this.isTornDown)
484
+ return;
485
+ if (this.concast && this.observer) {
486
+ this.concast.removeObserver(this.observer);
487
+ delete this.concast;
488
+ delete this.observer;
489
+ }
490
+ this.stopPolling();
491
+ this.subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
492
+ this.subscriptions.clear();
493
+ this.queryManager.stopQuery(this.queryId);
494
+ this.observers.clear();
495
+ this.isTornDown = true;
496
+ };
497
+ return ObservableQuery;
498
+ }(utilities.Observable));
499
+ utilities.fixObservableSubclass(ObservableQuery);
500
+ function reobserveCacheFirst(obsQuery) {
501
+ var _a = obsQuery.options, fetchPolicy = _a.fetchPolicy, nextFetchPolicy = _a.nextFetchPolicy;
502
+ if (fetchPolicy === "cache-and-network" ||
503
+ fetchPolicy === "network-only") {
504
+ return obsQuery.reobserve({
505
+ fetchPolicy: "cache-first",
506
+ nextFetchPolicy: function () {
507
+ this.nextFetchPolicy = nextFetchPolicy;
508
+ if (typeof nextFetchPolicy === "function") {
509
+ return nextFetchPolicy.apply(this, arguments);
510
+ }
511
+ return fetchPolicy;
512
+ },
513
+ });
514
+ }
515
+ return obsQuery.reobserve();
516
+ }
517
+ function defaultSubscriptionObserverErrorCallback(error) {
518
+ __DEV__ && globals.invariant.error('Unhandled error', error.message, error.stack);
519
+ }
520
+ function logMissingFieldErrors(missing) {
521
+ if (__DEV__ && missing) {
522
+ __DEV__ && globals.invariant.debug("Missing cache result fields: ".concat(JSON.stringify(missing)), missing);
523
+ }
524
+ }
525
+
526
+ var LocalState = (function () {
527
+ function LocalState(_a) {
528
+ var cache = _a.cache, client = _a.client, resolvers = _a.resolvers, fragmentMatcher = _a.fragmentMatcher;
529
+ this.cache = cache;
530
+ if (client) {
531
+ this.client = client;
532
+ }
533
+ if (resolvers) {
534
+ this.addResolvers(resolvers);
535
+ }
536
+ if (fragmentMatcher) {
537
+ this.setFragmentMatcher(fragmentMatcher);
538
+ }
539
+ }
540
+ LocalState.prototype.addResolvers = function (resolvers) {
541
+ var _this = this;
542
+ this.resolvers = this.resolvers || {};
543
+ if (Array.isArray(resolvers)) {
544
+ resolvers.forEach(function (resolverGroup) {
545
+ _this.resolvers = utilities.mergeDeep(_this.resolvers, resolverGroup);
546
+ });
547
+ }
548
+ else {
549
+ this.resolvers = utilities.mergeDeep(this.resolvers, resolvers);
550
+ }
551
+ };
552
+ LocalState.prototype.setResolvers = function (resolvers) {
553
+ this.resolvers = {};
554
+ this.addResolvers(resolvers);
555
+ };
556
+ LocalState.prototype.getResolvers = function () {
557
+ return this.resolvers || {};
558
+ };
559
+ LocalState.prototype.runResolvers = function (_a) {
560
+ var document = _a.document, remoteResult = _a.remoteResult, context = _a.context, variables = _a.variables, _b = _a.onlyRunForcedResolvers, onlyRunForcedResolvers = _b === void 0 ? false : _b;
561
+ return tslib.__awaiter(this, void 0, void 0, function () {
562
+ return tslib.__generator(this, function (_c) {
563
+ if (document) {
564
+ return [2, this.resolveDocument(document, remoteResult.data, context, variables, this.fragmentMatcher, onlyRunForcedResolvers).then(function (localResult) { return (tslib.__assign(tslib.__assign({}, remoteResult), { data: localResult.result })); })];
565
+ }
566
+ return [2, remoteResult];
567
+ });
568
+ });
569
+ };
570
+ LocalState.prototype.setFragmentMatcher = function (fragmentMatcher) {
571
+ this.fragmentMatcher = fragmentMatcher;
572
+ };
573
+ LocalState.prototype.getFragmentMatcher = function () {
574
+ return this.fragmentMatcher;
575
+ };
576
+ LocalState.prototype.clientQuery = function (document) {
577
+ if (utilities.hasDirectives(['client'], document)) {
578
+ if (this.resolvers) {
579
+ return document;
580
+ }
581
+ }
582
+ return null;
583
+ };
584
+ LocalState.prototype.serverQuery = function (document) {
585
+ return utilities.removeClientSetsFromDocument(document);
586
+ };
587
+ LocalState.prototype.prepareContext = function (context) {
588
+ var cache = this.cache;
589
+ return tslib.__assign(tslib.__assign({}, context), { cache: cache, getCacheKey: function (obj) {
590
+ return cache.identify(obj);
591
+ } });
592
+ };
593
+ LocalState.prototype.addExportedVariables = function (document, variables, context) {
594
+ if (variables === void 0) { variables = {}; }
595
+ if (context === void 0) { context = {}; }
596
+ return tslib.__awaiter(this, void 0, void 0, function () {
597
+ return tslib.__generator(this, function (_a) {
598
+ if (document) {
599
+ return [2, this.resolveDocument(document, this.buildRootValueFromCache(document, variables) || {}, this.prepareContext(context), variables).then(function (data) { return (tslib.__assign(tslib.__assign({}, variables), data.exportedVariables)); })];
600
+ }
601
+ return [2, tslib.__assign({}, variables)];
602
+ });
603
+ });
604
+ };
605
+ LocalState.prototype.shouldForceResolvers = function (document) {
606
+ var forceResolvers = false;
607
+ graphql.visit(document, {
608
+ Directive: {
609
+ enter: function (node) {
610
+ if (node.name.value === 'client' && node.arguments) {
611
+ forceResolvers = node.arguments.some(function (arg) {
612
+ return arg.name.value === 'always' &&
613
+ arg.value.kind === 'BooleanValue' &&
614
+ arg.value.value === true;
615
+ });
616
+ if (forceResolvers) {
617
+ return graphql.BREAK;
618
+ }
619
+ }
620
+ },
621
+ },
622
+ });
623
+ return forceResolvers;
624
+ };
625
+ LocalState.prototype.buildRootValueFromCache = function (document, variables) {
626
+ return this.cache.diff({
627
+ query: utilities.buildQueryFromSelectionSet(document),
628
+ variables: variables,
629
+ returnPartialData: true,
630
+ optimistic: false,
631
+ }).result;
632
+ };
633
+ LocalState.prototype.resolveDocument = function (document, rootValue, context, variables, fragmentMatcher, onlyRunForcedResolvers) {
634
+ if (context === void 0) { context = {}; }
635
+ if (variables === void 0) { variables = {}; }
636
+ if (fragmentMatcher === void 0) { fragmentMatcher = function () { return true; }; }
637
+ if (onlyRunForcedResolvers === void 0) { onlyRunForcedResolvers = false; }
638
+ return tslib.__awaiter(this, void 0, void 0, function () {
639
+ var mainDefinition, fragments, fragmentMap, definitionOperation, defaultOperationType, _a, cache, client, execContext;
640
+ return tslib.__generator(this, function (_b) {
641
+ mainDefinition = utilities.getMainDefinition(document);
642
+ fragments = utilities.getFragmentDefinitions(document);
643
+ fragmentMap = utilities.createFragmentMap(fragments);
644
+ definitionOperation = mainDefinition
645
+ .operation;
646
+ defaultOperationType = definitionOperation
647
+ ? definitionOperation.charAt(0).toUpperCase() +
648
+ definitionOperation.slice(1)
649
+ : 'Query';
650
+ _a = this, cache = _a.cache, client = _a.client;
651
+ execContext = {
652
+ fragmentMap: fragmentMap,
653
+ context: tslib.__assign(tslib.__assign({}, context), { cache: cache, client: client }),
654
+ variables: variables,
655
+ fragmentMatcher: fragmentMatcher,
656
+ defaultOperationType: defaultOperationType,
657
+ exportedVariables: {},
658
+ onlyRunForcedResolvers: onlyRunForcedResolvers,
659
+ };
660
+ return [2, this.resolveSelectionSet(mainDefinition.selectionSet, rootValue, execContext).then(function (result) { return ({
661
+ result: result,
662
+ exportedVariables: execContext.exportedVariables,
663
+ }); })];
664
+ });
665
+ });
666
+ };
667
+ LocalState.prototype.resolveSelectionSet = function (selectionSet, rootValue, execContext) {
668
+ return tslib.__awaiter(this, void 0, void 0, function () {
669
+ var fragmentMap, context, variables, resultsToMerge, execute;
670
+ var _this = this;
671
+ return tslib.__generator(this, function (_a) {
672
+ fragmentMap = execContext.fragmentMap, context = execContext.context, variables = execContext.variables;
673
+ resultsToMerge = [rootValue];
674
+ execute = function (selection) { return tslib.__awaiter(_this, void 0, void 0, function () {
675
+ var fragment, typeCondition;
676
+ return tslib.__generator(this, function (_a) {
677
+ if (!utilities.shouldInclude(selection, variables)) {
678
+ return [2];
679
+ }
680
+ if (utilities.isField(selection)) {
681
+ return [2, this.resolveField(selection, rootValue, execContext).then(function (fieldResult) {
682
+ var _a;
683
+ if (typeof fieldResult !== 'undefined') {
684
+ resultsToMerge.push((_a = {},
685
+ _a[utilities.resultKeyNameFromField(selection)] = fieldResult,
686
+ _a));
687
+ }
688
+ })];
689
+ }
690
+ if (utilities.isInlineFragment(selection)) {
691
+ fragment = selection;
692
+ }
693
+ else {
694
+ fragment = fragmentMap[selection.name.value];
695
+ __DEV__ ? globals.invariant(fragment, "No fragment named ".concat(selection.name.value)) : globals.invariant(fragment, 9);
696
+ }
697
+ if (fragment && fragment.typeCondition) {
698
+ typeCondition = fragment.typeCondition.name.value;
699
+ if (execContext.fragmentMatcher(rootValue, typeCondition, context)) {
700
+ return [2, this.resolveSelectionSet(fragment.selectionSet, rootValue, execContext).then(function (fragmentResult) {
701
+ resultsToMerge.push(fragmentResult);
702
+ })];
703
+ }
704
+ }
705
+ return [2];
706
+ });
707
+ }); };
708
+ return [2, Promise.all(selectionSet.selections.map(execute)).then(function () {
709
+ return utilities.mergeDeepArray(resultsToMerge);
710
+ })];
711
+ });
712
+ });
713
+ };
714
+ LocalState.prototype.resolveField = function (field, rootValue, execContext) {
715
+ return tslib.__awaiter(this, void 0, void 0, function () {
716
+ var variables, fieldName, aliasedFieldName, aliasUsed, defaultResult, resultPromise, resolverType, resolverMap, resolve;
717
+ var _this = this;
718
+ return tslib.__generator(this, function (_a) {
719
+ variables = execContext.variables;
720
+ fieldName = field.name.value;
721
+ aliasedFieldName = utilities.resultKeyNameFromField(field);
722
+ aliasUsed = fieldName !== aliasedFieldName;
723
+ defaultResult = rootValue[aliasedFieldName] || rootValue[fieldName];
724
+ resultPromise = Promise.resolve(defaultResult);
725
+ if (!execContext.onlyRunForcedResolvers ||
726
+ this.shouldForceResolvers(field)) {
727
+ resolverType = rootValue.__typename || execContext.defaultOperationType;
728
+ resolverMap = this.resolvers && this.resolvers[resolverType];
729
+ if (resolverMap) {
730
+ resolve = resolverMap[aliasUsed ? fieldName : aliasedFieldName];
731
+ if (resolve) {
732
+ resultPromise = Promise.resolve(cache.cacheSlot.withValue(this.cache, resolve, [
733
+ rootValue,
734
+ utilities.argumentsObjectFromField(field, variables),
735
+ execContext.context,
736
+ { field: field, fragmentMap: execContext.fragmentMap },
737
+ ]));
738
+ }
739
+ }
740
+ }
741
+ return [2, resultPromise.then(function (result) {
742
+ if (result === void 0) { result = defaultResult; }
743
+ if (field.directives) {
744
+ field.directives.forEach(function (directive) {
745
+ if (directive.name.value === 'export' && directive.arguments) {
746
+ directive.arguments.forEach(function (arg) {
747
+ if (arg.name.value === 'as' && arg.value.kind === 'StringValue') {
748
+ execContext.exportedVariables[arg.value.value] = result;
749
+ }
750
+ });
751
+ }
752
+ });
753
+ }
754
+ if (!field.selectionSet) {
755
+ return result;
756
+ }
757
+ if (result == null) {
758
+ return result;
759
+ }
760
+ if (Array.isArray(result)) {
761
+ return _this.resolveSubSelectedArray(field, result, execContext);
762
+ }
763
+ if (field.selectionSet) {
764
+ return _this.resolveSelectionSet(field.selectionSet, result, execContext);
765
+ }
766
+ })];
767
+ });
768
+ });
769
+ };
770
+ LocalState.prototype.resolveSubSelectedArray = function (field, result, execContext) {
771
+ var _this = this;
772
+ return Promise.all(result.map(function (item) {
773
+ if (item === null) {
774
+ return null;
775
+ }
776
+ if (Array.isArray(item)) {
777
+ return _this.resolveSubSelectedArray(field, item, execContext);
778
+ }
779
+ if (field.selectionSet) {
780
+ return _this.resolveSelectionSet(field.selectionSet, item, execContext);
781
+ }
782
+ }));
783
+ };
784
+ return LocalState;
785
+ }());
786
+
787
+ var destructiveMethodCounts = new (utilities.canUseWeakMap ? WeakMap : Map)();
788
+ function wrapDestructiveCacheMethod(cache, methodName) {
789
+ var original = cache[methodName];
790
+ if (typeof original === "function") {
791
+ cache[methodName] = function () {
792
+ destructiveMethodCounts.set(cache, (destructiveMethodCounts.get(cache) + 1) % 1e15);
793
+ return original.apply(this, arguments);
794
+ };
795
+ }
796
+ }
797
+ function cancelNotifyTimeout(info) {
798
+ if (info["notifyTimeout"]) {
799
+ clearTimeout(info["notifyTimeout"]);
800
+ info["notifyTimeout"] = void 0;
801
+ }
802
+ }
803
+ var QueryInfo = (function () {
804
+ function QueryInfo(queryManager, queryId) {
805
+ if (queryId === void 0) { queryId = queryManager.generateQueryId(); }
806
+ this.queryId = queryId;
807
+ this.listeners = new Set();
808
+ this.document = null;
809
+ this.lastRequestId = 1;
810
+ this.subscriptions = new Set();
811
+ this.stopped = false;
812
+ this.dirty = false;
813
+ this.observableQuery = null;
814
+ var cache = this.cache = queryManager.cache;
815
+ if (!destructiveMethodCounts.has(cache)) {
816
+ destructiveMethodCounts.set(cache, 0);
817
+ wrapDestructiveCacheMethod(cache, "evict");
818
+ wrapDestructiveCacheMethod(cache, "modify");
819
+ wrapDestructiveCacheMethod(cache, "reset");
820
+ }
821
+ }
822
+ QueryInfo.prototype.init = function (query) {
823
+ var networkStatus = query.networkStatus || exports.NetworkStatus.loading;
824
+ if (this.variables &&
825
+ this.networkStatus !== exports.NetworkStatus.loading &&
826
+ !equality.equal(this.variables, query.variables)) {
827
+ networkStatus = exports.NetworkStatus.setVariables;
828
+ }
829
+ if (!equality.equal(query.variables, this.variables)) {
830
+ this.lastDiff = void 0;
831
+ }
832
+ Object.assign(this, {
833
+ document: query.document,
834
+ variables: query.variables,
835
+ networkError: null,
836
+ graphQLErrors: this.graphQLErrors || [],
837
+ networkStatus: networkStatus,
838
+ });
839
+ if (query.observableQuery) {
840
+ this.setObservableQuery(query.observableQuery);
841
+ }
842
+ if (query.lastRequestId) {
843
+ this.lastRequestId = query.lastRequestId;
844
+ }
845
+ return this;
846
+ };
847
+ QueryInfo.prototype.reset = function () {
848
+ cancelNotifyTimeout(this);
849
+ this.lastDiff = void 0;
850
+ this.dirty = false;
851
+ };
852
+ QueryInfo.prototype.getDiff = function (variables) {
853
+ if (variables === void 0) { variables = this.variables; }
854
+ var options = this.getDiffOptions(variables);
855
+ if (this.lastDiff && equality.equal(options, this.lastDiff.options)) {
856
+ return this.lastDiff.diff;
857
+ }
858
+ this.updateWatch(this.variables = variables);
859
+ var oq = this.observableQuery;
860
+ if (oq && oq.options.fetchPolicy === "no-cache") {
861
+ return { complete: false };
862
+ }
863
+ var diff = this.cache.diff(options);
864
+ this.updateLastDiff(diff, options);
865
+ return diff;
866
+ };
867
+ QueryInfo.prototype.updateLastDiff = function (diff, options) {
868
+ this.lastDiff = diff ? {
869
+ diff: diff,
870
+ options: options || this.getDiffOptions(),
871
+ } : void 0;
872
+ };
873
+ QueryInfo.prototype.getDiffOptions = function (variables) {
874
+ var _a;
875
+ if (variables === void 0) { variables = this.variables; }
876
+ return {
877
+ query: this.document,
878
+ variables: variables,
879
+ returnPartialData: true,
880
+ optimistic: true,
881
+ canonizeResults: (_a = this.observableQuery) === null || _a === void 0 ? void 0 : _a.options.canonizeResults,
882
+ };
883
+ };
884
+ QueryInfo.prototype.setDiff = function (diff) {
885
+ var _this = this;
886
+ var oldDiff = this.lastDiff && this.lastDiff.diff;
887
+ this.updateLastDiff(diff);
888
+ if (!this.dirty &&
889
+ !equality.equal(oldDiff && oldDiff.result, diff && diff.result)) {
890
+ this.dirty = true;
891
+ if (!this.notifyTimeout) {
892
+ this.notifyTimeout = setTimeout(function () { return _this.notify(); }, 0);
893
+ }
894
+ }
895
+ };
896
+ QueryInfo.prototype.setObservableQuery = function (oq) {
897
+ var _this = this;
898
+ if (oq === this.observableQuery)
899
+ return;
900
+ if (this.oqListener) {
901
+ this.listeners.delete(this.oqListener);
902
+ }
903
+ this.observableQuery = oq;
904
+ if (oq) {
905
+ oq["queryInfo"] = this;
906
+ this.listeners.add(this.oqListener = function () {
907
+ var diff = _this.getDiff();
908
+ if (diff.fromOptimisticTransaction) {
909
+ oq["observe"]();
910
+ }
911
+ else {
912
+ reobserveCacheFirst(oq);
913
+ }
914
+ });
915
+ }
916
+ else {
917
+ delete this.oqListener;
918
+ }
919
+ };
920
+ QueryInfo.prototype.notify = function () {
921
+ var _this = this;
922
+ cancelNotifyTimeout(this);
923
+ if (this.shouldNotify()) {
924
+ this.listeners.forEach(function (listener) { return listener(_this); });
925
+ }
926
+ this.dirty = false;
927
+ };
928
+ QueryInfo.prototype.shouldNotify = function () {
929
+ if (!this.dirty || !this.listeners.size) {
930
+ return false;
931
+ }
932
+ if (isNetworkRequestInFlight(this.networkStatus) &&
933
+ this.observableQuery) {
934
+ var fetchPolicy = this.observableQuery.options.fetchPolicy;
935
+ if (fetchPolicy !== "cache-only" &&
936
+ fetchPolicy !== "cache-and-network") {
937
+ return false;
938
+ }
939
+ }
940
+ return true;
941
+ };
942
+ QueryInfo.prototype.stop = function () {
943
+ if (!this.stopped) {
944
+ this.stopped = true;
945
+ this.reset();
946
+ this.cancel();
947
+ this.cancel = QueryInfo.prototype.cancel;
948
+ this.subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
949
+ var oq = this.observableQuery;
950
+ if (oq)
951
+ oq.stopPolling();
952
+ }
953
+ };
954
+ QueryInfo.prototype.cancel = function () { };
955
+ QueryInfo.prototype.updateWatch = function (variables) {
956
+ var _this = this;
957
+ if (variables === void 0) { variables = this.variables; }
958
+ var oq = this.observableQuery;
959
+ if (oq && oq.options.fetchPolicy === "no-cache") {
960
+ return;
961
+ }
962
+ var watchOptions = tslib.__assign(tslib.__assign({}, this.getDiffOptions(variables)), { watcher: this, callback: function (diff) { return _this.setDiff(diff); } });
963
+ if (!this.lastWatch ||
964
+ !equality.equal(watchOptions, this.lastWatch)) {
965
+ this.cancel();
966
+ this.cancel = this.cache.watch(this.lastWatch = watchOptions);
967
+ }
968
+ };
969
+ QueryInfo.prototype.resetLastWrite = function () {
970
+ this.lastWrite = void 0;
971
+ };
972
+ QueryInfo.prototype.shouldWrite = function (result, variables) {
973
+ var lastWrite = this.lastWrite;
974
+ return !(lastWrite &&
975
+ lastWrite.dmCount === destructiveMethodCounts.get(this.cache) &&
976
+ equality.equal(variables, lastWrite.variables) &&
977
+ equality.equal(result.data, lastWrite.result.data));
978
+ };
979
+ QueryInfo.prototype.markResult = function (result, options, cacheWriteBehavior) {
980
+ var _this = this;
981
+ this.graphQLErrors = utilities.isNonEmptyArray(result.errors) ? result.errors : [];
982
+ this.reset();
983
+ if (options.fetchPolicy === 'no-cache') {
984
+ this.updateLastDiff({ result: result.data, complete: true }, this.getDiffOptions(options.variables));
985
+ }
986
+ else if (cacheWriteBehavior !== 0) {
987
+ if (shouldWriteResult(result, options.errorPolicy)) {
988
+ this.cache.performTransaction(function (cache) {
989
+ if (_this.shouldWrite(result, options.variables)) {
990
+ cache.writeQuery({
991
+ query: _this.document,
992
+ data: result.data,
993
+ variables: options.variables,
994
+ overwrite: cacheWriteBehavior === 1,
995
+ });
996
+ _this.lastWrite = {
997
+ result: result,
998
+ variables: options.variables,
999
+ dmCount: destructiveMethodCounts.get(_this.cache),
1000
+ };
1001
+ }
1002
+ else {
1003
+ if (_this.lastDiff &&
1004
+ _this.lastDiff.diff.complete) {
1005
+ result.data = _this.lastDiff.diff.result;
1006
+ return;
1007
+ }
1008
+ }
1009
+ var diffOptions = _this.getDiffOptions(options.variables);
1010
+ var diff = cache.diff(diffOptions);
1011
+ if (!_this.stopped) {
1012
+ _this.updateWatch(options.variables);
1013
+ }
1014
+ _this.updateLastDiff(diff, diffOptions);
1015
+ if (diff.complete) {
1016
+ result.data = diff.result;
1017
+ }
1018
+ });
1019
+ }
1020
+ else {
1021
+ this.lastWrite = void 0;
1022
+ }
1023
+ }
1024
+ };
1025
+ QueryInfo.prototype.markReady = function () {
1026
+ this.networkError = null;
1027
+ return this.networkStatus = exports.NetworkStatus.ready;
1028
+ };
1029
+ QueryInfo.prototype.markError = function (error) {
1030
+ this.networkStatus = exports.NetworkStatus.error;
1031
+ this.lastWrite = void 0;
1032
+ this.reset();
1033
+ if (error.graphQLErrors) {
1034
+ this.graphQLErrors = error.graphQLErrors;
1035
+ }
1036
+ if (error.networkError) {
1037
+ this.networkError = error.networkError;
1038
+ }
1039
+ return error;
1040
+ };
1041
+ return QueryInfo;
1042
+ }());
1043
+ function shouldWriteResult(result, errorPolicy) {
1044
+ if (errorPolicy === void 0) { errorPolicy = "none"; }
1045
+ var ignoreErrors = errorPolicy === "ignore" ||
1046
+ errorPolicy === "all";
1047
+ var writeWithErrors = !utilities.graphQLResultHasError(result);
1048
+ if (!writeWithErrors && ignoreErrors && result.data) {
1049
+ writeWithErrors = true;
1050
+ }
1051
+ return writeWithErrors;
1052
+ }
1053
+
1054
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
1055
+ var QueryManager = (function () {
1056
+ function QueryManager(_a) {
1057
+ var cache = _a.cache, link = _a.link, defaultOptions = _a.defaultOptions, _b = _a.queryDeduplication, queryDeduplication = _b === void 0 ? false : _b, onBroadcast = _a.onBroadcast, _c = _a.ssrMode, ssrMode = _c === void 0 ? false : _c, _d = _a.clientAwareness, clientAwareness = _d === void 0 ? {} : _d, localState = _a.localState, assumeImmutableResults = _a.assumeImmutableResults;
1058
+ this.clientAwareness = {};
1059
+ this.queries = new Map();
1060
+ this.fetchCancelFns = new Map();
1061
+ this.transformCache = new (utilities.canUseWeakMap ? WeakMap : Map)();
1062
+ this.queryIdCounter = 1;
1063
+ this.requestIdCounter = 1;
1064
+ this.mutationIdCounter = 1;
1065
+ this.inFlightLinkObservables = new Map();
1066
+ this.cache = cache;
1067
+ this.link = link;
1068
+ this.defaultOptions = defaultOptions || Object.create(null);
1069
+ this.queryDeduplication = queryDeduplication;
1070
+ this.clientAwareness = clientAwareness;
1071
+ this.localState = localState || new LocalState({ cache: cache });
1072
+ this.ssrMode = ssrMode;
1073
+ this.assumeImmutableResults = !!assumeImmutableResults;
1074
+ if ((this.onBroadcast = onBroadcast)) {
1075
+ this.mutationStore = Object.create(null);
1076
+ }
1077
+ }
1078
+ QueryManager.prototype.stop = function () {
1079
+ var _this = this;
1080
+ this.queries.forEach(function (_info, queryId) {
1081
+ _this.stopQueryNoBroadcast(queryId);
1082
+ });
1083
+ this.cancelPendingFetches(__DEV__ ? new globals.InvariantError('QueryManager stopped while query was in flight') : new globals.InvariantError(11));
1084
+ };
1085
+ QueryManager.prototype.cancelPendingFetches = function (error) {
1086
+ this.fetchCancelFns.forEach(function (cancel) { return cancel(error); });
1087
+ this.fetchCancelFns.clear();
1088
+ };
1089
+ QueryManager.prototype.mutate = function (_a) {
1090
+ var _b, _c;
1091
+ var mutation = _a.mutation, variables = _a.variables, optimisticResponse = _a.optimisticResponse, updateQueries = _a.updateQueries, _d = _a.refetchQueries, refetchQueries = _d === void 0 ? [] : _d, _e = _a.awaitRefetchQueries, awaitRefetchQueries = _e === void 0 ? false : _e, updateWithProxyFn = _a.update, onQueryUpdated = _a.onQueryUpdated, _f = _a.fetchPolicy, fetchPolicy = _f === void 0 ? ((_b = this.defaultOptions.mutate) === null || _b === void 0 ? void 0 : _b.fetchPolicy) || "network-only" : _f, _g = _a.errorPolicy, errorPolicy = _g === void 0 ? ((_c = this.defaultOptions.mutate) === null || _c === void 0 ? void 0 : _c.errorPolicy) || "none" : _g, keepRootFields = _a.keepRootFields, context = _a.context;
1092
+ return tslib.__awaiter(this, void 0, void 0, function () {
1093
+ var mutationId, mutationStoreValue, self;
1094
+ return tslib.__generator(this, function (_h) {
1095
+ switch (_h.label) {
1096
+ case 0:
1097
+ __DEV__ ? globals.invariant(mutation, 'mutation option is required. You must specify your GraphQL document in the mutation option.') : globals.invariant(mutation, 12);
1098
+ __DEV__ ? globals.invariant(fetchPolicy === 'network-only' ||
1099
+ fetchPolicy === 'no-cache', "Mutations support only 'network-only' or 'no-cache' fetchPolicy strings. The default `network-only` behavior automatically writes mutation results to the cache. Passing `no-cache` skips the cache write.") : globals.invariant(fetchPolicy === 'network-only' ||
1100
+ fetchPolicy === 'no-cache', 13);
1101
+ mutationId = this.generateMutationId();
1102
+ mutation = this.transform(mutation).document;
1103
+ variables = this.getVariables(mutation, variables);
1104
+ if (!this.transform(mutation).hasClientExports) return [3, 2];
1105
+ return [4, this.localState.addExportedVariables(mutation, variables, context)];
1106
+ case 1:
1107
+ variables = (_h.sent());
1108
+ _h.label = 2;
1109
+ case 2:
1110
+ mutationStoreValue = this.mutationStore &&
1111
+ (this.mutationStore[mutationId] = {
1112
+ mutation: mutation,
1113
+ variables: variables,
1114
+ loading: true,
1115
+ error: null,
1116
+ });
1117
+ if (optimisticResponse) {
1118
+ this.markMutationOptimistic(optimisticResponse, {
1119
+ mutationId: mutationId,
1120
+ document: mutation,
1121
+ variables: variables,
1122
+ fetchPolicy: fetchPolicy,
1123
+ errorPolicy: errorPolicy,
1124
+ context: context,
1125
+ updateQueries: updateQueries,
1126
+ update: updateWithProxyFn,
1127
+ keepRootFields: keepRootFields,
1128
+ });
1129
+ }
1130
+ this.broadcastQueries();
1131
+ self = this;
1132
+ return [2, new Promise(function (resolve, reject) {
1133
+ return utilities.asyncMap(self.getObservableFromLink(mutation, tslib.__assign(tslib.__assign({}, context), { optimisticResponse: optimisticResponse }), variables, false), function (result) {
1134
+ if (utilities.graphQLResultHasError(result) && errorPolicy === 'none') {
1135
+ throw new errors.ApolloError({
1136
+ graphQLErrors: result.errors,
1137
+ });
1138
+ }
1139
+ if (mutationStoreValue) {
1140
+ mutationStoreValue.loading = false;
1141
+ mutationStoreValue.error = null;
1142
+ }
1143
+ var storeResult = tslib.__assign({}, result);
1144
+ if (typeof refetchQueries === "function") {
1145
+ refetchQueries = refetchQueries(storeResult);
1146
+ }
1147
+ if (errorPolicy === 'ignore' &&
1148
+ utilities.graphQLResultHasError(storeResult)) {
1149
+ delete storeResult.errors;
1150
+ }
1151
+ return self.markMutationResult({
1152
+ mutationId: mutationId,
1153
+ result: storeResult,
1154
+ document: mutation,
1155
+ variables: variables,
1156
+ fetchPolicy: fetchPolicy,
1157
+ errorPolicy: errorPolicy,
1158
+ context: context,
1159
+ update: updateWithProxyFn,
1160
+ updateQueries: updateQueries,
1161
+ awaitRefetchQueries: awaitRefetchQueries,
1162
+ refetchQueries: refetchQueries,
1163
+ removeOptimistic: optimisticResponse ? mutationId : void 0,
1164
+ onQueryUpdated: onQueryUpdated,
1165
+ keepRootFields: keepRootFields,
1166
+ });
1167
+ }).subscribe({
1168
+ next: function (storeResult) {
1169
+ self.broadcastQueries();
1170
+ resolve(storeResult);
1171
+ },
1172
+ error: function (err) {
1173
+ if (mutationStoreValue) {
1174
+ mutationStoreValue.loading = false;
1175
+ mutationStoreValue.error = err;
1176
+ }
1177
+ if (optimisticResponse) {
1178
+ self.cache.removeOptimistic(mutationId);
1179
+ }
1180
+ self.broadcastQueries();
1181
+ reject(err instanceof errors.ApolloError ? err : new errors.ApolloError({
1182
+ networkError: err,
1183
+ }));
1184
+ },
1185
+ });
1186
+ })];
1187
+ }
1188
+ });
1189
+ });
1190
+ };
1191
+ QueryManager.prototype.markMutationResult = function (mutation, cache) {
1192
+ var _this = this;
1193
+ if (cache === void 0) { cache = this.cache; }
1194
+ var result = mutation.result;
1195
+ var cacheWrites = [];
1196
+ var skipCache = mutation.fetchPolicy === "no-cache";
1197
+ if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
1198
+ cacheWrites.push({
1199
+ result: result.data,
1200
+ dataId: 'ROOT_MUTATION',
1201
+ query: mutation.document,
1202
+ variables: mutation.variables,
1203
+ });
1204
+ var updateQueries_1 = mutation.updateQueries;
1205
+ if (updateQueries_1) {
1206
+ this.queries.forEach(function (_a, queryId) {
1207
+ var observableQuery = _a.observableQuery;
1208
+ var queryName = observableQuery && observableQuery.queryName;
1209
+ if (!queryName || !hasOwnProperty.call(updateQueries_1, queryName)) {
1210
+ return;
1211
+ }
1212
+ var updater = updateQueries_1[queryName];
1213
+ var _b = _this.queries.get(queryId), document = _b.document, variables = _b.variables;
1214
+ var _c = cache.diff({
1215
+ query: document,
1216
+ variables: variables,
1217
+ returnPartialData: true,
1218
+ optimistic: false,
1219
+ }), currentQueryResult = _c.result, complete = _c.complete;
1220
+ if (complete && currentQueryResult) {
1221
+ var nextQueryResult = updater(currentQueryResult, {
1222
+ mutationResult: result,
1223
+ queryName: document && utilities.getOperationName(document) || void 0,
1224
+ queryVariables: variables,
1225
+ });
1226
+ if (nextQueryResult) {
1227
+ cacheWrites.push({
1228
+ result: nextQueryResult,
1229
+ dataId: 'ROOT_QUERY',
1230
+ query: document,
1231
+ variables: variables,
1232
+ });
1233
+ }
1234
+ }
1235
+ });
1236
+ }
1237
+ }
1238
+ if (cacheWrites.length > 0 ||
1239
+ mutation.refetchQueries ||
1240
+ mutation.update ||
1241
+ mutation.onQueryUpdated ||
1242
+ mutation.removeOptimistic) {
1243
+ var results_1 = [];
1244
+ this.refetchQueries({
1245
+ updateCache: function (cache) {
1246
+ if (!skipCache) {
1247
+ cacheWrites.forEach(function (write) { return cache.write(write); });
1248
+ }
1249
+ var update = mutation.update;
1250
+ if (update) {
1251
+ if (!skipCache) {
1252
+ var diff = cache.diff({
1253
+ id: "ROOT_MUTATION",
1254
+ query: _this.transform(mutation.document).asQuery,
1255
+ variables: mutation.variables,
1256
+ optimistic: false,
1257
+ returnPartialData: true,
1258
+ });
1259
+ if (diff.complete) {
1260
+ result = tslib.__assign(tslib.__assign({}, result), { data: diff.result });
1261
+ }
1262
+ }
1263
+ update(cache, result, {
1264
+ context: mutation.context,
1265
+ variables: mutation.variables,
1266
+ });
1267
+ }
1268
+ if (!skipCache && !mutation.keepRootFields) {
1269
+ cache.modify({
1270
+ id: 'ROOT_MUTATION',
1271
+ fields: function (value, _a) {
1272
+ var fieldName = _a.fieldName, DELETE = _a.DELETE;
1273
+ return fieldName === "__typename" ? value : DELETE;
1274
+ },
1275
+ });
1276
+ }
1277
+ },
1278
+ include: mutation.refetchQueries,
1279
+ optimistic: false,
1280
+ removeOptimistic: mutation.removeOptimistic,
1281
+ onQueryUpdated: mutation.onQueryUpdated || null,
1282
+ }).forEach(function (result) { return results_1.push(result); });
1283
+ if (mutation.awaitRefetchQueries || mutation.onQueryUpdated) {
1284
+ return Promise.all(results_1).then(function () { return result; });
1285
+ }
1286
+ }
1287
+ return Promise.resolve(result);
1288
+ };
1289
+ QueryManager.prototype.markMutationOptimistic = function (optimisticResponse, mutation) {
1290
+ var _this = this;
1291
+ var data = typeof optimisticResponse === "function"
1292
+ ? optimisticResponse(mutation.variables)
1293
+ : optimisticResponse;
1294
+ return this.cache.recordOptimisticTransaction(function (cache) {
1295
+ try {
1296
+ _this.markMutationResult(tslib.__assign(tslib.__assign({}, mutation), { result: { data: data } }), cache);
1297
+ }
1298
+ catch (error) {
1299
+ __DEV__ && globals.invariant.error(error);
1300
+ }
1301
+ }, mutation.mutationId);
1302
+ };
1303
+ QueryManager.prototype.fetchQuery = function (queryId, options, networkStatus) {
1304
+ return this.fetchQueryObservable(queryId, options, networkStatus).promise;
1305
+ };
1306
+ QueryManager.prototype.getQueryStore = function () {
1307
+ var store = Object.create(null);
1308
+ this.queries.forEach(function (info, queryId) {
1309
+ store[queryId] = {
1310
+ variables: info.variables,
1311
+ networkStatus: info.networkStatus,
1312
+ networkError: info.networkError,
1313
+ graphQLErrors: info.graphQLErrors,
1314
+ };
1315
+ });
1316
+ return store;
1317
+ };
1318
+ QueryManager.prototype.resetErrors = function (queryId) {
1319
+ var queryInfo = this.queries.get(queryId);
1320
+ if (queryInfo) {
1321
+ queryInfo.networkError = undefined;
1322
+ queryInfo.graphQLErrors = [];
1323
+ }
1324
+ };
1325
+ QueryManager.prototype.transform = function (document) {
1326
+ var transformCache = this.transformCache;
1327
+ if (!transformCache.has(document)) {
1328
+ var transformed = this.cache.transformDocument(document);
1329
+ var forLink = utilities.removeConnectionDirectiveFromDocument(this.cache.transformForLink(transformed));
1330
+ var clientQuery = this.localState.clientQuery(transformed);
1331
+ var serverQuery = forLink && this.localState.serverQuery(forLink);
1332
+ var cacheEntry_1 = {
1333
+ document: transformed,
1334
+ hasClientExports: utilities.hasClientExports(transformed),
1335
+ hasForcedResolvers: this.localState.shouldForceResolvers(transformed),
1336
+ clientQuery: clientQuery,
1337
+ serverQuery: serverQuery,
1338
+ defaultVars: utilities.getDefaultValues(utilities.getOperationDefinition(transformed)),
1339
+ asQuery: tslib.__assign(tslib.__assign({}, transformed), { definitions: transformed.definitions.map(function (def) {
1340
+ if (def.kind === "OperationDefinition" &&
1341
+ def.operation !== "query") {
1342
+ return tslib.__assign(tslib.__assign({}, def), { operation: "query" });
1343
+ }
1344
+ return def;
1345
+ }) })
1346
+ };
1347
+ var add = function (doc) {
1348
+ if (doc && !transformCache.has(doc)) {
1349
+ transformCache.set(doc, cacheEntry_1);
1350
+ }
1351
+ };
1352
+ add(document);
1353
+ add(transformed);
1354
+ add(clientQuery);
1355
+ add(serverQuery);
1356
+ }
1357
+ return transformCache.get(document);
1358
+ };
1359
+ QueryManager.prototype.getVariables = function (document, variables) {
1360
+ return tslib.__assign(tslib.__assign({}, this.transform(document).defaultVars), variables);
1361
+ };
1362
+ QueryManager.prototype.watchQuery = function (options) {
1363
+ options = tslib.__assign(tslib.__assign({}, options), { variables: this.getVariables(options.query, options.variables) });
1364
+ if (typeof options.notifyOnNetworkStatusChange === 'undefined') {
1365
+ options.notifyOnNetworkStatusChange = false;
1366
+ }
1367
+ var queryInfo = new QueryInfo(this);
1368
+ var observable = new ObservableQuery({
1369
+ queryManager: this,
1370
+ queryInfo: queryInfo,
1371
+ options: options,
1372
+ });
1373
+ this.queries.set(observable.queryId, queryInfo);
1374
+ queryInfo.init({
1375
+ document: observable.query,
1376
+ observableQuery: observable,
1377
+ variables: observable.variables,
1378
+ });
1379
+ return observable;
1380
+ };
1381
+ QueryManager.prototype.query = function (options, queryId) {
1382
+ var _this = this;
1383
+ if (queryId === void 0) { queryId = this.generateQueryId(); }
1384
+ __DEV__ ? globals.invariant(options.query, 'query option is required. You must specify your GraphQL document ' +
1385
+ 'in the query option.') : globals.invariant(options.query, 14);
1386
+ __DEV__ ? globals.invariant(options.query.kind === 'Document', 'You must wrap the query string in a "gql" tag.') : globals.invariant(options.query.kind === 'Document', 15);
1387
+ __DEV__ ? globals.invariant(!options.returnPartialData, 'returnPartialData option only supported on watchQuery.') : globals.invariant(!options.returnPartialData, 16);
1388
+ __DEV__ ? globals.invariant(!options.pollInterval, 'pollInterval option only supported on watchQuery.') : globals.invariant(!options.pollInterval, 17);
1389
+ return this.fetchQuery(queryId, options).finally(function () { return _this.stopQuery(queryId); });
1390
+ };
1391
+ QueryManager.prototype.generateQueryId = function () {
1392
+ return String(this.queryIdCounter++);
1393
+ };
1394
+ QueryManager.prototype.generateRequestId = function () {
1395
+ return this.requestIdCounter++;
1396
+ };
1397
+ QueryManager.prototype.generateMutationId = function () {
1398
+ return String(this.mutationIdCounter++);
1399
+ };
1400
+ QueryManager.prototype.stopQueryInStore = function (queryId) {
1401
+ this.stopQueryInStoreNoBroadcast(queryId);
1402
+ this.broadcastQueries();
1403
+ };
1404
+ QueryManager.prototype.stopQueryInStoreNoBroadcast = function (queryId) {
1405
+ var queryInfo = this.queries.get(queryId);
1406
+ if (queryInfo)
1407
+ queryInfo.stop();
1408
+ };
1409
+ QueryManager.prototype.clearStore = function (options) {
1410
+ if (options === void 0) { options = {
1411
+ discardWatches: true,
1412
+ }; }
1413
+ this.cancelPendingFetches(__DEV__ ? new globals.InvariantError('Store reset while query was in flight (not completed in link chain)') : new globals.InvariantError(18));
1414
+ this.queries.forEach(function (queryInfo) {
1415
+ if (queryInfo.observableQuery) {
1416
+ queryInfo.networkStatus = exports.NetworkStatus.loading;
1417
+ }
1418
+ else {
1419
+ queryInfo.stop();
1420
+ }
1421
+ });
1422
+ if (this.mutationStore) {
1423
+ this.mutationStore = Object.create(null);
1424
+ }
1425
+ return this.cache.reset(options);
1426
+ };
1427
+ QueryManager.prototype.getObservableQueries = function (include) {
1428
+ var _this = this;
1429
+ if (include === void 0) { include = "active"; }
1430
+ var queries = new Map();
1431
+ var queryNamesAndDocs = new Map();
1432
+ var legacyQueryOptions = new Set();
1433
+ if (Array.isArray(include)) {
1434
+ include.forEach(function (desc) {
1435
+ if (typeof desc === "string") {
1436
+ queryNamesAndDocs.set(desc, false);
1437
+ }
1438
+ else if (utilities.isDocumentNode(desc)) {
1439
+ queryNamesAndDocs.set(_this.transform(desc).document, false);
1440
+ }
1441
+ else if (utilities.isNonNullObject(desc) && desc.query) {
1442
+ legacyQueryOptions.add(desc);
1443
+ }
1444
+ });
1445
+ }
1446
+ this.queries.forEach(function (_a, queryId) {
1447
+ var oq = _a.observableQuery, document = _a.document;
1448
+ if (oq) {
1449
+ if (include === "all") {
1450
+ queries.set(queryId, oq);
1451
+ return;
1452
+ }
1453
+ var queryName = oq.queryName, fetchPolicy = oq.options.fetchPolicy;
1454
+ if (fetchPolicy === "standby" ||
1455
+ (include === "active" && !oq.hasObservers())) {
1456
+ return;
1457
+ }
1458
+ if (include === "active" ||
1459
+ (queryName && queryNamesAndDocs.has(queryName)) ||
1460
+ (document && queryNamesAndDocs.has(document))) {
1461
+ queries.set(queryId, oq);
1462
+ if (queryName)
1463
+ queryNamesAndDocs.set(queryName, true);
1464
+ if (document)
1465
+ queryNamesAndDocs.set(document, true);
1466
+ }
1467
+ }
1468
+ });
1469
+ if (legacyQueryOptions.size) {
1470
+ legacyQueryOptions.forEach(function (options) {
1471
+ var queryId = utilities.makeUniqueId("legacyOneTimeQuery");
1472
+ var queryInfo = _this.getQuery(queryId).init({
1473
+ document: options.query,
1474
+ variables: options.variables,
1475
+ });
1476
+ var oq = new ObservableQuery({
1477
+ queryManager: _this,
1478
+ queryInfo: queryInfo,
1479
+ options: tslib.__assign(tslib.__assign({}, options), { fetchPolicy: "network-only" }),
1480
+ });
1481
+ globals.invariant(oq.queryId === queryId);
1482
+ queryInfo.setObservableQuery(oq);
1483
+ queries.set(queryId, oq);
1484
+ });
1485
+ }
1486
+ if (__DEV__ && queryNamesAndDocs.size) {
1487
+ queryNamesAndDocs.forEach(function (included, nameOrDoc) {
1488
+ if (!included) {
1489
+ __DEV__ && globals.invariant.warn("Unknown query ".concat(typeof nameOrDoc === "string" ? "named " : "").concat(JSON.stringify(nameOrDoc, null, 2), " requested in refetchQueries options.include array"));
1490
+ }
1491
+ });
1492
+ }
1493
+ return queries;
1494
+ };
1495
+ QueryManager.prototype.reFetchObservableQueries = function (includeStandby) {
1496
+ var _this = this;
1497
+ if (includeStandby === void 0) { includeStandby = false; }
1498
+ var observableQueryPromises = [];
1499
+ this.getObservableQueries(includeStandby ? "all" : "active").forEach(function (observableQuery, queryId) {
1500
+ var fetchPolicy = observableQuery.options.fetchPolicy;
1501
+ observableQuery.resetLastResults();
1502
+ if (includeStandby ||
1503
+ (fetchPolicy !== "standby" &&
1504
+ fetchPolicy !== "cache-only")) {
1505
+ observableQueryPromises.push(observableQuery.refetch());
1506
+ }
1507
+ _this.getQuery(queryId).setDiff(null);
1508
+ });
1509
+ this.broadcastQueries();
1510
+ return Promise.all(observableQueryPromises);
1511
+ };
1512
+ QueryManager.prototype.setObservableQuery = function (observableQuery) {
1513
+ this.getQuery(observableQuery.queryId).setObservableQuery(observableQuery);
1514
+ };
1515
+ QueryManager.prototype.startGraphQLSubscription = function (_a) {
1516
+ var _this = this;
1517
+ var query = _a.query, fetchPolicy = _a.fetchPolicy, errorPolicy = _a.errorPolicy, variables = _a.variables, _b = _a.context, context = _b === void 0 ? {} : _b;
1518
+ query = this.transform(query).document;
1519
+ variables = this.getVariables(query, variables);
1520
+ var makeObservable = function (variables) {
1521
+ return _this.getObservableFromLink(query, context, variables).map(function (result) {
1522
+ if (fetchPolicy !== 'no-cache') {
1523
+ if (shouldWriteResult(result, errorPolicy)) {
1524
+ _this.cache.write({
1525
+ query: query,
1526
+ result: result.data,
1527
+ dataId: 'ROOT_SUBSCRIPTION',
1528
+ variables: variables,
1529
+ });
1530
+ }
1531
+ _this.broadcastQueries();
1532
+ }
1533
+ if (utilities.graphQLResultHasError(result)) {
1534
+ throw new errors.ApolloError({
1535
+ graphQLErrors: result.errors,
1536
+ });
1537
+ }
1538
+ return result;
1539
+ });
1540
+ };
1541
+ if (this.transform(query).hasClientExports) {
1542
+ var observablePromise_1 = this.localState.addExportedVariables(query, variables, context).then(makeObservable);
1543
+ return new utilities.Observable(function (observer) {
1544
+ var sub = null;
1545
+ observablePromise_1.then(function (observable) { return sub = observable.subscribe(observer); }, observer.error);
1546
+ return function () { return sub && sub.unsubscribe(); };
1547
+ });
1548
+ }
1549
+ return makeObservable(variables);
1550
+ };
1551
+ QueryManager.prototype.stopQuery = function (queryId) {
1552
+ this.stopQueryNoBroadcast(queryId);
1553
+ this.broadcastQueries();
1554
+ };
1555
+ QueryManager.prototype.stopQueryNoBroadcast = function (queryId) {
1556
+ this.stopQueryInStoreNoBroadcast(queryId);
1557
+ this.removeQuery(queryId);
1558
+ };
1559
+ QueryManager.prototype.removeQuery = function (queryId) {
1560
+ this.fetchCancelFns.delete(queryId);
1561
+ if (this.queries.has(queryId)) {
1562
+ this.getQuery(queryId).stop();
1563
+ this.queries.delete(queryId);
1564
+ }
1565
+ };
1566
+ QueryManager.prototype.broadcastQueries = function () {
1567
+ if (this.onBroadcast)
1568
+ this.onBroadcast();
1569
+ this.queries.forEach(function (info) { return info.notify(); });
1570
+ };
1571
+ QueryManager.prototype.getLocalState = function () {
1572
+ return this.localState;
1573
+ };
1574
+ QueryManager.prototype.getObservableFromLink = function (query, context, variables, deduplication) {
1575
+ var _this = this;
1576
+ var _a;
1577
+ if (deduplication === void 0) { deduplication = (_a = context === null || context === void 0 ? void 0 : context.queryDeduplication) !== null && _a !== void 0 ? _a : this.queryDeduplication; }
1578
+ var observable;
1579
+ var serverQuery = this.transform(query).serverQuery;
1580
+ if (serverQuery) {
1581
+ var _b = this, inFlightLinkObservables_1 = _b.inFlightLinkObservables, link = _b.link;
1582
+ var operation = {
1583
+ query: serverQuery,
1584
+ variables: variables,
1585
+ operationName: utilities.getOperationName(serverQuery) || void 0,
1586
+ context: this.prepareContext(tslib.__assign(tslib.__assign({}, context), { forceFetch: !deduplication })),
1587
+ };
1588
+ context = operation.context;
1589
+ if (deduplication) {
1590
+ var byVariables_1 = inFlightLinkObservables_1.get(serverQuery) || new Map();
1591
+ inFlightLinkObservables_1.set(serverQuery, byVariables_1);
1592
+ var varJson_1 = cache.canonicalStringify(variables);
1593
+ observable = byVariables_1.get(varJson_1);
1594
+ if (!observable) {
1595
+ var concast = new utilities.Concast([
1596
+ core.execute(link, operation)
1597
+ ]);
1598
+ byVariables_1.set(varJson_1, observable = concast);
1599
+ concast.cleanup(function () {
1600
+ if (byVariables_1.delete(varJson_1) &&
1601
+ byVariables_1.size < 1) {
1602
+ inFlightLinkObservables_1.delete(serverQuery);
1603
+ }
1604
+ });
1605
+ }
1606
+ }
1607
+ else {
1608
+ observable = new utilities.Concast([
1609
+ core.execute(link, operation)
1610
+ ]);
1611
+ }
1612
+ }
1613
+ else {
1614
+ observable = new utilities.Concast([
1615
+ utilities.Observable.of({ data: {} })
1616
+ ]);
1617
+ context = this.prepareContext(context);
1618
+ }
1619
+ var clientQuery = this.transform(query).clientQuery;
1620
+ if (clientQuery) {
1621
+ observable = utilities.asyncMap(observable, function (result) {
1622
+ return _this.localState.runResolvers({
1623
+ document: clientQuery,
1624
+ remoteResult: result,
1625
+ context: context,
1626
+ variables: variables,
1627
+ });
1628
+ });
1629
+ }
1630
+ return observable;
1631
+ };
1632
+ QueryManager.prototype.getResultsFromLink = function (queryInfo, cacheWriteBehavior, options) {
1633
+ var requestId = queryInfo.lastRequestId = this.generateRequestId();
1634
+ return utilities.asyncMap(this.getObservableFromLink(queryInfo.document, options.context, options.variables), function (result) {
1635
+ var hasErrors = utilities.isNonEmptyArray(result.errors);
1636
+ if (requestId >= queryInfo.lastRequestId) {
1637
+ if (hasErrors && options.errorPolicy === "none") {
1638
+ throw queryInfo.markError(new errors.ApolloError({
1639
+ graphQLErrors: result.errors,
1640
+ }));
1641
+ }
1642
+ queryInfo.markResult(result, options, cacheWriteBehavior);
1643
+ queryInfo.markReady();
1644
+ }
1645
+ var aqr = {
1646
+ data: result.data,
1647
+ loading: false,
1648
+ networkStatus: exports.NetworkStatus.ready,
1649
+ };
1650
+ if (hasErrors && options.errorPolicy !== "ignore") {
1651
+ aqr.errors = result.errors;
1652
+ aqr.networkStatus = exports.NetworkStatus.error;
1653
+ }
1654
+ return aqr;
1655
+ }, function (networkError) {
1656
+ var error = errors.isApolloError(networkError)
1657
+ ? networkError
1658
+ : new errors.ApolloError({ networkError: networkError });
1659
+ if (requestId >= queryInfo.lastRequestId) {
1660
+ queryInfo.markError(error);
1661
+ }
1662
+ throw error;
1663
+ });
1664
+ };
1665
+ QueryManager.prototype.fetchQueryObservable = function (queryId, options, networkStatus) {
1666
+ var _this = this;
1667
+ if (networkStatus === void 0) { networkStatus = exports.NetworkStatus.loading; }
1668
+ var query = this.transform(options.query).document;
1669
+ var variables = this.getVariables(query, options.variables);
1670
+ var queryInfo = this.getQuery(queryId);
1671
+ var defaults = this.defaultOptions.watchQuery;
1672
+ var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? defaults && defaults.fetchPolicy || "cache-first" : _a, _b = options.errorPolicy, errorPolicy = _b === void 0 ? defaults && defaults.errorPolicy || "none" : _b, _c = options.returnPartialData, returnPartialData = _c === void 0 ? false : _c, _d = options.notifyOnNetworkStatusChange, notifyOnNetworkStatusChange = _d === void 0 ? false : _d, _e = options.context, context = _e === void 0 ? {} : _e;
1673
+ var normalized = Object.assign({}, options, {
1674
+ query: query,
1675
+ variables: variables,
1676
+ fetchPolicy: fetchPolicy,
1677
+ errorPolicy: errorPolicy,
1678
+ returnPartialData: returnPartialData,
1679
+ notifyOnNetworkStatusChange: notifyOnNetworkStatusChange,
1680
+ context: context,
1681
+ });
1682
+ var fromVariables = function (variables) {
1683
+ normalized.variables = variables;
1684
+ var concastSources = _this.fetchQueryByPolicy(queryInfo, normalized, networkStatus);
1685
+ if (normalized.fetchPolicy !== "standby" &&
1686
+ concastSources.length > 0 &&
1687
+ queryInfo.observableQuery) {
1688
+ queryInfo.observableQuery["applyNextFetchPolicy"]("after-fetch", options);
1689
+ }
1690
+ return concastSources;
1691
+ };
1692
+ var cleanupCancelFn = function () { return _this.fetchCancelFns.delete(queryId); };
1693
+ this.fetchCancelFns.set(queryId, function (reason) {
1694
+ cleanupCancelFn();
1695
+ setTimeout(function () { return concast.cancel(reason); });
1696
+ });
1697
+ var concast = new utilities.Concast(this.transform(normalized.query).hasClientExports
1698
+ ? this.localState.addExportedVariables(normalized.query, normalized.variables, normalized.context).then(fromVariables)
1699
+ : fromVariables(normalized.variables));
1700
+ concast.promise.then(cleanupCancelFn, cleanupCancelFn);
1701
+ return concast;
1702
+ };
1703
+ QueryManager.prototype.refetchQueries = function (_a) {
1704
+ var _this = this;
1705
+ var updateCache = _a.updateCache, include = _a.include, _b = _a.optimistic, optimistic = _b === void 0 ? false : _b, _c = _a.removeOptimistic, removeOptimistic = _c === void 0 ? optimistic ? utilities.makeUniqueId("refetchQueries") : void 0 : _c, onQueryUpdated = _a.onQueryUpdated;
1706
+ var includedQueriesById = new Map();
1707
+ if (include) {
1708
+ this.getObservableQueries(include).forEach(function (oq, queryId) {
1709
+ includedQueriesById.set(queryId, {
1710
+ oq: oq,
1711
+ lastDiff: _this.getQuery(queryId).getDiff(),
1712
+ });
1713
+ });
1714
+ }
1715
+ var results = new Map;
1716
+ if (updateCache) {
1717
+ this.cache.batch({
1718
+ update: updateCache,
1719
+ optimistic: optimistic && removeOptimistic || false,
1720
+ removeOptimistic: removeOptimistic,
1721
+ onWatchUpdated: function (watch, diff, lastDiff) {
1722
+ var oq = watch.watcher instanceof QueryInfo &&
1723
+ watch.watcher.observableQuery;
1724
+ if (oq) {
1725
+ if (onQueryUpdated) {
1726
+ includedQueriesById.delete(oq.queryId);
1727
+ var result = onQueryUpdated(oq, diff, lastDiff);
1728
+ if (result === true) {
1729
+ result = oq.refetch();
1730
+ }
1731
+ if (result !== false) {
1732
+ results.set(oq, result);
1733
+ }
1734
+ return result;
1735
+ }
1736
+ if (onQueryUpdated !== null) {
1737
+ includedQueriesById.set(oq.queryId, { oq: oq, lastDiff: lastDiff, diff: diff });
1738
+ }
1739
+ }
1740
+ },
1741
+ });
1742
+ }
1743
+ if (includedQueriesById.size) {
1744
+ includedQueriesById.forEach(function (_a, queryId) {
1745
+ var oq = _a.oq, lastDiff = _a.lastDiff, diff = _a.diff;
1746
+ var result;
1747
+ if (onQueryUpdated) {
1748
+ if (!diff) {
1749
+ var info = oq["queryInfo"];
1750
+ info.reset();
1751
+ diff = info.getDiff();
1752
+ }
1753
+ result = onQueryUpdated(oq, diff, lastDiff);
1754
+ }
1755
+ if (!onQueryUpdated || result === true) {
1756
+ result = oq.refetch();
1757
+ }
1758
+ if (result !== false) {
1759
+ results.set(oq, result);
1760
+ }
1761
+ if (queryId.indexOf("legacyOneTimeQuery") >= 0) {
1762
+ _this.stopQueryNoBroadcast(queryId);
1763
+ }
1764
+ });
1765
+ }
1766
+ if (removeOptimistic) {
1767
+ this.cache.removeOptimistic(removeOptimistic);
1768
+ }
1769
+ return results;
1770
+ };
1771
+ QueryManager.prototype.fetchQueryByPolicy = function (queryInfo, _a, networkStatus) {
1772
+ var _this = this;
1773
+ var query = _a.query, variables = _a.variables, fetchPolicy = _a.fetchPolicy, refetchWritePolicy = _a.refetchWritePolicy, errorPolicy = _a.errorPolicy, returnPartialData = _a.returnPartialData, context = _a.context, notifyOnNetworkStatusChange = _a.notifyOnNetworkStatusChange;
1774
+ var oldNetworkStatus = queryInfo.networkStatus;
1775
+ queryInfo.init({
1776
+ document: this.transform(query).document,
1777
+ variables: variables,
1778
+ networkStatus: networkStatus,
1779
+ });
1780
+ var readCache = function () { return queryInfo.getDiff(variables); };
1781
+ var resultsFromCache = function (diff, networkStatus) {
1782
+ if (networkStatus === void 0) { networkStatus = queryInfo.networkStatus || exports.NetworkStatus.loading; }
1783
+ var data = diff.result;
1784
+ if (__DEV__ &&
1785
+ !returnPartialData &&
1786
+ !equality.equal(data, {})) {
1787
+ logMissingFieldErrors(diff.missing);
1788
+ }
1789
+ var fromData = function (data) { return utilities.Observable.of(tslib.__assign({ data: data, loading: isNetworkRequestInFlight(networkStatus), networkStatus: networkStatus }, (diff.complete ? null : { partial: true }))); };
1790
+ if (data && _this.transform(query).hasForcedResolvers) {
1791
+ return _this.localState.runResolvers({
1792
+ document: query,
1793
+ remoteResult: { data: data },
1794
+ context: context,
1795
+ variables: variables,
1796
+ onlyRunForcedResolvers: true,
1797
+ }).then(function (resolved) { return fromData(resolved.data || void 0); });
1798
+ }
1799
+ return fromData(data);
1800
+ };
1801
+ var cacheWriteBehavior = fetchPolicy === "no-cache" ? 0 :
1802
+ (networkStatus === exports.NetworkStatus.refetch &&
1803
+ refetchWritePolicy !== "merge") ? 1
1804
+ : 2;
1805
+ var resultsFromLink = function () { return _this.getResultsFromLink(queryInfo, cacheWriteBehavior, {
1806
+ variables: variables,
1807
+ context: context,
1808
+ fetchPolicy: fetchPolicy,
1809
+ errorPolicy: errorPolicy,
1810
+ }); };
1811
+ var shouldNotify = notifyOnNetworkStatusChange &&
1812
+ typeof oldNetworkStatus === "number" &&
1813
+ oldNetworkStatus !== networkStatus &&
1814
+ isNetworkRequestInFlight(networkStatus);
1815
+ switch (fetchPolicy) {
1816
+ default:
1817
+ case "cache-first": {
1818
+ var diff = readCache();
1819
+ if (diff.complete) {
1820
+ return [
1821
+ resultsFromCache(diff, queryInfo.markReady()),
1822
+ ];
1823
+ }
1824
+ if (returnPartialData || shouldNotify) {
1825
+ return [
1826
+ resultsFromCache(diff),
1827
+ resultsFromLink(),
1828
+ ];
1829
+ }
1830
+ return [
1831
+ resultsFromLink(),
1832
+ ];
1833
+ }
1834
+ case "cache-and-network": {
1835
+ var diff = readCache();
1836
+ if (diff.complete || returnPartialData || shouldNotify) {
1837
+ return [
1838
+ resultsFromCache(diff),
1839
+ resultsFromLink(),
1840
+ ];
1841
+ }
1842
+ return [
1843
+ resultsFromLink(),
1844
+ ];
1845
+ }
1846
+ case "cache-only":
1847
+ return [
1848
+ resultsFromCache(readCache(), queryInfo.markReady()),
1849
+ ];
1850
+ case "network-only":
1851
+ if (shouldNotify) {
1852
+ return [
1853
+ resultsFromCache(readCache()),
1854
+ resultsFromLink(),
1855
+ ];
1856
+ }
1857
+ return [resultsFromLink()];
1858
+ case "no-cache":
1859
+ if (shouldNotify) {
1860
+ return [
1861
+ resultsFromCache(queryInfo.getDiff()),
1862
+ resultsFromLink(),
1863
+ ];
1864
+ }
1865
+ return [resultsFromLink()];
1866
+ case "standby":
1867
+ return [];
1868
+ }
1869
+ };
1870
+ QueryManager.prototype.getQuery = function (queryId) {
1871
+ if (queryId && !this.queries.has(queryId)) {
1872
+ this.queries.set(queryId, new QueryInfo(this, queryId));
1873
+ }
1874
+ return this.queries.get(queryId);
1875
+ };
1876
+ QueryManager.prototype.prepareContext = function (context) {
1877
+ if (context === void 0) { context = {}; }
1878
+ var newContext = this.localState.prepareContext(context);
1879
+ return tslib.__assign(tslib.__assign({}, newContext), { clientAwareness: this.clientAwareness });
1880
+ };
1881
+ return QueryManager;
1882
+ }());
1883
+
1884
+ var hasSuggestedDevtools = false;
1885
+ var ApolloClient = (function () {
1886
+ function ApolloClient(options) {
1887
+ var _this = this;
1888
+ this.resetStoreCallbacks = [];
1889
+ this.clearStoreCallbacks = [];
1890
+ var uri = options.uri, credentials = options.credentials, headers = options.headers, cache = options.cache, _a = options.ssrMode, ssrMode = _a === void 0 ? false : _a, _b = options.ssrForceFetchDelay, ssrForceFetchDelay = _b === void 0 ? 0 : _b, _c = options.connectToDevTools, connectToDevTools = _c === void 0 ? typeof window === 'object' &&
1891
+ !window.__APOLLO_CLIENT__ &&
1892
+ __DEV__ : _c, _d = options.queryDeduplication, queryDeduplication = _d === void 0 ? true : _d, defaultOptions = options.defaultOptions, _e = options.assumeImmutableResults, assumeImmutableResults = _e === void 0 ? false : _e, resolvers = options.resolvers, typeDefs = options.typeDefs, fragmentMatcher = options.fragmentMatcher, clientAwarenessName = options.name, clientAwarenessVersion = options.version;
1893
+ var link = options.link;
1894
+ if (!link) {
1895
+ link = uri
1896
+ ? new http.HttpLink({ uri: uri, credentials: credentials, headers: headers })
1897
+ : core.ApolloLink.empty();
1898
+ }
1899
+ if (!cache) {
1900
+ throw __DEV__ ? new globals.InvariantError("To initialize Apollo Client, you must specify a 'cache' property " +
1901
+ "in the options object. \n" +
1902
+ "For more information, please visit: https://go.apollo.dev/c/docs") : new globals.InvariantError(7);
1903
+ }
1904
+ this.link = link;
1905
+ this.cache = cache;
1906
+ this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
1907
+ this.queryDeduplication = queryDeduplication;
1908
+ this.defaultOptions = defaultOptions || Object.create(null);
1909
+ this.typeDefs = typeDefs;
1910
+ if (ssrForceFetchDelay) {
1911
+ setTimeout(function () { return (_this.disableNetworkFetches = false); }, ssrForceFetchDelay);
1912
+ }
1913
+ this.watchQuery = this.watchQuery.bind(this);
1914
+ this.query = this.query.bind(this);
1915
+ this.mutate = this.mutate.bind(this);
1916
+ this.resetStore = this.resetStore.bind(this);
1917
+ this.reFetchObservableQueries = this.reFetchObservableQueries.bind(this);
1918
+ if (connectToDevTools && typeof window === 'object') {
1919
+ window.__APOLLO_CLIENT__ = this;
1920
+ }
1921
+ if (!hasSuggestedDevtools && __DEV__) {
1922
+ hasSuggestedDevtools = true;
1923
+ if (typeof window !== 'undefined' &&
1924
+ window.document &&
1925
+ window.top === window.self &&
1926
+ !window.__APOLLO_DEVTOOLS_GLOBAL_HOOK__) {
1927
+ var nav = window.navigator;
1928
+ var ua = nav && nav.userAgent;
1929
+ var url = void 0;
1930
+ if (typeof ua === "string") {
1931
+ if (ua.indexOf("Chrome/") > -1) {
1932
+ url = "https://chrome.google.com/webstore/detail/" +
1933
+ "apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm";
1934
+ }
1935
+ else if (ua.indexOf("Firefox/") > -1) {
1936
+ url = "https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/";
1937
+ }
1938
+ }
1939
+ if (url) {
1940
+ __DEV__ && globals.invariant.log("Download the Apollo DevTools for a better development " +
1941
+ "experience: " + url);
1942
+ }
1943
+ }
1944
+ }
1945
+ this.version = version;
1946
+ this.localState = new LocalState({
1947
+ cache: cache,
1948
+ client: this,
1949
+ resolvers: resolvers,
1950
+ fragmentMatcher: fragmentMatcher,
1951
+ });
1952
+ this.queryManager = new QueryManager({
1953
+ cache: this.cache,
1954
+ link: this.link,
1955
+ defaultOptions: this.defaultOptions,
1956
+ queryDeduplication: queryDeduplication,
1957
+ ssrMode: ssrMode,
1958
+ clientAwareness: {
1959
+ name: clientAwarenessName,
1960
+ version: clientAwarenessVersion,
1961
+ },
1962
+ localState: this.localState,
1963
+ assumeImmutableResults: assumeImmutableResults,
1964
+ onBroadcast: connectToDevTools ? function () {
1965
+ if (_this.devToolsHookCb) {
1966
+ _this.devToolsHookCb({
1967
+ action: {},
1968
+ state: {
1969
+ queries: _this.queryManager.getQueryStore(),
1970
+ mutations: _this.queryManager.mutationStore || {},
1971
+ },
1972
+ dataWithOptimisticResults: _this.cache.extract(true),
1973
+ });
1974
+ }
1975
+ } : void 0,
1976
+ });
1977
+ }
1978
+ ApolloClient.prototype.stop = function () {
1979
+ this.queryManager.stop();
1980
+ };
1981
+ ApolloClient.prototype.watchQuery = function (options) {
1982
+ if (this.defaultOptions.watchQuery) {
1983
+ options = utilities.mergeOptions(this.defaultOptions.watchQuery, options);
1984
+ }
1985
+ if (this.disableNetworkFetches &&
1986
+ (options.fetchPolicy === 'network-only' ||
1987
+ options.fetchPolicy === 'cache-and-network')) {
1988
+ options = tslib.__assign(tslib.__assign({}, options), { fetchPolicy: 'cache-first' });
1989
+ }
1990
+ return this.queryManager.watchQuery(options);
1991
+ };
1992
+ ApolloClient.prototype.query = function (options) {
1993
+ if (this.defaultOptions.query) {
1994
+ options = utilities.mergeOptions(this.defaultOptions.query, options);
1995
+ }
1996
+ __DEV__ ? globals.invariant(options.fetchPolicy !== 'cache-and-network', 'The cache-and-network fetchPolicy does not work with client.query, because ' +
1997
+ 'client.query can only return a single result. Please use client.watchQuery ' +
1998
+ 'to receive multiple results from the cache and the network, or consider ' +
1999
+ 'using a different fetchPolicy, such as cache-first or network-only.') : globals.invariant(options.fetchPolicy !== 'cache-and-network', 8);
2000
+ if (this.disableNetworkFetches && options.fetchPolicy === 'network-only') {
2001
+ options = tslib.__assign(tslib.__assign({}, options), { fetchPolicy: 'cache-first' });
2002
+ }
2003
+ return this.queryManager.query(options);
2004
+ };
2005
+ ApolloClient.prototype.mutate = function (options) {
2006
+ if (this.defaultOptions.mutate) {
2007
+ options = utilities.mergeOptions(this.defaultOptions.mutate, options);
2008
+ }
2009
+ return this.queryManager.mutate(options);
2010
+ };
2011
+ ApolloClient.prototype.subscribe = function (options) {
2012
+ return this.queryManager.startGraphQLSubscription(options);
2013
+ };
2014
+ ApolloClient.prototype.readQuery = function (options, optimistic) {
2015
+ if (optimistic === void 0) { optimistic = false; }
2016
+ return this.cache.readQuery(options, optimistic);
2017
+ };
2018
+ ApolloClient.prototype.readFragment = function (options, optimistic) {
2019
+ if (optimistic === void 0) { optimistic = false; }
2020
+ return this.cache.readFragment(options, optimistic);
2021
+ };
2022
+ ApolloClient.prototype.writeQuery = function (options) {
2023
+ this.cache.writeQuery(options);
2024
+ this.queryManager.broadcastQueries();
2025
+ };
2026
+ ApolloClient.prototype.writeFragment = function (options) {
2027
+ this.cache.writeFragment(options);
2028
+ this.queryManager.broadcastQueries();
2029
+ };
2030
+ ApolloClient.prototype.__actionHookForDevTools = function (cb) {
2031
+ this.devToolsHookCb = cb;
2032
+ };
2033
+ ApolloClient.prototype.__requestRaw = function (payload) {
2034
+ return core.execute(this.link, payload);
2035
+ };
2036
+ ApolloClient.prototype.resetStore = function () {
2037
+ var _this = this;
2038
+ return Promise.resolve()
2039
+ .then(function () { return _this.queryManager.clearStore({
2040
+ discardWatches: false,
2041
+ }); })
2042
+ .then(function () { return Promise.all(_this.resetStoreCallbacks.map(function (fn) { return fn(); })); })
2043
+ .then(function () { return _this.reFetchObservableQueries(); });
2044
+ };
2045
+ ApolloClient.prototype.clearStore = function () {
2046
+ var _this = this;
2047
+ return Promise.resolve()
2048
+ .then(function () { return _this.queryManager.clearStore({
2049
+ discardWatches: true,
2050
+ }); })
2051
+ .then(function () { return Promise.all(_this.clearStoreCallbacks.map(function (fn) { return fn(); })); });
2052
+ };
2053
+ ApolloClient.prototype.onResetStore = function (cb) {
2054
+ var _this = this;
2055
+ this.resetStoreCallbacks.push(cb);
2056
+ return function () {
2057
+ _this.resetStoreCallbacks = _this.resetStoreCallbacks.filter(function (c) { return c !== cb; });
2058
+ };
2059
+ };
2060
+ ApolloClient.prototype.onClearStore = function (cb) {
2061
+ var _this = this;
2062
+ this.clearStoreCallbacks.push(cb);
2063
+ return function () {
2064
+ _this.clearStoreCallbacks = _this.clearStoreCallbacks.filter(function (c) { return c !== cb; });
2065
+ };
2066
+ };
2067
+ ApolloClient.prototype.reFetchObservableQueries = function (includeStandby) {
2068
+ return this.queryManager.reFetchObservableQueries(includeStandby);
2069
+ };
2070
+ ApolloClient.prototype.refetchQueries = function (options) {
2071
+ var map = this.queryManager.refetchQueries(options);
2072
+ var queries = [];
2073
+ var results = [];
2074
+ map.forEach(function (result, obsQuery) {
2075
+ queries.push(obsQuery);
2076
+ results.push(result);
2077
+ });
2078
+ var result = Promise.all(results);
2079
+ result.queries = queries;
2080
+ result.results = results;
2081
+ result.catch(function (error) {
2082
+ __DEV__ && globals.invariant.debug("In client.refetchQueries, Promise.all promise rejected with error ".concat(error));
2083
+ });
2084
+ return result;
2085
+ };
2086
+ ApolloClient.prototype.getObservableQueries = function (include) {
2087
+ if (include === void 0) { include = "active"; }
2088
+ return this.queryManager.getObservableQueries(include);
2089
+ };
2090
+ ApolloClient.prototype.extract = function (optimistic) {
2091
+ return this.cache.extract(optimistic);
2092
+ };
2093
+ ApolloClient.prototype.restore = function (serializedState) {
2094
+ return this.cache.restore(serializedState);
2095
+ };
2096
+ ApolloClient.prototype.addResolvers = function (resolvers) {
2097
+ this.localState.addResolvers(resolvers);
2098
+ };
2099
+ ApolloClient.prototype.setResolvers = function (resolvers) {
2100
+ this.localState.setResolvers(resolvers);
2101
+ };
2102
+ ApolloClient.prototype.getResolvers = function () {
2103
+ return this.localState.getResolvers();
2104
+ };
2105
+ ApolloClient.prototype.setLocalStateFragmentMatcher = function (fragmentMatcher) {
2106
+ this.localState.setFragmentMatcher(fragmentMatcher);
2107
+ };
2108
+ ApolloClient.prototype.setLink = function (newLink) {
2109
+ this.link = this.queryManager.link = newLink;
2110
+ };
2111
+ return ApolloClient;
2112
+ }());
2113
+
2114
+ tsInvariant.setVerbosity(globals.DEV ? "log" : "silent");
2115
+
2116
+ exports.ApolloCache = cache.ApolloCache;
2117
+ exports.Cache = cache.Cache;
2118
+ exports.InMemoryCache = cache.InMemoryCache;
2119
+ exports.MissingFieldError = cache.MissingFieldError;
2120
+ exports.defaultDataIdFromObject = cache.defaultDataIdFromObject;
2121
+ exports.makeVar = cache.makeVar;
2122
+ exports.Observable = utilities.Observable;
2123
+ exports.isReference = utilities.isReference;
2124
+ exports.makeReference = utilities.makeReference;
2125
+ exports.mergeOptions = utilities.mergeOptions;
2126
+ exports.ApolloError = errors.ApolloError;
2127
+ exports.isApolloError = errors.isApolloError;
2128
+ exports.fromError = utils.fromError;
2129
+ exports.fromPromise = utils.fromPromise;
2130
+ exports.throwServerError = utils.throwServerError;
2131
+ exports.toPromise = utils.toPromise;
2132
+ exports.setLogVerbosity = tsInvariant.setVerbosity;
2133
+ exports.disableExperimentalFragmentVariables = graphqlTag.disableExperimentalFragmentVariables;
2134
+ exports.disableFragmentWarnings = graphqlTag.disableFragmentWarnings;
2135
+ exports.enableExperimentalFragmentVariables = graphqlTag.enableExperimentalFragmentVariables;
2136
+ exports.gql = graphqlTag.gql;
2137
+ exports.resetCaches = graphqlTag.resetCaches;
2138
+ exports.ApolloClient = ApolloClient;
2139
+ exports.ObservableQuery = ObservableQuery;
2140
+ for (var k in core) {
2141
+ if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = core[k];
2142
+ }
2143
+ for (var k in http) {
2144
+ if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = http[k];
2145
+ }
2146
+ //# sourceMappingURL=core.cjs.map