Skip to content

Commit

Permalink
Improve ObservableQuery options.{f,initialF}etchPolicy default logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed May 3, 2022
1 parent 21f1faa commit 77119b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/fetchMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ describe('fetchMore on an observable query', () => {
},
});

expect(observable.options.fetchPolicy).toBeUndefined();
expect(observable.options.fetchPolicy).toBe("cache-first");
});

} else if (count === 2) {
Expand Down
23 changes: 21 additions & 2 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,31 @@ export class ObservableQuery<
// active state
this.isTornDown = false;

const {
watchQuery: {
fetchPolicy: defaultFetchPolicy = "cache-first",
} = {},
} = queryManager.defaultOptions;

const {
fetchPolicy = defaultFetchPolicy,
initialFetchPolicy = (
// Make sure we don't store "standby" as the initialFetchPolicy.
fetchPolicy === "standby" ? defaultFetchPolicy : fetchPolicy
),
} = options;

this.options = {
...options,

// Remember the initial options.fetchPolicy so we can revert back to this
// policy when variables change. This information can also be specified
// (or overridden) by providing options.initialFetchPolicy explicitly.
initialFetchPolicy: options.fetchPolicy || "cache-first",
...options,
initialFetchPolicy,

// This ensures this.options.fetchPolicy always has a string value, in
// case options.fetchPolicy was not provided.
fetchPolicy,
};

this.queryId = queryInfo.queryId || queryManager.generateQueryId();
Expand Down

0 comments on commit 77119b0

Please sign in to comment.