Skip to content

Commit

Permalink
Make fetchMore pass new variables to merge functions.
Browse files Browse the repository at this point in the history
This commit also deprecates the updateQuery function, while preserving its
existing behavior for backwards compatibility. If you're using a field
policy, you shouldn't need an updateQuery function.

Fixes #5951.
  • Loading branch information
benjamn committed Jun 22, 2020
1 parent 616b518 commit d261c4f
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const hasError = (
(policy === 'none' && isNonEmptyArray(storeValue.graphQLErrors))
);

let warnedAboutUpdateQuery = false;

export class ObservableQuery<
TData = any,
TVariables = OperationVariables
Expand Down Expand Up @@ -309,15 +311,45 @@ export class ObservableQuery<
combinedOptions,
NetworkStatus.fetchMore,
).then(fetchMoreResult => {
this.updateQuery((previousResult: any) => {
const data = fetchMoreResult.data as TData;
const { updateQuery } = fetchMoreOptions;
return updateQuery ? updateQuery(previousResult, {
const data = fetchMoreResult.data as TData;
const { updateQuery } = fetchMoreOptions;

if (updateQuery) {
if (process.env.NODE_ENV !== "production" &&
!warnedAboutUpdateQuery) {
invariant.warn(
`The updateQuery callback for fetchMore is deprecated, and will be removed
in the next major version of Apollo Client.
Please convert updateQuery functions to field policies with appropriate
read and merge functions, or use/adapt a helper function (such as
concatPagination, offsetLimitPagination, or relayStylePagination) from
@apollo/client/utilities.
The field policy system handles pagination more effectively than a
hand-written updateQuery function, and you only need to define the policy
once, rather than every time you call fetchMore.`);
warnedAboutUpdateQuery = true;
}
this.updateQuery(previous => updateQuery(previous, {
fetchMoreResult: data,
variables: combinedOptions.variables as TVariables,
}) : data;
});
}));
} else {
// If we're using a field policy instead of updateQuery, the only
// thing we need to do is write the new data to the cache using
// combinedOptions.variables (instead of this.variables, which is
// what this.updateQuery uses, because it works by abusing the
// original field value, keyed by the original variables).
this.queryManager.cache.writeQuery({
query: combinedOptions.query,
variables: combinedOptions.variables,
data,
});
}

return fetchMoreResult as ApolloQueryResult<TData>;

}).finally(() => {
this.queryManager.stopQuery(qid);
this.reobserve();
Expand Down Expand Up @@ -423,7 +455,7 @@ export class ObservableQuery<
return Promise.resolve();
}

let { fetchPolicy } = this.options;
let { fetchPolicy = 'cache-first' } = this.options;
if (fetchPolicy !== 'cache-first' &&
fetchPolicy !== 'no-cache' &&
fetchPolicy !== 'network-only') {
Expand Down

0 comments on commit d261c4f

Please sign in to comment.