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

Support inheritance of typePolicies, according to possibleTypes. #7065

Merged
merged 6 commits into from
Sep 24, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

## Improvements

- Support inheritance of type and field policies, according to `possibleTypes`. <br/>
[@benjamn](https://github.com/benjamn) in [#7065](https://github.com/apollographql/apollo-client/pull/7065)

- Shallow-merge `options.variables` when combining existing or default options with newly-provided options, so new variables do not completely overwrite existing variables. <br/>
[@amannn](https://github.com/amannn) in [#6927](https://github.com/apollographql/apollo-client/pull/6927)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-client.cjs.min.js",
"maxSize": "24.75 kB"
"maxSize": "24.9 kB"
}
],
"peerDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/policies.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -977,3 +977,48 @@ Object {
},
}
`;

exports[`type policies support inheritance 1`] = `
Object {
"Cobra:{\\"tagId\\":\\"Egypt30BC\\"}": Object {
"__typename": "Cobra",
"scientificName": "naja haje",
"tagId": "Egypt30BC",
"venomous": true,
},
"Cottonmouth:{\\"tagId\\":\\"CM420\\"}": Object {
"__typename": "Cottonmouth",
"scientificName": "agkistrodon piscivorus",
"tagId": "CM420",
"venomous": true,
},
"Python:{\\"tagId\\":\\"BigHug4U\\"}": Object {
"__typename": "Python",
"scientificName": "malayopython reticulatus",
"tagId": "BigHug4U",
"venomous": false,
},
"ROOT_QUERY": Object {
"__typename": "Query",
"reptiles": Array [
Object {
"__ref": "Turtle:{\\"tagId\\":\\"RedEaredSlider42\\"}",
},
Object {
"__ref": "Python:{\\"tagId\\":\\"BigHug4U\\"}",
},
Object {
"__ref": "Cobra:{\\"tagId\\":\\"Egypt30BC\\"}",
},
Object {
"__ref": "Cottonmouth:{\\"tagId\\":\\"CM420\\"}",
},
],
},
"Turtle:{\\"tagId\\":\\"RedEaredSlider42\\"}": Object {
"__typename": "Turtle",
"scientificName": "trachemys scripta elegans",
"tagId": "RedEaredSlider42",
},
}
`;
128 changes: 128 additions & 0 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,134 @@ describe("type policies", function () {
})).toBe("MotionPicture::3993d4118143");
});

it("support inheritance", function () {
const cache = new InMemoryCache({
possibleTypes: {
Reptile: ["Snake", "Turtle"],
Snake: ["Python", "Viper", "Cobra"],
Viper: ["Cottonmouth"],
},

typePolicies: {
Reptile: {
keyFields: ["tagId"],

fields: {
scientificName: {
merge(_, incoming) {
// Normalize all scientific names to lower case.
return incoming.toLowerCase();
},
},
},
},

Snake: {
fields: {
// Default to a truthy non-boolean value if we don't know
// whether this snake is venomous.
venomous(status = "unknown") {
return status;
},
},
},
},
});

const query: TypedDocumentNode<{
reptiles: Record<string, any>[];
}> = gql`
query {
reptiles {
tagId
scientificName
... on Snake {
venomous
}
}
}
`;

const reptiles = [
{
__typename: "Turtle",
tagId: "RedEaredSlider42",
scientificName: "Trachemys scripta elegans",
},
{
__typename: "Python",
tagId: "BigHug4U",
scientificName: "Malayopython reticulatus",
venomous: false,
},
{
__typename: "Cobra",
tagId: "Egypt30BC",
scientificName: "Naja haje",
venomous: true,
},
{
__typename: "Cottonmouth",
tagId: "CM420",
scientificName: "Agkistrodon piscivorus",
venomous: true,
},
];

cache.writeQuery({
query,
data: { reptiles },
});

expect(cache.extract()).toMatchSnapshot();

const result1 = cache.readQuery({ query })!;
expect(result1).toEqual({
reptiles: reptiles.map(reptile => ({
...reptile,
scientificName: reptile.scientificName.toLowerCase(),
})),
});

const cmId = cache.identify({
__typename: "Cottonmouth",
tagId: "CM420",
});

expect(cache.evict({
id: cmId,
fieldName: "venomous",
})).toBe(true);

const result2 = cache.readQuery({ query })!;

result2.reptiles.forEach((reptile, i) => {
if (reptile.__typename === "Cottonmouth") {
expect(reptile).not.toBe(result1.reptiles[i]);
expect(reptile).not.toEqual(result1.reptiles[i]);
expect(reptile).toEqual({
__typename: "Cottonmouth",
tagId: "CM420",
// This name has been normalized to lower case.
scientificName: "agkistrodon piscivorus",
// Venomosity status has been set to a default value.
venomous: "unknown",
});
} else {
expect(reptile).toBe(result1.reptiles[i]);
}
});

cache.policies.addPossibleTypes({
Viper: ["DeathAdder"],
});

expect(cache.identify({
__typename: "DeathAdder",
tagId: "LethalAbacus666",
})).toBe('DeathAdder:{"tagId":"LethalAbacus666"}');
});

describe("field policies", function () {
it("can filter key arguments", function () {
const cache = new InMemoryCache({
Expand Down
Loading