@apollo/client 3.7.1 → 3.7.3

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 (45) hide show
  1. package/README.md +5 -5
  2. package/apollo-client.cjs +105 -50
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/cache/core/types/common.d.ts +3 -2
  6. package/cache/core/types/common.d.ts.map +1 -1
  7. package/cache/core/types/common.js.map +1 -1
  8. package/cache/index.d.ts +1 -1
  9. package/cache/index.d.ts.map +1 -1
  10. package/cache/index.js.map +1 -1
  11. package/core/ApolloClient.js +1 -1
  12. package/core/ApolloClient.js.map +1 -1
  13. package/core/QueryInfo.d.ts.map +1 -1
  14. package/core/QueryInfo.js +8 -17
  15. package/core/QueryInfo.js.map +1 -1
  16. package/core/QueryManager.d.ts.map +1 -1
  17. package/core/QueryManager.js +52 -28
  18. package/core/QueryManager.js.map +1 -1
  19. package/core/core.cjs +148 -46
  20. package/core/core.cjs.map +1 -1
  21. package/core/core.cjs.native.js +148 -46
  22. package/errors/errors.cjs +1 -0
  23. package/errors/errors.cjs.map +1 -1
  24. package/errors/errors.cjs.native.js +1 -0
  25. package/errors/index.d.ts +1 -0
  26. package/errors/index.d.ts.map +1 -1
  27. package/errors/index.js +1 -0
  28. package/errors/index.js.map +1 -1
  29. package/invariantErrorCodes.js +1 -1
  30. package/package.json +17 -15
  31. package/react/hooks/useMutation.d.ts +1 -1
  32. package/react/hooks/useMutation.d.ts.map +1 -1
  33. package/react/hooks/useMutation.js.map +1 -1
  34. package/utilities/common/errorHandling.d.ts +3 -2
  35. package/utilities/common/errorHandling.d.ts.map +1 -1
  36. package/utilities/common/errorHandling.js +18 -1
  37. package/utilities/common/errorHandling.js.map +1 -1
  38. package/utilities/common/incrementalResult.d.ts +5 -2
  39. package/utilities/common/incrementalResult.d.ts.map +1 -1
  40. package/utilities/common/incrementalResult.js +29 -1
  41. package/utilities/common/incrementalResult.js.map +1 -1
  42. package/utilities/utilities.cjs +21 -1
  43. package/utilities/utilities.cjs.map +1 -1
  44. package/utilities/utilities.cjs.native.js +21 -1
  45. package/version.js +1 -1
package/core/core.cjs CHANGED
@@ -15,10 +15,99 @@ var utils = require('../link/utils');
15
15
  var tsInvariant = require('ts-invariant');
16
16
  var graphqlTag = require('graphql-tag');
17
17
 
18
- var version = '3.7.1';
18
+ var version = '3.7.3';
19
+
20
+ function isNonEmptyArray(value) {
21
+ return Array.isArray(value) && value.length > 0;
22
+ }
23
+
24
+ function isNonNullObject(obj) {
25
+ return obj !== null && typeof obj === 'object';
26
+ }
27
+
28
+ var hasOwnProperty$2 = Object.prototype.hasOwnProperty;
29
+ var defaultReconciler = function (target, source, property) {
30
+ return this.merge(target[property], source[property]);
31
+ };
32
+ var DeepMerger = (function () {
33
+ function DeepMerger(reconciler) {
34
+ if (reconciler === void 0) { reconciler = defaultReconciler; }
35
+ this.reconciler = reconciler;
36
+ this.isObject = isNonNullObject;
37
+ this.pastCopies = new Set();
38
+ }
39
+ DeepMerger.prototype.merge = function (target, source) {
40
+ var _this = this;
41
+ var context = [];
42
+ for (var _i = 2; _i < arguments.length; _i++) {
43
+ context[_i - 2] = arguments[_i];
44
+ }
45
+ if (isNonNullObject(source) && isNonNullObject(target)) {
46
+ Object.keys(source).forEach(function (sourceKey) {
47
+ if (hasOwnProperty$2.call(target, sourceKey)) {
48
+ var targetValue = target[sourceKey];
49
+ if (source[sourceKey] !== targetValue) {
50
+ var result = _this.reconciler.apply(_this, tslib.__spreadArray([target, source, sourceKey], context, false));
51
+ if (result !== targetValue) {
52
+ target = _this.shallowCopyForMerge(target);
53
+ target[sourceKey] = result;
54
+ }
55
+ }
56
+ }
57
+ else {
58
+ target = _this.shallowCopyForMerge(target);
59
+ target[sourceKey] = source[sourceKey];
60
+ }
61
+ });
62
+ return target;
63
+ }
64
+ return source;
65
+ };
66
+ DeepMerger.prototype.shallowCopyForMerge = function (value) {
67
+ if (isNonNullObject(value)) {
68
+ if (!this.pastCopies.has(value)) {
69
+ if (Array.isArray(value)) {
70
+ value = value.slice(0);
71
+ }
72
+ else {
73
+ value = tslib.__assign({ __proto__: Object.getPrototypeOf(value) }, value);
74
+ }
75
+ this.pastCopies.add(value);
76
+ }
77
+ }
78
+ return value;
79
+ };
80
+ return DeepMerger;
81
+ }());
19
82
 
