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

Automatically filter out dangling references from arrays. #6454

Merged
merged 3 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-client.cjs.min.js",
"maxSize": "24.2 kB"
"maxSize": "24.25 kB"
}
],
"peerDependencies": {
Expand Down
226 changes: 226 additions & 0 deletions src/cache/inmemory/__tests__/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,232 @@ describe('reading from the store', () => {
});
});

it("custom read functions can map/filter dangling references", () => {
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
ducks(existing: Reference[] = [], { canRead }) {
return existing.map(duck => canRead(duck) ? duck : null);
},
chickens(existing: Reference[] = [], { canRead }) {
return existing.map(chicken => canRead(chicken) ? chicken : {});
},
oxen(existing: Reference[] = [], { canRead }) {
return existing.filter(canRead);
},
},
},
},
});

cache.writeQuery({
query: gql`
query {
ducks { quacking }
chickens { inCoop }
oxen { gee haw }
}
`,
data: {
ducks: [
{ __typename: "Duck", id: 1, quacking: true },
{ __typename: "Duck", id: 2, quacking: false },
{ __typename: "Duck", id: 3, quacking: false },
],
chickens: [
{ __typename: "Chicken", id: 1, inCoop: true },
{ __typename: "Chicken", id: 2, inCoop: true },
{ __typename: "Chicken", id: 3, inCoop: false },
],
oxen: [
{ __typename: "Ox", id: 1, gee: true, haw: false },
{ __typename: "Ox", id: 2, gee: false, haw: true },
],
},
});

expect(cache.extract()).toEqual({
"Chicken:1": {
__typename: "Chicken",
id: 1,
inCoop: true,
},
"Chicken:2": {
__typename: "Chicken",
id: 2,
inCoop: true,
},
"Chicken:3": {
__typename: "Chicken",
id: 3,
inCoop: false,
},
"Duck:1": {
__typename: "Duck",
id: 1,
quacking: true,
},
"Duck:2": {
__typename: "Duck",
id: 2,
quacking: false,
},
"Duck:3": {
__typename: "Duck",
id: 3,
quacking: false,
},
"Ox:1": {
__typename: "Ox",
id: 1,
gee: true,
haw: false,
},
"Ox:2": {
__typename: "Ox",
id: 2,
gee: false,
haw: true,
},
ROOT_QUERY: {
__typename: "Query",
chickens: [
{ __ref: "Chicken:1" },
{ __ref: "Chicken:2" },
{ __ref: "Chicken:3" },
],
ducks: [
{ __ref: "Duck:1" },
{ __ref: "Duck:2" },
{ __ref: "Duck:3" },
],
oxen: [
{ __ref: "Ox:1" },
{ __ref: "Ox:2" },
],
},
});

function diffChickens() {
return cache.diff({
query: gql`query { chickens { id inCoop }}`,
optimistic: true,
});
}

expect(diffChickens()).toEqual({
complete: true,
optimistic: false,
result: {
chickens: [
{ __typename: "Chicken", id: 1, inCoop: true },
{ __typename: "Chicken", id: 2, inCoop: true },
{ __typename: "Chicken", id: 3, inCoop: false },
],
}
});

expect(cache.evict({
id: cache.identify({
__typename: "Chicken",
id: 2,
}),
})).toBe(true);

expect(diffChickens()).toEqual({
complete: false,
optimistic: false,
missing: [
expect.anything(),
expect.anything(),
],
result: {
chickens: [
{ __typename: "Chicken", id: 1, inCoop: true },
{},
{ __typename: "Chicken", id: 3, inCoop: false },
],
},
});

function diffDucks() {
return cache.diff({
query: gql`query { ducks { id quacking }}`,
optimistic: true,
});
}

expect(diffDucks()).toEqual({
complete: true,
optimistic: false,
result: {
ducks: [
{ __typename: "Duck", id: 1, quacking: true },
{ __typename: "Duck", id: 2, quacking: false },
{ __typename: "Duck", id: 3, quacking: false },
],
},
});

expect(cache.evict({
id: cache.identify({
__typename: "Duck",
id: 3,
}),
})).toBe(true);

// Returning null as a placeholder in a list is a way to indicate that
// a list element has been removed, without causing an incomplete
// diff, and without altering the positions of later elements.
expect(diffDucks()).toEqual({
complete: true,
optimistic: false,
result: {
ducks: [
{ __typename: "Duck", id: 1, quacking: true },
{ __typename: "Duck", id: 2, quacking: false },
null,
],
},
});

function diffOxen() {
return cache.diff({
query: gql`query { oxen { id gee haw }}`,
optimistic: true,
});
}

expect(diffOxen()).toEqual({
complete: true,
optimistic: false,
result: {
oxen: [
{ __typename: "Ox", id: 1, gee: true, haw: false },
{ __typename: "Ox", id: 2, gee: false, haw: true },
],
},
});

expect(cache.evict({
id: cache.identify({
__typename: "Ox",
id: 1,
}),
})).toBe(true);

expect(diffOxen()).toEqual({
complete: true,
optimistic: false,
result: {
oxen: [
{ __typename: "Ox", id: 2, gee: false, haw: true },
],
},
});
});

it("propagates eviction signals to parent queries", () => {
const cache = new InMemoryCache({
typePolicies: {
Expand Down
4 changes: 4 additions & 0 deletions src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ export class StoreReader {
return childResult.result;
}

if (field.selectionSet) {
array = array.filter(context.store.canRead);
Comment on lines +389 to +390
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that executeSubSelectedArray is result-cached (like executeSelectionSet), so this filtering only happens when we're recomputing the current result object.

Also note that the original array remains unfiltered in the cache. I described a possible strategy for permanently pruning the list in #6412 (comment), though I don't think that should be necessary in most cases.

}

array = array.map((item, i) => {
// null value in array
if (item === null) {
Expand Down