@apollo/client 3.8.0-alpha.0 → 3.8.0-alpha.1

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 (37) hide show
  1. package/README.md +7 -7
  2. package/apollo-client.cjs +130 -54
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/core/ObservableQuery.d.ts +2 -1
  6. package/core/ObservableQuery.d.ts.map +1 -1
  7. package/core/ObservableQuery.js +5 -2
  8. package/core/ObservableQuery.js.map +1 -1
  9. package/core/QueryInfo.d.ts.map +1 -1
  10. package/core/QueryInfo.js +3 -16
  11. package/core/QueryInfo.js.map +1 -1
  12. package/core/QueryManager.d.ts.map +1 -1
  13. package/core/QueryManager.js +52 -27
  14. package/core/QueryManager.js.map +1 -1
  15. package/core/core.cjs +147 -45
  16. package/core/core.cjs.map +1 -1
  17. package/core/core.cjs.native.js +147 -45
  18. package/invariantErrorCodes.js +1 -1
  19. package/package.json +8 -8
  20. package/react/hooks/hooks.cjs +27 -5
  21. package/react/hooks/hooks.cjs.map +1 -1
  22. package/react/hooks/hooks.cjs.native.js +27 -5
  23. package/react/hooks/useSuspenseQuery.d.ts.map +1 -1
  24. package/react/hooks/useSuspenseQuery.js +28 -6
  25. package/react/hooks/useSuspenseQuery.js.map +1 -1
  26. package/utilities/common/errorHandling.d.ts +3 -2
  27. package/utilities/common/errorHandling.d.ts.map +1 -1
  28. package/utilities/common/errorHandling.js +18 -1
  29. package/utilities/common/errorHandling.js.map +1 -1
  30. package/utilities/common/incrementalResult.d.ts +5 -2
  31. package/utilities/common/incrementalResult.d.ts.map +1 -1
  32. package/utilities/common/incrementalResult.js +29 -1
  33. package/utilities/common/incrementalResult.js.map +1 -1
  34. package/utilities/utilities.cjs +21 -1
  35. package/utilities/utilities.cjs.map +1 -1
  36. package/utilities/utilities.cjs.native.js +21 -1
  37. 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.8.0-alpha.0';
18
+ var version = '3.8.0-alpha.1';
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;
@@ -421,7 +510,7 @@ var ObservableQuery = (function (_super) {
421
510
  }
422
511
  return this.last;
423
512
  };
424
- ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
513
+ ObservableQuery.prototype.reobserveAsConcast = function (newOptions, newNetworkStatus) {
425
514
  var _this = this;
426
515
  this.isTornDown = false;
427
516
  var useDisposableConcast = newNetworkStatus === exports.NetworkStatus.refetch ||
@@ -464,7 +553,10 @@ var ObservableQuery = (function (_super) {
464
553
  this.observer = observer;
465
554
  }
466
555
  concast.addObserver(observer);
467
- return concast.promise;
556
+ return concast;
557
+ };
558
+ ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
559
+ return this.reobserveAsConcast(newOptions, newNetworkStatus).promise;
468
560
  };
