From 3751a8985c0ade553150522234a2d0159f4fc4b6 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Wed, 30 Jun 2021 16:11:55 +0100 Subject: [PATCH 1/2] (core) - Fix stale on reexectuted operation being skipped for cache-first --- .changeset/fresh-moose-exist.md | 5 +++++ packages/core/src/client.ts | 10 ++++------ 2 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 .changeset/fresh-moose-exist.md diff --git a/.changeset/fresh-moose-exist.md b/.changeset/fresh-moose-exist.md new file mode 100644 index 0000000000..f6e46408fd --- /dev/null +++ b/.changeset/fresh-moose-exist.md @@ -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. diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index 892c2535c1..ca3c05a1bd 100755 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -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 ( + filter( + op => op.kind === operation.kind && 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 })) ), From 706c52b6cb81ed8cf6d939238be8aaae6a75f689 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Wed, 30 Jun 2021 16:57:14 +0100 Subject: [PATCH 2/2] Fix stale reexecution logic being applied to subscriptions --- packages/core/src/client.test.ts | 15 ++++++++++----- packages/core/src/client.ts | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/core/src/client.test.ts b/packages/core/src/client.test.ts index 0e4e84a8d4..98df50bd1b 100755 --- a/packages/core/src/client.test.ts +++ b/packages/core/src/client.test.ts @@ -6,7 +6,9 @@ import { Source, delay, map, + never, pipe, + merge, subscribe, publish, filter, @@ -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({ diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index ca3c05a1bd..754035f27d 100755 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -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); } @@ -217,7 +217,7 @@ export const Client: new (opts: ClientOptions) => Client = function Client( operations$, filter( op => - op.kind === operation.kind && + op.kind === 'query' && op.key === operation.key && op.context.requestPolicy !== 'cache-only' ),