Skip to content

Commit

Permalink
Fix merging of toBeAdded type policies (#8361)
Browse files Browse the repository at this point in the history
Fixes #7535.
  • Loading branch information
Banou26 authored Jun 8, 2021
1 parent fc6f729 commit b6c18b2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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. <br/>
[@Banou26](https://github.com/Banou26) in [#8361](https://github.com/apollographql/apollo-client/pull/8361)

## Apollo Client 3.3.19

### Bug fixes
Expand Down
23 changes: 23 additions & 0 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,29 @@ describe("type policies", function () {
})).toBe("MotionPicture::3993d4118143");
});

it("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: {
Expand Down
7 changes: 5 additions & 2 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
isReference,
getStoreKeyName,
canUseWeakMap,
compact,
} from '../../utilities';
import { IdGetter, ReadMergeModifyContext, MergeInfo } from "./types";
import {
Expand Down Expand Up @@ -545,7 +544,11 @@ export class Policies {

const inbox = this.toBeAdded[typename];
if (inbox && inbox.length) {
this.updateTypePolicy(typename, compact(...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];
Expand Down

0 comments on commit b6c18b2

Please sign in to comment.