Skip to content
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

(core) - Fix stale on reexecute being skipped for cache-first #1755

Merged
merged 2 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-moose-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix accidental change in passive `stale: true`, where a `cache-first` operation issued by Graphcache wouldn't yield an affected query and update its result to reflect the loading state with `stale: true`. This is a regression from `v2.1.0` and mostly becomes unexpected when `cache.invalidate(...)` is used.
15 changes: 10 additions & 5 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
Source,
delay,
map,
never,
pipe,
merge,
subscribe,
publish,
filter,
Expand Down Expand Up @@ -869,11 +871,14 @@ describe('shared sources behavior', () => {

it('does nothing when operation is a subscription has been emitted yet', () => {
const exchange: Exchange = () => ops$ => {
return pipe(
ops$,
map(op => ({ data: 1, operation: op })),
take(1)
);
return merge([
pipe(
ops$,
map(op => ({ data: 1, operation: op })),
take(1)
),
never,
]);
};

const client = createClient({
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
)
),
switchMap(result => {
if (result.stale) {
if (operation.kind !== 'query' || result.stale) {
return fromValue(result);
}

Expand All @@ -215,14 +215,12 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
// Mark a result as stale when a new operation is sent for it
pipe(
operations$,
filter(op => {
return (
op.kind === operation.kind &&
filter(
op =>
op.kind === 'query' &&
op.key === operation.key &&
(op.context.requestPolicy === 'network-only' ||
op.context.requestPolicy === 'cache-and-network')
);
}),
op.context.requestPolicy !== 'cache-only'
),
take(1),
map(() => ({ ...result, stale: true }))
),
Expand Down