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

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 (59) hide show
  1. package/README.md +7 -7
  2. package/apollo-client.cjs +146 -57
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/core/ApolloClient.d.ts +2 -1
  6. package/core/ApolloClient.d.ts.map +1 -1
  7. package/core/ApolloClient.js.map +1 -1
  8. package/core/LocalState.d.ts +3 -1
  9. package/core/LocalState.d.ts.map +1 -1
  10. package/core/LocalState.js +6 -2
  11. package/core/LocalState.js.map +1 -1
  12. package/core/ObservableQuery.d.ts +2 -1
  13. package/core/ObservableQuery.d.ts.map +1 -1
  14. package/core/ObservableQuery.js +5 -2
  15. package/core/ObservableQuery.js.map +1 -1
  16. package/core/QueryInfo.d.ts.map +1 -1
  17. package/core/QueryInfo.js +3 -16
  18. package/core/QueryInfo.js.map +1 -1
  19. package/core/QueryManager.d.ts.map +1 -1
  20. package/core/QueryManager.js +55 -28
  21. package/core/QueryManager.js.map +1 -1
  22. package/core/core.cjs +156 -48
  23. package/core/core.cjs.map +1 -1
  24. package/core/core.cjs.native.js +156 -48
  25. package/core/types.d.ts +3 -0
  26. package/core/types.d.ts.map +1 -1
  27. package/core/types.js.map +1 -1
  28. package/invariantErrorCodes.js +1 -1
  29. package/link/batch-http/batch-http.cjs +12 -2
  30. package/link/batch-http/batch-http.cjs.map +1 -1
  31. package/link/batch-http/batch-http.cjs.native.js +12 -2
  32. package/link/batch-http/batchHttpLink.d.ts.map +1 -1
  33. package/link/batch-http/batchHttpLink.js +13 -3
  34. package/link/batch-http/batchHttpLink.js.map +1 -1
  35. package/link/http/createHttpLink.d.ts.map +1 -1
  36. package/link/http/createHttpLink.js +8 -1
  37. package/link/http/createHttpLink.js.map +1 -1
  38. package/link/http/http.cjs +7 -0
  39. package/link/http/http.cjs.map +1 -1
  40. package/link/http/http.cjs.native.js +7 -0
  41. package/package.json +8 -8
  42. package/react/hooks/hooks.cjs +27 -5
  43. package/react/hooks/hooks.cjs.map +1 -1
  44. package/react/hooks/hooks.cjs.native.js +27 -5
  45. package/react/hooks/useSuspenseQuery.d.ts.map +1 -1
  46. package/react/hooks/useSuspenseQuery.js +28 -6
  47. package/react/hooks/useSuspenseQuery.js.map +1 -1
  48. package/utilities/common/errorHandling.d.ts +3 -2
  49. package/utilities/common/errorHandling.d.ts.map +1 -1
  50. package/utilities/common/errorHandling.js +18 -1
  51. package/utilities/common/errorHandling.js.map +1 -1
  52. package/utilities/common/incrementalResult.d.ts +5 -2
  53. package/utilities/common/incrementalResult.d.ts.map +1 -1
  54. package/utilities/common/incrementalResult.js +29 -1
  55. package/utilities/common/incrementalResult.js.map +1 -1
  56. package/utilities/utilities.cjs +21 -1
  57. package/utilities/utilities.cjs.map +1 -1
  58. package/utilities/utilities.cjs.native.js +21 -1
  59. package/version.js +1 -1
package/README.md CHANGED
@@ -21,13 +21,13 @@ Learn how to use Apollo Client with self-paced hands-on training on Odyssey, Apo
21
21
 
22
22
  ## Maintainers
23
23
 
