From 0c4b52504029a91e22fa607a31dc2f9bf747a2e4 Mon Sep 17 00:00:00 2001 From: Banou Date: Tue, 8 Jun 2021 19:03:49 +0200 Subject: [PATCH 1/5] =?UTF-8?q?test:=20=E2=9C=85add=20`addTypePolicies`=20?= =?UTF-8?q?case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cache/inmemory/__tests__/policies.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/cache/inmemory/__tests__/policies.ts b/src/cache/inmemory/__tests__/policies.ts index beb5976d58a..4aef2a66b21 100644 --- a/src/cache/inmemory/__tests__/policies.ts +++ b/src/cache/inmemory/__tests__/policies.ts @@ -318,6 +318,29 @@ describe("type policies", function () { })).toBe("MotionPicture::3993d4118143"); }); + it.only("does not remove previous typePolicies", function () { + const cache = new InMemoryCache({ + typePolicies: { + Query: { + fields: { + foo: () => 'foo' + } + }, + }, + }); + + cache.policies.addTypePolicies({ + Query: { + fields: { + bar: () => 'bar' + } + }, + }); + + expect(cache.readQuery({ query: gql` { foo } ` })).toEqual({foo: "foo"}); + expect(cache.readQuery({ query: gql` { bar } ` })).toEqual({bar: "bar"}); + }); + it("support inheritance", function () { const cache = new InMemoryCache({ possibleTypes: { From 2049e2e040f00dc063a5be8401276b299c2b7373 Mon Sep 17 00:00:00 2001 From: Banou Date: Tue, 8 Jun 2021 20:11:16 +0200 Subject: [PATCH 2/5] =?UTF-8?q?fix(cache):=20=F0=9F=90=9Bmerge=20policies?= =?UTF-8?q?=20instead=20of=20compacting=20them=20inside=20of=20getTypePoli?= =?UTF-8?q?cy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cache/inmemory/policies.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cache/inmemory/policies.ts b/src/cache/inmemory/policies.ts index 76ffa0597cc..b9acff37dc9 100644 --- a/src/cache/inmemory/policies.ts +++ b/src/cache/inmemory/policies.ts @@ -21,7 +21,7 @@ import { isReference, getStoreKeyName, canUseWeakMap, - compact, + mergeDeep, } from '../../utilities'; import { IdGetter, ReadMergeModifyContext, MergeInfo } from "./types"; import { @@ -545,7 +545,7 @@ export class Policies { const inbox = this.toBeAdded[typename]; if (inbox && inbox.length) { - this.updateTypePolicy(typename, compact(...inbox.splice(0))); + this.updateTypePolicy(typename, mergeDeep(...inbox.splice(0))); } return this.typePolicies[typename]; From efbaf99c96caaecbe81d14b56d147dc0af56af3e Mon Sep 17 00:00:00 2001 From: Banou Date: Tue, 8 Jun 2021 20:12:09 +0200 Subject: [PATCH 3/5] =?UTF-8?q?test:=20=E2=9C=85remove=20`only`=20test=20f?= =?UTF-8?q?or=20the=20policies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cache/inmemory/__tests__/policies.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache/inmemory/__tests__/policies.ts b/src/cache/inmemory/__tests__/policies.ts index 4aef2a66b21..7787beee225 100644 --- a/src/cache/inmemory/__tests__/policies.ts +++ b/src/cache/inmemory/__tests__/policies.ts @@ -318,7 +318,7 @@ describe("type policies", function () { })).toBe("MotionPicture::3993d4118143"); }); - it.only("does not remove previous typePolicies", function () { + it("does not remove previous typePolicies", function () { const cache = new InMemoryCache({ typePolicies: { Query: { From 44367288269c01185fc2187e60ac3868163a112b Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 8 Jun 2021 15:15:03 -0400 Subject: [PATCH 4/5] Call updateTypePolicy iteratively, rather than compacting policies. --- src/cache/inmemory/policies.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cache/inmemory/policies.ts b/src/cache/inmemory/policies.ts index b9acff37dc9..a7aca3a2f9d 100644 --- a/src/cache/inmemory/policies.ts +++ b/src/cache/inmemory/policies.ts @@ -21,7 +21,6 @@ import { isReference, getStoreKeyName, canUseWeakMap, - mergeDeep, } from '../../utilities'; import { IdGetter, ReadMergeModifyContext, MergeInfo } from "./types"; import { @@ -545,7 +544,11 @@ export class Policies { const inbox = this.toBeAdded[typename]; if (inbox && inbox.length) { - this.updateTypePolicy(typename, mergeDeep(...inbox.splice(0))); + // Merge the pending policies into this.typePolicies, in the order they + // were originally passed to addTypePolicy. + inbox.splice(0).forEach(policy => { + this.updateTypePolicy(typename, policy); + }); } return this.typePolicies[typename]; From a009de18b7b7364d2493b37a6687394bfcb71e68 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 8 Jun 2021 15:21:31 -0400 Subject: [PATCH 5/5] Mention PR #8361 in CHANGELOG.md. --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7ae9f3bb10..319ae46c739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## Apollo Client 3.3.20 (not yet released) + +### Bug fixes + +- Fix policy merging bug when calling `cache.policies.addTypePolicies` multiple times for the same type policy.
+ [@Banou26](https://github.com/Banou26) in [#8361](https://github.com/apollographql/apollo-client/pull/8361) + ## Apollo Client 3.3.19 ### Bug fixes