Skip to content

Commit

Permalink
Add initialPolicy field to NextFetchPolicyContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 10, 2022
1 parent 60b0b70 commit 48c8a88
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3312,17 +3312,24 @@ describe('@connection', () => {

defaultOptions: {
watchQuery: {
nextFetchPolicy(fetchPolicy) {
nextFetchPolicy(fetchPolicy, context) {
expect(++nextFetchPolicyCallCount).toBe(1);
expect(this.query).toBe(query);
expect(fetchPolicy).toBe("cache-first");

expect(context.reason).toBe("after-fetch");
expect(context.observable).toBe(obs);
expect(context.options).toBe(obs.options);
expect(context.initialPolicy).toBe("cache-first");

// Usually options.nextFetchPolicy applies only once, but a
// nextFetchPolicy function can set this.nextFetchPolicy
// again to perform an additional transition.
this.nextFetchPolicy = fetchPolicy => {
++nextFetchPolicyCallCount;
return "cache-first";
};

return "cache-and-network";
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ once, rather than every time you call fetchMore.`);
options.fetchPolicy = options.nextFetchPolicy(fetchPolicy, {
reason,
options,
observableQuery: this,
observable: this,
initialPolicy: this.initialFetchPolicy,
});
} else if (reason === "variables-changed") {
options.fetchPolicy = this.initialFetchPolicy;
Expand Down
5 changes: 3 additions & 2 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ export interface WatchQueryOptions<TVariables = OperationVariables, TData = any>
export interface NextFetchPolicyContext<TData, TVariables> {
reason:
| "after-fetch"
| "variables-changed"
observableQuery: ObservableQuery;
| "variables-changed";
observable: ObservableQuery<TData, TVariables>;
options: WatchQueryOptions<TVariables, TData>;
initialPolicy: WatchQueryFetchPolicy;
}

export interface FetchMoreQueryOptions<TVariables, TData = any> {
Expand Down

0 comments on commit 48c8a88

Please sign in to comment.