20
83
  function isExecutionPatchIncrementalResult(value) {
21
- return !!value.incremental;
84
+ return "incremental" in value;
85
+ }
86
+ function isExecutionPatchInitialResult(value) {
87
+ return "hasNext" in value && "data" in value;
88
+ }
89
+ function isExecutionPatchResult(value) {
90
+ return (isExecutionPatchIncrementalResult(value) ||
91
+ isExecutionPatchInitialResult(value));
92
+ }
93
+ function mergeIncrementalData(prevResult, result) {
94
+ var mergedData = prevResult;
95
+ var merger = new DeepMerger();
96
+ if (isExecutionPatchIncrementalResult(result) &&
97
+ isNonEmptyArray(result.incremental)) {
98
+ result.incremental.forEach(function (_a) {
99
+ var data = _a.data, path = _a.path;
100
+ for (var i = path.length - 1; i >= 0; --i) {
101
+ var key = path[i];
102
+ var isNumericKey = !isNaN(+key);
103
+ var parent_1 = isNumericKey ? [] : {};
104
+ parent_1[key] = data;
105
+ data = parent_1;
106
+ }
107
+ mergedData = merger.merge(mergedData, data);
108
+ });
109
+ }
110
+ return mergedData;
22
111
  }
23
112
 
24
113
  exports.NetworkStatus = void 0;
