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

Deep merge toBeAdded type policies #8361

Merged
merged 5 commits into from
Jun 8, 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
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