Skip to content

Commit

Permalink
Reset options.fetchPolicy in ObservableQuery#setVariables.
Browse files Browse the repository at this point in the history
This change seems consistent with the goals of #7437, though variables
can be changed without calling ObservableQuery#setVariables.
  • Loading branch information
benjamn committed Jul 8, 2021
1 parent c6f0ada commit 94527c1
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
WatchQueryOptions,
FetchMoreQueryOptions,
SubscribeToMoreOptions,
WatchQueryFetchPolicy,
} from './watchQueryOptions';
import { QueryInfo } from './QueryInfo';

Expand Down Expand Up @@ -57,6 +58,10 @@ export class ObservableQuery<
return this.options.variables;
}

// Original value of this.options.fetchPolicy (defaulting to "cache-first"),
// from whenever the ObservableQuery was first created.
public initialFetchPolicy: WatchQueryFetchPolicy;

private isTornDown: boolean;
private queryManager: QueryManager<any>;
private observers = new Set<Observer<ApolloQueryResult<TData>>>();
Expand Down Expand Up @@ -129,6 +134,8 @@ export class ObservableQuery<
const opDef = getOperationDefinition(options.query);
this.queryName = opDef && opDef.name && opDef.name.value;

this.initialFetchPolicy = options.fetchPolicy || "cache-first";

// related classes
this.queryManager = queryManager;
this.queryInfo = queryInfo;
Expand Down Expand Up @@ -475,23 +482,11 @@ once, rather than every time you call fetchMore.`);
return Promise.resolve();
}

let { fetchPolicy = 'cache-first' } = this.options;
const reobserveOptions: Partial<WatchQueryOptions<TVariables, TData>> = {
fetchPolicy,
return this.reobserve({
// Reset options.fetchPolicy to its original value.
fetchPolicy: this.initialFetchPolicy,
variables,
};

if (fetchPolicy !== 'cache-first' &&
fetchPolicy !== 'no-cache' &&
fetchPolicy !== 'network-only') {
reobserveOptions.fetchPolicy = 'cache-and-network';
reobserveOptions.nextFetchPolicy = fetchPolicy;
}

return this.reobserve(
reobserveOptions,
NetworkStatus.setVariables,
);
}, NetworkStatus.setVariables);
}

public updateQuery<TVars = TVariables>(
Expand Down

0 comments on commit 94527c1

Please sign in to comment.