469
561
  ObservableQuery.prototype.observe = function () {
470
562
  this.reportResult(this.getCurrentResult(false), this.variables);
@@ -990,22 +1082,8 @@ var QueryInfo = (function () {
990
1082
  : [];
991
1083
  this.reset();
992
1084
  if ('incremental' in result && utilities.isNonEmptyArray(result.incremental)) {
993
- var mergedData_1 = this.getDiff().result;
994
- result.incremental.forEach(function (_a) {
995
- var data = _a.data, path = _a.path, errors = _a.errors;
996
- for (var i = path.length - 1; i >= 0; --i) {
997
- var key = path[i];
998
- var isNumericKey = !isNaN(+key);
999
- var parent_1 = isNumericKey ? [] : {};
1000
- parent_1[key] = data;
1001
- data = parent_1;
1002
- }
1003
- if (errors) {
1004
- graphQLErrors.push.apply(graphQLErrors, errors);
1005
- }
1006
- mergedData_1 = merger.merge(mergedData_1, data);
1007
- });
1008
- result.data = mergedData_1;
1085
+ var mergedData = mergeIncrementalData(this.getDiff().result, result);
1086
+ result.data = mergedData;
1009
1087
  }
1010
1088
  else if ('hasNext' in result && result.hasNext) {
1011
1089
  var diff = this.getDiff();
@@ -1166,7 +1244,7 @@ var QueryManager = (function () {
1166
1244
  return utilities.asyncMap(self.getObservableFromLink(mutation, tslib.__assign(tslib.__assign({}, context), { optimisticResponse: optimisticResponse }), variables, false), function (result) {
1167
1245
  if (utilities.graphQLResultHasError(result) && errorPolicy === 'none') {
1168
1246
  throw new errors.ApolloError({
1169
- graphQLErrors: result.errors,
1247
+ graphQLErrors: utilities.getGraphQLErrorsFromResult(result),
1170
1248
  });
1171
1249
  }
1172
1250
  if (mutationStoreValue) {
@@ -1200,7 +1278,9 @@ var QueryManager = (function () {
1200
1278
  }).subscribe({
1201
1279
  next: function (storeResult) {
1202
1280
  self.broadcastQueries();
1203
- resolve(storeResult);
1281
+ if (!('hasNext' in storeResult) || storeResult.hasNext === false) {
1282
+ resolve(storeResult);
1283
+ }
1204
1284
  },
1205
1285
  error: function (err) {
1206
1286
  if (mutationStoreValue) {
@@ -1228,12 +1308,33 @@ var QueryManager = (function () {
1228
1308
  var cacheWrites = [];
1229
1309
  var skipCache = mutation.fetchPolicy === "no-cache";
1230
1310
  if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
1231
- cacheWrites.push({
1232
- result: result.data,
1233
- dataId: 'ROOT_MUTATION',
1234
- query: mutation.document,
1235
- variables: mutation.variables,
1236
- });
1311
+ if (!isExecutionPatchIncrementalResult(result)) {
1312
+ cacheWrites.push({
1313
+ result: result.data,
1314
+ dataId: 'ROOT_MUTATION',
1315
+ query: mutation.document,
1316
+ variables: mutation.variables,
1317
+ });
1318
+ }
1319
+ if (isExecutionPatchIncrementalResult(result) && utilities.isNonEmptyArray(result.incremental)) {
1320
+ var diff = cache.diff({
1321
+ id: "ROOT_MUTATION",
1322
+ query: this.transform(mutation.document).asQuery,
1323
+ variables: mutation.variables,
1324
+ optimistic: false,
1325
+ returnPartialData: true,
1326
+ });
1327
+ var mergedData = mergeIncrementalData(diff.result, result);
1328
+ if (typeof mergedData !== 'undefined') {
1329
+ result.data = mergedData;
1330
+ cacheWrites.push({
1331
+ result: mergedData,
1332
+ dataId: 'ROOT_MUTATION',
1333
+ query: mutation.document,
1334
+ variables: mutation.variables,
1335
+ });
1336
+ }
1337
+ }
1237
1338
  var updateQueries_1 = mutation.updateQueries;
1238
1339
  if (updateQueries_1) {
1239
1340
  this.queries.forEach(function (_a, queryId) {
@@ -1280,6 +1381,8 @@ var QueryManager = (function () {
1280
1381
  cacheWrites.forEach(function (write) { return cache.write(write); });
1281
1382
  }
1282
1383
  var update = mutation.update;
1384
+ var isFinalResult = !isExecutionPatchResult(result) ||
1385
+ (isExecutionPatchIncrementalResult(result) && !result.hasNext);
1283
1386
  if (update) {
1284
1387
  if (!skipCache) {
1285
1388
  var diff = cache.diff({
@@ -1289,16 +1392,24 @@ var QueryManager = (function () {
1289
1392
  optimistic: false,
1290
1393
  returnPartialData: true,
1291
1394
  });
1292
- if (diff.complete && !(isExecutionPatchIncrementalResult(result))) {
1395
+ if (diff.complete) {
1293
1396
  result = tslib.__assign(tslib.__assign({}, result), { data: diff.result });
1397
+ if ('incremental' in result) {
1398
+ delete result.incremental;
1399
+ }
1400
+ if ('hasNext' in result) {
1401
+ delete result.hasNext;
1402
+ }
1294
1403
  }
1295
1404
  }
1296
- update(cache, result, {
1297
- context: mutation.context,
1298
- variables: mutation.variables,
1299
- });
1405
+ if (isFinalResult) {
1406
+ update(cache, result, {
1407
+ context: mutation.context,
1408
+ variables: mutation.variables,
1409
+ });
1410
+ }
1300
1411
  }
1301
- if (!skipCache && !mutation.keepRootFields) {
1412
+ if (!skipCache && !mutation.keepRootFields && isFinalResult) {
1302
1413
  cache.modify({
1303
1414
  id: 'ROOT_MUTATION',
1304
1415
  fields: function (value, _a) {
@@ -1666,17 +1777,8 @@ var QueryManager = (function () {
1666
1777
  var requestId = queryInfo.lastRequestId = this.generateRequestId();
1667
1778
  var linkDocument = this.cache.transformForLink(this.transform(queryInfo.document).document);
1668
1779
  return utilities.asyncMap(this.getObservableFromLink(linkDocument, options.context, options.variables), function (result) {
1669
- var graphQLErrors = utilities.isNonEmptyArray(result.errors)
1670
- ? result.errors.slice(0)
1671
- : [];
1672
- if ('incremental' in result && utilities.isNonEmptyArray(result.incremental)) {
1673
- result.incremental.forEach(function (incrementalResult) {
1674
- if (incrementalResult.errors) {
1675
- graphQLErrors.push.apply(graphQLErrors, incrementalResult.errors);
1676
- }
1677
- });
1678
- }
1679
- var hasErrors = utilities.isNonEmptyArray(graphQLErrors);
1780
+ var graphQLErrors = utilities.getGraphQLErrorsFromResult(result);
1781
+ var hasErrors = graphQLErrors.length > 0;
1680
1782
  if (requestId >= queryInfo.lastRequestId) {
1681
1783
  if (hasErrors && options.errorPolicy === "none") {
1682
1784
  throw queryInfo.markError(new errors.ApolloError({