-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Don't allow fetchMore
or updates to polling for cache-only
queries and exclude from client.refetchQueries
#12712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't allow fetchMore
or updates to polling for cache-only
queries and exclude from client.refetchQueries
#12712
Conversation
🦋 Changeset detectedLatest commit: 0d15e5a The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
size-limit report 📦
|
This comment was marked as resolved.
This comment was marked as resolved.
src/__tests__/refetchQueries.ts
Outdated
).rejects.toEqual( | ||
new InvariantError( | ||
"Cannot execute `refetch` for 'cache-only' query 'C'. Please use a different fetch policy." | ||
) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you have 10 ObservableQuery
instances with the same query, but different variables and fetchPolicies
and a single one with cache-only
?
I think this shouldn't error. It should just be skipped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. Should we at least warn if you provide a specific query to include
that is a cache-only
query? Or just silently not include it?
src/core/ObservableQuery.ts
Outdated
if (pollInterval && fetchPolicy === "cache-only") { | ||
invariant.warn( | ||
"Cannot poll on 'cache-only' query '%s' and as such, polling is disabled. Please use a different fetch policy.", | ||
getOperationName(this.query, "(anonymous)") | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reobserve
, and as a result updatePolling
can be called quite often - this shouldn't warn every time. Maybe only once per ObservableQuery
instance?
src/core/QueryManager.ts
Outdated
) { | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) { | |
return; | |
} | |
if (oq.options.fetchPolicy === "cache-only") { | |
return; | |
} | |
As a result of my previous comment, this should probably do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One follow-up question might be if we should also disallow it for standby
queries.
I started doing this for I think limiting it to just |
src/core/ObservableQuery.ts
Outdated
); | ||
|
||
this.setError(error); | ||
return Promise.reject(error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just throw and avoid emitting an error for fetchMore
.
src/core/ObservableQuery.ts
Outdated
@@ -1683,6 +1716,18 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`, | |||
}); | |||
} | |||
|
|||
private setError(error: ErrorLike) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can go away
src/core/ObservableQuery.ts
Outdated
@@ -660,6 +665,23 @@ export class ObservableQuery< | |||
public refetch( | |||
variables?: Partial<TVariables> | |||
): ObservableQuery.ResultPromise<QueryResult<TData>> { | |||
const { fetchPolicy } = this.options; | |||
|
|||
if (fetchPolicy === "cache-only") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this. We will allow refetch
with cache-only queries.
@@ -1954,6 +2139,44 @@ describe("ObservableQuery", () => { | |||
expect(observable.options.fetchPolicy).toBe("no-cache"); | |||
}); | |||
|
|||
it("does not allow refetch on cache-only query", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update to allow this
src/core/__tests__/fetchPolicies.ts
Outdated
@@ -599,7 +600,7 @@ describe("cache-first", () => { | |||
}); | |||
|
|||
describe("cache-only", () => { | |||
it("allows explicit refetch to happen", async () => { | |||
it("does not allow refetch", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert back
@@ -6080,6 +6080,80 @@ test("rerenders with data: undefined when changing variables and an error is ret | |||
await expect(takeSnapshot).not.toRerender(); | |||
}); | |||
|
|||
test("throws when calling `refetch` on a cache-only query", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow this
@@ -6418,6 +6464,64 @@ describe("useQuery Hook", () => { | |||
await expect(takeSnapshot).not.toRerender(); | |||
}); | |||
|
|||
it("does not allow refetch on a cache-only query", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverse this
refetch
, fetchMore
or updates to polling for cache-only
queriesfetchMore
or updates to polling for cache-only
queries and exclude from client.refetchQueries
Co-authored-by: Lenz Weber-Tronic <lorenz.weber-tronic@apollographql.com>
Co-authored-by: Lenz Weber-Tronic <lorenz.weber-tronic@apollographql.com>
…ch-standby-cache-only
Fixes #11997
Throws an error when
fetchMore
is called on acache-only
query. Additionally a warning will be emitted when setting a poll interval on acache-only
query and polling will be cancelled.cache-only
queries are now excluded fromrefetchQueries
when usinginclude: 'all' | 'active'
and when listed as a query ininclude
.cache-only
queries are also excluded fromclient.reFetchObservableQueries
whenincludeStandby
istrue
.