@apollo/client 3.5.5 → 3.6.0-beta.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.
package/apollo-client.cjs CHANGED
@@ -13,6 +13,7 @@ var context = require('@wry/context');
13
13
  var trie = require('@wry/trie');
14
14
  var graphqlTag = require('graphql-tag');
15
15
  var React = require('react');
16
+ var shim = require('use-sync-external-store/shim');
16
17
 
17
18
  function _interopNamespace(e) {
18
19
  if (e && e.__esModule) return e;
@@ -1291,7 +1292,7 @@ var concat = ApolloLink.concat;
1291
1292
 
1292
1293
  var execute = ApolloLink.execute;
1293
1294
 
1294
- var version = '3.5.5';
1295
+ var version = '3.6.0-beta.3';
1295
1296
 
1296
1297
  var hasOwnProperty$2 = Object.prototype.hasOwnProperty;
1297
1298
  function parseAndCheckHttpResponse(operations) {
@@ -6055,6 +6056,12 @@ function useQuery(query, options) {
6055
6056
  var context = React.useContext(getApolloContext());
6056
6057
  var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
6057
6058
  verifyDocumentType(query, exports.DocumentType.Query);
6059
+ var ref = React.useRef({
6060
+ client: client,
6061
+ query: query,
6062
+ options: options,
6063
+ watchQueryOptions: createWatchQueryOptions(query, options),
6064
+ });
6058
6065
  var _b = React.useState(function () {
6059
6066
  var watchQueryOptions = createWatchQueryOptions(query, options);
6060
6067
  var obsQuery = null;
@@ -6094,114 +6101,96 @@ function useQuery(query, options) {
6094
6101
  }
6095
6102
  return obsQuery;
6096
6103
  }), obsQuery = _b[0], setObsQuery = _b[1];
6097
- var _c = React.useState(function () {
6098
- var _a, _b;
6099
- var result = obsQuery.getCurrentResult();
6100
- if (!result.loading && options) {
6101
- if (result.error) {
6102
- (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, result.error);
6103
- }
6104
- else if (result.data) {
6105
- (_b = options.onCompleted) === null || _b === void 0 ? void 0 : _b.call(options, result.data);
6106
- }
6107
- }
6108
- return result;
6109
- }), result = _c[0], setResult = _c[1];
6110
- var ref = React.useRef({
6111
- client: client,
6112
- query: query,
6113
- options: options,
6114
- result: result,
6115
- previousData: void 0,
6116
- watchQueryOptions: createWatchQueryOptions(query, options),
6117
- });
6118
6104
  React.useEffect(function () {
6119
- var _a, _b;
6120
6105
  var watchQueryOptions = createWatchQueryOptions(query, options);
6121
- var nextResult;
6122
6106
  if (ref.current.client !== client || !equality.equal(ref.current.query, query)) {
6123
6107
  var obsQuery_1 = client.watchQuery(watchQueryOptions);
6124
6108
  setObsQuery(obsQuery_1);
6125
- nextResult = obsQuery_1.getCurrentResult();
6126
6109
  }
6127
6110
  else if (!equality.equal(ref.current.watchQueryOptions, watchQueryOptions)) {
6128
- obsQuery.setOptions(watchQueryOptions).catch(function () { });
6129
- nextResult = obsQuery.getCurrentResult();
6130
- ref.current.watchQueryOptions = watchQueryOptions;
6131
- }
6132
- if (nextResult) {
6133
- var previousResult = ref.current.result;
6134
- if (previousResult.data) {
6135
- ref.current.previousData = previousResult.data;
6136
- }
6137
- setResult(ref.current.result = nextResult);
6138
- if (!nextResult.loading && options) {
6139
- if (!result.loading) {
6140
- if (result.error) {
6141
- (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, result.error);
6142
- }
6143
- else if (result.data) {
6144
- (_b = options.onCompleted) === null || _b === void 0 ? void 0 : _b.call(options, result.data);
6145
- }
6146
- }
6147
- }
6111
+ obsQuery.setOptions(watchQueryOptions);
6112
+ setObsQuery(obsQuery);
6148
6113
  }
6149
- Object.assign(ref.current, { client: client, query: query, options: options });
6114
+ Object.assign(ref.current, {
6115
+ client: client,
6116
+ query: query,
6117
+ options: options,
6118
+ watchQueryOptions: watchQueryOptions,
6119
+ });
6150
6120
  }, [obsQuery, client, query, options]);
6151
- React.useEffect(function () {
6152
- if (context.renderPromises) {
6153
- return;
6154
- }
6155
- var subscription = obsQuery.subscribe(onNext, onError);
6156
- function onNext() {
6157
- var _a, _b;
6158
- var previousResult = ref.current.result;
6159
- var result = obsQuery.getCurrentResult();
6160
- if (previousResult &&
6161
- previousResult.loading === result.loading &&
6162
- previousResult.networkStatus === result.networkStatus &&
6163
- equality.equal(previousResult.data, result.data)) {
6164
- return;
6165
- }
6166
- if (previousResult.data) {
6167
- ref.current.previousData = previousResult.data;
6168
- }
6169
- setResult(ref.current.result = result);
6170
- if (!result.loading) {
6171
- (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onCompleted) === null || _b === void 0 ? void 0 : _b.call(_a, result.data);
6172
- }
6173
- }
6174
- function onError(error) {
6175
- var _a, _b;
6176
- var last = obsQuery["last"];
6177
- subscription.unsubscribe();
6178
- try {
6121
+ var _c = React.useMemo(function () {
6122
+ var previousResult;
6123
+ var subscribe = function (forceUpdate) {
6124
+ var subscription = obsQuery.subscribe(forceUpdate, onError);
6125
+ function onError(error) {
6126
+ forceUpdate();
6127
+ subscription.unsubscribe();
6128
+ var last = obsQuery["last"];
6179
6129
  obsQuery.resetLastResults();
6180
- subscription = obsQuery.subscribe(onNext, onError);
6181
- }
6182
- finally {
6130
+ obsQuery.subscribe(forceUpdate, onError);
6183
6131
  obsQuery["last"] = last;
6132
+ if (!error.hasOwnProperty('graphQLErrors')) {
6133
+ throw error;
6134
+ }
6184
6135
  }
6185
- if (!error.hasOwnProperty('graphQLErrors')) {
6186
- throw error;
6136
+ return function () {
6137
+ subscription.unsubscribe();
6138
+ };
6139
+ };
6140
+ var getSnapshot = function () {
6141
+ var result = obsQuery.getCurrentResult();
6142
+ if (result.errors && result.errors.length) {
6143
+ result = tslib.__assign(tslib.__assign({}, result), { error: result.error || new ApolloError({ graphQLErrors: result.errors }) });
6144
+ }
6145
+ if (!previousResult ||
6146
+ previousResult.loading !== result.loading ||
6147
+ previousResult.networkStatus !== result.networkStatus ||
6148
+ !equality.equal(previousResult.data, result.data) ||
6149
+ !equality.equal(previousResult.error, result.error)) {
6150
+ if (previousResult) {
6151
+ result = tslib.__assign(tslib.__assign({}, result), { previousData: previousResult.data || previousResult.previousData });
6152
+ }
6153
+ previousResult = result;
6187
6154
  }
6188
- var previousResult = ref.current.result;
6189
- if ((previousResult && previousResult.loading) ||
6190
- !equality.equal(error, previousResult.error)) {
6191
- setResult(ref.current.result = {
6192
- data: previousResult.data,
6193
- error: error,
6194
- loading: false,
6195
- networkStatus: exports.NetworkStatus.error,
6196
- });
6197
- (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
6155
+ return previousResult;
6156
+ };
6157
+ return [subscribe, getSnapshot];
6158
+ }, [obsQuery]), subscribe = _c[0], getSnapshot = _c[1];
6159
+ var obsQueryMethods = React.useMemo(function () { return ({
6160
+ refetch: obsQuery.refetch.bind(obsQuery),
6161
+ fetchMore: obsQuery.fetchMore.bind(obsQuery),
6162
+ updateQuery: obsQuery.updateQuery.bind(obsQuery),
6163
+ startPolling: obsQuery.startPolling.bind(obsQuery),
6164
+ stopPolling: obsQuery.stopPolling.bind(obsQuery),
6165
+ subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
6166
+ }); }, [obsQuery]);
6167
+ var result = shim.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
6168
+ React.useEffect(function () {
6169
+ var _a, _b, _c, _d, _e, _f;
6170
+ if (((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) ||
6171
+ ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) === 'standby') {
6172
+ return;
6173
+ }
6174
+ if (!result.loading) {
6175
+ if (result.error) {
6176
+ (_d = (_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.onError) === null || _d === void 0 ? void 0 : _d.call(_c, result.error);
6177
+ }
6178
+ else if (result.data) {
6179
+ (_f = (_e = ref.current.options) === null || _e === void 0 ? void 0 : _e.onCompleted) === null || _f === void 0 ? void 0 : _f.call(_e, result.data);
6198
6180
  }
6199
6181
  }
6200
- return function () { return subscription.unsubscribe(); };
6201
- }, [obsQuery, context.renderPromises, client.disableNetworkFetches]);
6182
+ }, [result]);
6202
6183
  var partial;
6203
6184
  (_a = result, partial = _a.partial, result = tslib.__rest(_a, ["partial"]));
6204
- {
6185
+ if ((options === null || options === void 0 ? void 0 : options.skip) || (options === null || options === void 0 ? void 0 : options.fetchPolicy) === 'standby') {
6186
+ result = {
6187
+ loading: false,
6188
+ data: void 0,
6189
+ error: void 0,
6190
+ networkStatus: exports.NetworkStatus.ready,
6191
+ };
6192
+ }
6193
+ else {
6205
6194
  if (partial &&
6206
6195
  (options === null || options === void 0 ? void 0 : options.partialRefetch) &&
6207
6196
  !result.loading &&
@@ -6210,42 +6199,12 @@ function useQuery(query, options) {
6210
6199
  result = tslib.__assign(tslib.__assign({}, result), { loading: true, networkStatus: exports.NetworkStatus.refetch });
6211
6200
  obsQuery.refetch();
6212
6201
  }
6213
- if (context.renderPromises &&
6214
- (options === null || options === void 0 ? void 0 : options.ssr) !== false &&
6215
- !(options === null || options === void 0 ? void 0 : options.skip) &&
6216
- result.loading) {
6217
- obsQuery.setOptions(createWatchQueryOptions(query, options)).catch(function () { });
6202
+ if (context.renderPromises && (options === null || options === void 0 ? void 0 : options.ssr) !== false && result.loading) {
6203
+ obsQuery.setOptions(createWatchQueryOptions(query, options))
6204
+ .catch(function () { });
6218
6205
  }
6219
6206
  }
6220
- if ((context.renderPromises || client.disableNetworkFetches) &&
6221
- (options === null || options === void 0 ? void 0 : options.ssr) === false) {
6222
- result = ref.current.result = {
6223
- loading: true,
6224
- data: void 0,
6225
- error: void 0,
6226
- networkStatus: exports.NetworkStatus.loading,
6227
- };
6228
- }
6229
- else if ((options === null || options === void 0 ? void 0 : options.skip) || (options === null || options === void 0 ? void 0 : options.fetchPolicy) === 'standby') {
6230
- result = {
6231
- loading: false,
6232
- data: void 0,
6233
- error: void 0,
6234
- networkStatus: exports.NetworkStatus.ready,
6235
- };
6236
- }
6237
- if (result.errors && result.errors.length) {
6238
- result = tslib.__assign(tslib.__assign({}, result), { error: result.error || new ApolloError({ graphQLErrors: result.errors }) });
6239
- }
6240
- var obsQueryFields = React.useMemo(function () { return ({
6241
- refetch: obsQuery.refetch.bind(obsQuery),
6242
- fetchMore: obsQuery.fetchMore.bind(obsQuery),
6243
- updateQuery: obsQuery.updateQuery.bind(obsQuery),
6244
- startPolling: obsQuery.startPolling.bind(obsQuery),
6245
- stopPolling: obsQuery.stopPolling.bind(obsQuery),
6246
- subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
6247
- }); }, [obsQuery]);
6248
- return tslib.__assign(tslib.__assign(tslib.__assign({}, obsQueryFields), { variables: obsQuery.variables, client: client, called: true, previousData: ref.current.previousData }), result);
6207
+ return tslib.__assign(tslib.__assign(tslib.__assign({}, obsQueryMethods), { variables: obsQuery.variables, client: client, called: true }), result);
6249
6208
  }
6250
6209
  function createWatchQueryOptions(query, options) {
6251
6210
  var _a;