@@ -983,28 +1072,18 @@ var QueryInfo = (function () {
983
1072
  };
984
1073
  QueryInfo.prototype.markResult = function (result, document, options, cacheWriteBehavior) {
985
1074
  var _this = this;
1075
+ var merger = new utilities.DeepMerger();
986
1076
  var graphQLErrors = utilities.isNonEmptyArray(result.errors)
987
1077
  ? result.errors.slice(0)
988
1078
  : [];
989
1079
  this.reset();
990
1080
  if ('incremental' in result && utilities.isNonEmptyArray(result.incremental)) {
991
- var mergedData_1 = this.getDiff().result;
992
- var merger_1 = new utilities.DeepMerger();
993
- result.incremental.forEach(function (_a) {
994
- var data = _a.data, path = _a.path, errors = _a.errors;
995
- for (var i = path.length - 1; i >= 0; --i) {
996
- var key = path[i];
997
- var isNumericKey = !isNaN(+key);
998
- var parent_1 = isNumericKey ? [] : {};
999
- parent_1[key] = data;
1000
- data = parent_1;
1001
- }
1002
- if (errors) {
1003
- graphQLErrors.push.apply(graphQLErrors, errors);
1004
- }
1005
- mergedData_1 = merger_1.merge(mergedData_1, data);
1006
- });
1007
- result.data = mergedData_1;
1081
+ var mergedData = mergeIncrementalData(this.getDiff().result, result);
1082
+ result.data = mergedData;
1083
+ }
1084
+ else if ('hasNext' in result && result.hasNext) {
1085
+ var diff = this.getDiff();
1086
+ result.data = merger.merge(diff.result, result.data);
1008
1087
  }
1009
1088
  this.graphQLErrors = graphQLErrors;
1010
1089
  if (options.fetchPolicy === 'no-cache') {
@@ -1161,7 +1240,7 @@ var QueryManager = (function () {
1161
1240
  return utilities.asyncMap(self.getObservableFromLink(mutation, tslib.__assign(tslib.__assign({}, context), { optimisticResponse: optimisticResponse }), variables, false), function (result) {
1162
1241
  if (utilities.graphQLResultHasError(result) && errorPolicy === 'none') {
1163
1242
  throw new errors.ApolloError({
1164
- graphQLErrors: result.errors,
1243
+ graphQLErrors: utilities.getGraphQLErrorsFromResult(result),
1165
1244
  });
1166
1245
  }
1167
1246
  if (mutationStoreValue) {
@@ -1195,7 +1274,9 @@ var QueryManager = (function () {
1195
1274
  }).subscribe({
1196
1275
  next: function (storeResult) {
1197
1276
  self.broadcastQueries();
1198
- resolve(storeResult);
1277
+ if (!('hasNext' in storeResult) || storeResult.hasNext === false) {
1278
+ resolve(storeResult);
1279
+ }
1199
1280
  },
1200
1281
  error: function (err) {
1201
1282
  if (mutationStoreValue) {
@@ -1223,12 +1304,33 @@ var QueryManager = (function () {
1223
1304
  var cacheWrites = [];
1224
1305
  var skipCache = mutation.fetchPolicy === "no-cache";
1225
1306
  if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
1226
- cacheWrites.push({
1227
- result: result.data,
1228
- dataId: 'ROOT_MUTATION',
1229
- query: mutation.document,
1230
- variables: mutation.variables,
1231
- });
1307
+ if (!isExecutionPatchIncrementalResult(result)) {
1308
+ cacheWrites.push({
1309
+ result: result.data,
1310
+ dataId: 'ROOT_MUTATION',
1311
+ query: mutation.document,
1312
+ variables: mutation.variables,
1313
+ });
1314
+ }
1315
+ if (isExecutionPatchIncrementalResult(result) && utilities.isNonEmptyArray(result.incremental)) {
1316
+ var diff = cache.diff({
1317
+ id: "ROOT_MUTATION",
1318
+ query: this.transform(mutation.document).asQuery,
1319
+ variables: mutation.variables,
1320
+ optimistic: false,
1321
+ returnPartialData: true,
1322
+ });
1323
+ var mergedData = mergeIncrementalData(diff.result, result);
1324
+ if (typeof mergedData !== 'undefined') {
1325
+ result.data = mergedData;
1326
+ cacheWrites.push({
1327
+ result: mergedData,
1328
+ dataId: 'ROOT_MUTATION',
1329
+ query: mutation.document,
1330
+ variables: mutation.variables,
1331
+ });
1332
+ }
1333
+ }
1232
1334
  var updateQueries_1 = mutation.updateQueries;
1233
1335
  if (updateQueries_1) {
1234
1336
  this.queries.forEach(function (_a, queryId) {
@@ -1275,6 +1377,8 @@ var QueryManager = (function () {
1275
1377
  cacheWrites.forEach(function (write) { return cache.write(write); });
1276
1378
  }
1277
1379
  var update = mutation.update;
1380
+ var isFinalResult = !isExecutionPatchResult(result) ||
1381
+ (isExecutionPatchIncrementalResult(result) && !result.hasNext);
1278
1382
  if (update) {
1279
1383
  if (!skipCache) {
1280
1384
  var diff = cache.diff({
@@ -1284,16 +1388,24 @@ var QueryManager = (function () {
1284
1388
  optimistic: false,
1285
1389
  returnPartialData: true,
1286
1390
  });
1287
- if (diff.complete && !(isExecutionPatchIncrementalResult(result))) {
1391
+ if (diff.complete) {
1288
1392
  result = tslib.__assign(tslib.__assign({}, result), { data: diff.result });
1393
+ if ('incremental' in result) {
1394
+ delete result.incremental;
1395
+ }
1396
+ if ('hasNext' in result) {
1397
+ delete result.hasNext;
1398
+ }
1289
1399
  }
1290
1400
  }
1291
- update(cache, result, {
1292
- context: mutation.context,
1293
- variables: mutation.variables,
1294
- });
1401
+ if (isFinalResult) {
1402
+ update(cache, result, {
1403
+ context: mutation.context,
1404
+ variables: mutation.variables,
1405
+ });
1406
+ }
1295
1407
  }
1296
- if (!skipCache && !mutation.keepRootFields) {
1408
+ if (!skipCache && !mutation.keepRootFields && isFinalResult) {
1297
1409
  cache.modify({
1298
1410
  id: 'ROOT_MUTATION',
1299
1411
  fields: function (value, _a) {
@@ -1659,20 +1771,10 @@ var QueryManager = (function () {
1659
1771
  };
1660
1772
  QueryManager.prototype.getResultsFromLink = function (queryInfo, cacheWriteBehavior, options) {
1661
1773
  var requestId = queryInfo.lastRequestId = this.generateRequestId();
1662
- options = utilities.cloneDeep(options);
1663
1774
  var linkDocument = this.cache.transformForLink(this.transform(queryInfo.document).document);
1664
1775
  return utilities.asyncMap(this.getObservableFromLink(linkDocument, options.context, options.variables), function (result) {
1665
- var graphQLErrors = utilities.isNonEmptyArray(result.errors)
1666
- ? result.errors.slice(0)
1667
- : [];
1668
- if ('incremental' in result && utilities.isNonEmptyArray(result.incremental)) {
1669
- result.incremental.forEach(function (incrementalResult) {
1670
- if (incrementalResult.errors) {
1671
- graphQLErrors.push.apply(graphQLErrors, incrementalResult.errors);
1672
- }
1673
- });
1674
- }
1675
- var hasErrors = utilities.isNonEmptyArray(graphQLErrors);
1776
+ var graphQLErrors = utilities.getGraphQLErrorsFromResult(result);
1777
+ var hasErrors = graphQLErrors.length > 0;
1676
1778
  if (requestId >= queryInfo.lastRequestId) {
1677
1779
  if (hasErrors && options.errorPolicy === "none") {
1678
1780
  throw queryInfo.markError(new errors.ApolloError({
@@ -1958,7 +2060,7 @@ var ApolloClient = (function () {
1958
2060
  if (connectToDevTools && typeof window === 'object') {
1959
2061
  window.__APOLLO_CLIENT__ = this;
1960
2062
  }
1961
- if (!hasSuggestedDevtools && __DEV__) {
2063
+ if (!hasSuggestedDevtools && connectToDevTools && __DEV__) {
1962
2064
  hasSuggestedDevtools = true;
1963
2065
  if (typeof window !== 'undefined' &&
1964
2066
  window.document &&