-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
resultCacheCleaning.ts
186 lines (168 loc) · 3.5 KB
/
resultCacheCleaning.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { makeExecutableSchema } from "graphql-tools";
import { ApolloClient, Resolvers, gql } from "../core";
import { InMemoryCache, NormalizedCacheObject } from "../cache";
import { SchemaLink } from "../link/schema";
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);
}
const typeDefs = gql`
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!
}
`;
const resolvers: 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 };
},
},
};
let client: ApolloClient<NormalizedCacheObject>;
beforeEach(() => {
client = new ApolloClient({
cache: new InMemoryCache,
link: new SchemaLink({
schema: makeExecutableSchema({
typeDefs,
resolvers,
}),
}),
});
});
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();
});
});
});
});