Skip to content

Commit

Permalink
Add tests of result cache invalidation and LRU eviction (#8169)
Browse files Browse the repository at this point in the history
  • Loading branch information
sofianhn committed May 11, 2021
1 parent 9d72340 commit 605256b
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 5 deletions.
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -81,7 +81,7 @@
"fast-json-stable-stringify": "^2.0.0",
"graphql-tag": "^2.12.3",
"hoist-non-react-statics": "^3.3.2",
"optimism": "^0.15.0",
"optimism": "^0.16.1",
"prop-types": "^15.7.2",
"symbol-observable": "^2.0.0",
"ts-invariant": "^0.7.3",
Expand Down
183 changes: 183 additions & 0 deletions src/__tests__/resultCacheCleaning.ts
@@ -0,0 +1,183 @@
import gql from "graphql-tag";

import { ApolloClient } from "../core";

import { SchemaLink } from "../link/schema";

import { InMemoryCache, NormalizedCacheObject } from "../cache";
import { makeExecutableSchema } from "graphql-tools";

describe("resultCache cleaning", () => {
const fragments = gql`
fragment user on User {
id
name
}
fragment reaction on Reaction {
id
type
author {
...user
}
}
fragment message on Message {
id
author {
...user
}
reactions {
...reaction
}
viewedBy {
...user
}
}
`;
const query = gql`
query getChat($id: ID!) {
chat(id: $id) {
id
name
members {
...user
}
messages {
...message
}
}
}
${{ ...fragments }}
`;
function uuid(label: string) {
return () =>
`${label}-${Math.random()
.toString(16)
.substr(2)}`;
}

function emptyList(len: number) {
return new Array(len).fill(true);
}
let client: ApolloClient<NormalizedCacheObject>;
beforeEach(() => {
const cache = new InMemoryCache();

client = new ApolloClient({
link: new SchemaLink({
schema: makeExecutableSchema({
typeDefs: `
type Query {
chat(id: ID!): Chat!
}
type Chat {
id: ID!
name: String!
messages: [Message!]!
members: [User!]!
}
type Message {
id: ID!
author: User!
reactions: [Reaction!]!
viewedBy: [User!]!
content: String!
}
type User {
id: ID!
name: String!
}
type Reaction {
id: ID!
type: String!
author: User!
}
`,
resolvers: {
Query: {
chat(_, { id }) {
return id;
},
},
Chat: {
id(id) {
return id;
},
name(id) {
return id;
},
messages() {
return emptyList(10);
},
members() {
return emptyList(10);
},
},
Message: {
id: uuid("Message"),
author() {
return { foo: true };
},
reactions() {
return emptyList(10);
},
viewedBy() {
return emptyList(10);
},
content: uuid("Message-Content"),
},
User: {
id: uuid("User"),
name: uuid("User.name"),
},
Reaction: {
id: uuid("Reaction"),
type: uuid("Reaction.type"),
author() {
return { foo: true };
},
},
},
}),
}),
cache,
});
});

afterEach(() => {
const storeReader = (client.cache as InMemoryCache)["storeReader"];
expect(storeReader["executeSubSelectedArray"].size).toBeGreaterThan(0);
expect(storeReader["executeSelectionSet"].size).toBeGreaterThan(0);
client.cache.evict({
id: "ROOT_QUERY",
});
client.cache.gc();
expect(storeReader["executeSubSelectedArray"].size).toEqual(0);
expect(storeReader["executeSelectionSet"].size).toEqual(0);
});

it(`empties all result caches after eviction - query`, async () => {
await client.query({
query,
variables: { id: 1 },
});
});

it(`empties all result caches after eviction - watchQuery`, async () => {
return new Promise<void>((r) => {
const observable = client.watchQuery({
query,
variables: { id: 1 },
});
const unsubscribe = observable.subscribe(() => {
unsubscribe.unsubscribe();
r();
});
});
});
});
3 changes: 2 additions & 1 deletion src/cache/inmemory/entityStore.ts
Expand Up @@ -536,7 +536,8 @@ class CacheGroup {
if (this.d) {
// TODO When storeFieldName === "__exists", we might want to consider
// sending a stronger signal: https://github.com/benjamn/optimism/pull/195
this.d.dirty(makeDepKey(dataId, storeFieldName));
const entityMethodName: any = storeFieldName === "__exists" ? "forget" : undefined;
this.d.dirty(makeDepKey(dataId, storeFieldName), entityMethodName);
}
}

Expand Down

0 comments on commit 605256b

Please sign in to comment.