24
- - [@benjamn](https://github.com/benjamn)
25
- - [@alessbell](https://github.com/alessbell)
26
- - [@bignimbus](https://github.com/bignimbus)
27
- - [@hwillson](https://github.com/hwillson)
28
- - [@jpvajda](https://github.com/jpvajda)
29
- - [@mrdoombringer](https://github.com/mrdoombringer)
30
- - [@jerelmiller](https://github.com/jerelmiller)
24
+ |Name|Username|
25
+ |---|---|
26
+ |Ben Newman|[@benjamn](https://github.com/benjamn)|
27
+ |Alessia Bellisario|[@alessbell](https://github.com/alessbell)|
28
+ |Jeff Auriemma|[@bignimbus](https://github.com/bignimbus)|
29
+ |Hugh Willson|[@hwillson](https://github.com/hwillson)|
30
+ |Jerel Miller|[@jerelmiller](https://github.com/jerelmiller)|
31
31
 
32
32
  ## Who is Apollo?
33
33
 
package/apollo-client.cjs CHANGED
@@ -1053,8 +1053,53 @@ function isNonEmptyArray(value) {
1053
1053
  return Array.isArray(value) && value.length > 0;
1054
1054
  }
1055
1055
 
1056
+ function isExecutionPatchIncrementalResult(value) {
1057
+ return "incremental" in value;
1058
+ }
1059
+ function isExecutionPatchInitialResult(value) {
1060
+ return "hasNext" in value && "data" in value;
1061
+ }
1062
+ function isExecutionPatchResult(value) {
1063
+ return (isExecutionPatchIncrementalResult(value) ||
1064
+ isExecutionPatchInitialResult(value));
1065
+ }
1066
+ function mergeIncrementalData(prevResult, result) {
1067
+ var mergedData = prevResult;
1068
+ var merger = new DeepMerger();
1069
+ if (isExecutionPatchIncrementalResult(result) &&
1070
+ isNonEmptyArray(result.incremental)) {
1071
+ result.incremental.forEach(function (_a) {
1072
+ var data = _a.data, path = _a.path;
1073
+ for (var i = path.length - 1; i >= 0; --i) {
1074
+ var key = path[i];
1075
+ var isNumericKey = !isNaN(+key);
1076
+ var parent_1 = isNumericKey ? [] : {};
1077
+ parent_1[key] = data;
1078
+ data = parent_1;
1079
+ }
1080
+ mergedData = merger.merge(mergedData, data);
1081
+ });
1082
+ }
1083
+ return mergedData;
1084
+ }
1085
+
1056
1086
  function graphQLResultHasError(result) {
1057
- return (result.errors && result.errors.length > 0) || false;
1087
+ var errors = getGraphQLErrorsFromResult(result);
1088
+ return isNonEmptyArray(errors);
1089
+ }
1090
+ function getGraphQLErrorsFromResult(result) {
1091
+ var graphQLErrors = isNonEmptyArray(result.errors)
1092
+ ? result.errors.slice(0)
1093
+ : [];
1094
+ if (isExecutionPatchIncrementalResult(result) &&
1095
+ isNonEmptyArray(result.incremental)) {
1096
+ result.incremental.forEach(function (incrementalResult) {
1097
+ if (incrementalResult.errors) {
1098
+ graphQLErrors.push.apply(graphQLErrors, incrementalResult.errors);
1099
+ }
1100
+ });
1101
+ }
1102
+ return graphQLErrors;
1058
1103
  }
1059
1104
 
1060
1105
  function compact() {
@@ -1300,7 +1345,7 @@ var concat = ApolloLink.concat;
1300
1345
 
1301
1346
  var execute = ApolloLink.execute;
1302
1347
 
1303
- var version = '3.8.0-alpha.0';
1348
+ var version = '3.8.0-alpha.2';
1304
1349
 
1305
1350
  function isNodeResponse(value) {
1306
1351
  return !!value.body;
@@ -1798,6 +1843,13 @@ var createHttpLink = function (linkOptions) {
1798
1843
  credentials: context.credentials,
1799
1844
  headers: contextHeaders,
1800
1845
  };
1846
+ if (hasDirectives(['client'], operation.query)) {
1847
+ var transformedQuery = removeClientSetsFromDocument(operation.query);
1848
+ if (!transformedQuery) {
1849
+ return fromError(new Error('HttpLink: Trying to send a client-only query to the server. To send to the server, ensure a non-client field is added to the query or set the `transformOptions.removeClientFields` option to `true`.'));
1850
+ }
1851
+ operation.query = transformedQuery;
1852
+ }
1801
1853
  var _b = selectHttpOptionsAndBodyInternal(operation, print, fallbackHttpConfig, linkConfig, contextConfig), options = _b.options, body = _b.body;
1802
1854
  if (body.variables && !includeUnusedVariables) {
1803
1855
  var unusedNames_1 = new Set(Object.keys(body.variables));
@@ -1881,10 +1933,6 @@ var HttpLink = (function (_super) {
1881
1933
  return HttpLink;
1882
1934
  }(ApolloLink));
1883
1935
 
1884
- function isExecutionPatchIncrementalResult(value) {
1885
- return !!value.incremental;
1886
- }
1887
-
1888
1936
  var ApolloCache = (function () {
1889
1937
  function ApolloCache() {
1890
1938
  this.getFragmentDoc = optimism.wrap(getFragmentQueryDocument);
@@ -4613,7 +4661,7 @@ var ObservableQuery = (function (_super) {
4613
4661
  }
4614
4662
  return this.last;
4615
4663
  };
4616
- ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
4664
+ ObservableQuery.prototype.reobserveAsConcast = function (newOptions, newNetworkStatus) {
4617
4665
  var _this = this;
4618
4666
  this.isTornDown = false;
4619
4667
  var useDisposableConcast = newNetworkStatus === exports.NetworkStatus.refetch ||
@@ -4656,7 +4704,10 @@ var ObservableQuery = (function (_super) {
4656
4704
  this.observer = observer;
4657
4705
  }
4658
4706
  concast.addObserver(observer);
4659
- return concast.promise;
4707
+ return concast;
4708
+ };
4709
+ ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
4710
+ return this.reobserveAsConcast(newOptions, newNetworkStatus).promise;
4660
4711
  };
4661
4712
  ObservableQuery.prototype.observe = function () {
4662
4713
  this.reportResult(this.getCurrentResult(false), this.variables);
@@ -4780,8 +4831,12 @@ var LocalState = (function () {
4780
4831
  }
4781
4832
  return null;
4782
4833
  };
4783
- LocalState.prototype.serverQuery = function (document) {
4784
- return removeClientSetsFromDocument(document);
4834
+ LocalState.prototype.serverQuery = function (document, options) {
4835
+ if (options === void 0) { options = Object.create(null); }
4836
+ var _a = options.removeClientFields, removeClientFields = _a === void 0 ? true : _a;
4837
+ return removeClientFields
4838
+ ? removeClientSetsFromDocument(document)
4839
+ : document;
4785
4840
  };
4786
4841
  LocalState.prototype.prepareContext = function (context) {
4787
4842
  var cache = this.cache;
@@ -5182,22 +5237,8 @@ var QueryInfo = (function () {
5182
5237
  : [];
5183
5238
  this.reset();
5184
5239
  if ('incremental' in result && isNonEmptyArray(result.incremental)) {
5185
- var mergedData_1 = this.getDiff().result;
5186
- result.incremental.forEach(function (_a) {
5187
- var data = _a.data, path = _a.path, errors = _a.errors;
5188
- for (var i = path.length - 1; i >= 0; --i) {
5189
- var key = path[i];
5190
- var isNumericKey = !isNaN(+key);
5191
- var parent_1 = isNumericKey ? [] : {};
5192
- parent_1[key] = data;
5193
- data = parent_1;
5194
- }
5195
- if (errors) {
5196
- graphQLErrors.push.apply(graphQLErrors, errors);
5197
- }
5198
- mergedData_1 = merger.merge(mergedData_1, data);
5199
- });
5200
- result.data = mergedData_1;
5240
+ var mergedData = mergeIncrementalData(this.getDiff().result, result);
5241
+ result.data = mergedData;
5201
5242
  }
5202
5243
  else if ('hasNext' in result && result.hasNext) {
5203
5244
  var diff = this.getDiff();
@@ -5358,7 +5399,7 @@ var QueryManager = (function () {
5358
5399
  return asyncMap(self.getObservableFromLink(mutation, tslib.__assign(tslib.__assign({}, context), { optimisticResponse: optimisticResponse }), variables, false), function (result) {
5359
5400
  if (graphQLResultHasError(result) && errorPolicy === 'none') {
5360
5401
  throw new ApolloError({
5361
- graphQLErrors: result.errors,
5402
+ graphQLErrors: getGraphQLErrorsFromResult(result),
5362
5403
  });
5363
5404
  }
5364
5405
  if (mutationStoreValue) {
@@ -5392,7 +5433,9 @@ var QueryManager = (function () {
5392
5433
  }).subscribe({
5393
5434
  next: function (storeResult) {
5394
5435
  self.broadcastQueries();
5395
- resolve(storeResult);
5436
+ if (!('hasNext' in storeResult) || storeResult.hasNext === false) {
5437
+ resolve(storeResult);
5438
+ }
5396
5439
  },
5397
5440
  error: function (err) {
5398
5441
  if (mutationStoreValue) {
@@ -5420,12 +5463,33 @@ var QueryManager = (function () {
5420
5463
  var cacheWrites = [];
5421
5464
  var skipCache = mutation.fetchPolicy === "no-cache";
5422
5465
  if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
5423
- cacheWrites.push({
5424
- result: result.data,
5425
- dataId: 'ROOT_MUTATION',
5426
- query: mutation.document,
5427
- variables: mutation.variables,
5428
- });
5466
+ if (!isExecutionPatchIncrementalResult(result)) {
5467
+ cacheWrites.push({
5468
+ result: result.data,
5469
+ dataId: 'ROOT_MUTATION',
5470
+ query: mutation.document,
5471
+ variables: mutation.variables,
5472
+ });
5473
+ }
5474
+ if (isExecutionPatchIncrementalResult(result) && isNonEmptyArray(result.incremental)) {
5475
+ var diff = cache.diff({
5476
+ id: "ROOT_MUTATION",
5477
+ query: this.transform(mutation.document).asQuery,
5478
+ variables: mutation.variables,
5479
+ optimistic: false,
5480
+ returnPartialData: true,
5481
+ });
5482
+ var mergedData = mergeIncrementalData(diff.result, result);
5483
+ if (typeof mergedData !== 'undefined') {
5484
+ result.data = mergedData;
5485
+ cacheWrites.push({
5486
+ result: mergedData,
5487
+ dataId: 'ROOT_MUTATION',
5488
+ query: mutation.document,
5489
+ variables: mutation.variables,
5490
+ });
5491
+ }
5492
+ }
5429
5493
  var updateQueries_1 = mutation.updateQueries;
5430
5494
  if (updateQueries_1) {
5431
5495
  this.queries.forEach(function (_a, queryId) {
@@ -5472,6 +5536,8 @@ var QueryManager = (function () {
5472
5536
  cacheWrites.forEach(function (write) { return cache.write(write); });
5473
5537
  }
5474
5538
  var update = mutation.update;
5539
+ var isFinalResult = !isExecutionPatchResult(result) ||
5540
+ (isExecutionPatchIncrementalResult(result) && !result.hasNext);
5475
5541
  if (update) {
5476
5542
  if (!skipCache) {
5477
5543
  var diff = cache.diff({
@@ -5481,16 +5547,24 @@ var QueryManager = (function () {
5481
5547
  optimistic: false,
5482
5548
  returnPartialData: true,
5483
5549
  });
5484
- if (diff.complete && !(isExecutionPatchIncrementalResult(result))) {
5550
+ if (diff.complete) {
5485
5551
  result = tslib.__assign(tslib.__assign({}, result), { data: diff.result });
5552
+ if ('incremental' in result) {
5553
+ delete result.incremental;
5554
+ }
5555
+ if ('hasNext' in result) {
5556
+ delete result.hasNext;
5557
+ }
5486
5558
  }
5487
5559
  }
5488
- update(cache, result, {
5489
- context: mutation.context,
5490
- variables: mutation.variables,
5491
- });
5560
+ if (isFinalResult) {
5561
+ update(cache, result, {
5562
+ context: mutation.context,
5563
+ variables: mutation.variables,
5564
+ });
5565
+ }
5492
5566
  }
5493
- if (!skipCache && !mutation.keepRootFields) {
5567
+ if (!skipCache && !mutation.keepRootFields && isFinalResult) {
5494
5568
  cache.modify({
5495
5569
  id: 'ROOT_MUTATION',
5496
5570
  fields: function (value, _a) {
@@ -5549,11 +5623,13 @@ var QueryManager = (function () {
5549
5623
  };
5550
5624
  QueryManager.prototype.transform = function (document) {
5551
5625
  var transformCache = this.transformCache;
5626
+ var _a = (this.defaultOptions.transformQuery || Object.create(null)).removeClientFields, removeClientFields = _a === void 0 ? true : _a;
5552
5627
  if (!transformCache.has(document)) {
5553
5628
  var transformed = this.cache.transformDocument(document);
5554
5629
  var noConnection = removeConnectionDirectiveFromDocument(transformed);
5555
5630
  var clientQuery = this.localState.clientQuery(transformed);
5556
- var serverQuery = noConnection && this.localState.serverQuery(noConnection);
5631
+ var serverQuery = noConnection &&
5632
+ this.localState.serverQuery(noConnection, { removeClientFields: removeClientFields });
5557
5633
  var cacheEntry_1 = {
5558
5634
  document: transformed,
5559
5635
  hasClientExports: hasClientExports(transformed),
@@ -5858,17 +5934,8 @@ var QueryManager = (function () {
5858
5934
  var requestId = queryInfo.lastRequestId = this.generateRequestId();
5859
5935
  var linkDocument = this.cache.transformForLink(this.transform(queryInfo.document).document);
5860
5936
  return asyncMap(this.getObservableFromLink(linkDocument, options.context, options.variables), function (result) {
5861
- var graphQLErrors = isNonEmptyArray(result.errors)
5862
- ? result.errors.slice(0)
5863
- : [];
5864
- if ('incremental' in result && isNonEmptyArray(result.incremental)) {
5865
- result.incremental.forEach(function (incrementalResult) {
5866
- if (incrementalResult.errors) {
5867
- graphQLErrors.push.apply(graphQLErrors, incrementalResult.errors);
5868
- }
5869
- });
5870
- }
5871
- var hasErrors = isNonEmptyArray(graphQLErrors);
5937
+ var graphQLErrors = getGraphQLErrorsFromResult(result);
5938
+ var hasErrors = graphQLErrors.length > 0;
5872
5939
  if (requestId >= queryInfo.lastRequestId) {
5873
5940
  if (hasErrors && options.errorPolicy === "none") {
5874
5941
  throw queryInfo.markError(new ApolloError({
@@ -7171,24 +7238,28 @@ function useSuspenseQuery_experimental(query, options) {
7171
7238
  var client = useApolloClient(options.client);
7172
7239
  var watchQueryOptions = useWatchQueryOptions({ query: query, options: options, client: client });
7173
7240
  var previousWatchQueryOptionsRef = React.useRef(watchQueryOptions);
7241
+ var deferred = useIsDeferred(query);
7174
7242
  var fetchPolicy = watchQueryOptions.fetchPolicy, errorPolicy = watchQueryOptions.errorPolicy, returnPartialData = watchQueryOptions.returnPartialData, variables = watchQueryOptions.variables;
7175
7243
  var cacheEntry = suspenseCache.lookup(query, variables);
7176
7244
  var observable = React.useState(function () {
7177
7245
  return (cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.observable) || client.watchQuery(watchQueryOptions);
7178
7246
  })[0];
7179
7247
  var result = useObservableQueryResult(observable);
7180
- if (result.error && errorPolicy === 'none') {
7248
+ var hasFullResult = result.data && !result.partial;
7249
+ var hasPartialResult = result.data && result.partial;
7250
+ var usePartialResult = returnPartialData && hasPartialResult;
7251
+ if (result.error &&
7252
+ errorPolicy === 'none' &&
7253
+ (!deferred || !hasPartialResult)) {
7181
7254
  throw result.error;
7182
7255
  }
7183
7256
  if (result.loading) {
7184
7257
  if (!cacheEntry) {
7185
7258
  cacheEntry = suspenseCache.add(query, variables, {
7186
- promise: observable.reobserve(watchQueryOptions),
7259
+ promise: maybeWrapConcastWithCustomPromise(observable.reobserveAsConcast(watchQueryOptions), { deferred: deferred }),
7187
7260
  observable: observable,
7188
7261
  });
7189
7262
  }
7190
- var hasFullResult = result.data && !result.partial;
7191
- var usePartialResult = returnPartialData && result.partial && result.data;
7192
7263
  var hasUsableResult = usePartialResult ||
7193
7264
  (fetchPolicy === 'cache-and-network' && hasFullResult);
7194
7265
  if (!hasUsableResult && !cacheEntry.fulfilled) {
@@ -7215,7 +7286,7 @@ function useSuspenseQuery_experimental(query, options) {
7215
7286
  return React.useMemo(function () {
7216
7287
  return {
7217
7288
  data: result.data,
7218
- error: errorPolicy === 'all' ? toApolloError(result) : void 0,
7289
+ error: errorPolicy === 'ignore' ? void 0 : toApolloError(result),
7219
7290
  fetchMore: function (options) {
7220
7291
  var promise = observable.fetchMore(options);
7221
7292
  suspenseCache.add(query, watchQueryOptions.variables, {
@@ -7254,6 +7325,21 @@ function toApolloError(result) {
7254
7325
  ? new ApolloError({ graphQLErrors: result.errors })
7255
7326
  : result.error;
7256
7327
  }
7328
+ function maybeWrapConcastWithCustomPromise(concast, _a) {
7329
+ var deferred = _a.deferred;
7330
+ if (deferred) {
7331
+ return new Promise(function (resolve, reject) {
7332
+ var subscription = concast.subscribe({
7333
+ next: function (value) {
7334
+ resolve(value);
7335
+ subscription.unsubscribe();
7336
+ },
7337
+ error: reject,
7338
+ });
7339
+ });
7340
+ }
7341
+ return concast.promise;
7342
+ }
7257
7343
  function useWatchQueryOptions(_a) {
7258
7344
  var query = _a.query, options = _a.options, client = _a.client;
7259
7345
  var defaultOptions = client.defaultOptions.watchQuery;
@@ -7266,6 +7352,9 @@ function useWatchQueryOptions(_a) {
7266
7352
  }
7267
7353
  return watchQueryOptions;
7268
7354
  }
7355
+ function useIsDeferred(query) {
7356
+ return React.useMemo(function () { return hasDirectives(['defer'], query); }, [query]);
7357
+ }
7269
7358
  function useObservableQueryResult(observable) {
7270
7359
  var resultRef = React.useRef();
7271
7360
  var isMountedRef = React.useRef(false);