-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
184 lines (162 loc) · 5.2 KB
/
index.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
import {ApolloClient, gql, HttpLink} from '@apollo/client/core';
import {InMemoryCache, NormalizedCacheObject, Reference, ApolloCache} from '@apollo/client/cache';
import {entries as Entries, entriesVariables as EntriesVariables} from './gql/__gen_gql/entries';
import {readFileSync} from 'fs';
import {resolve} from 'path';
import fetch from 'node-fetch';
import { removeEntryVariables as RemoveEntryVariables, removeEntry as RemoveEntry } from './gql/__gen_gql/removeEntry';
import { removeEntryFailedVariables as RemoveEntryFailedVariables, removeEntryFailed as RemoveEntryFailed } from './gql/__gen_gql/removeEntryFailed';
import {inspect} from 'util';
import chalk from 'chalk'
inspect.defaultOptions.depth = null;
const uri = 'http://localhost:4000/';
const getGql = (path: string) => gql(readFileSync(resolve(__dirname, path), 'utf-8'));
const entriesQuery = getGql('gql/entries.gql');
const removeEntryMutation = getGql('gql/removeEntry.gql');
const removeEntryFailedMutation = getGql('gql/removeEntryFailed.gql');
async function queryEntries (variables: EntriesVariables, client: ApolloClient<NormalizedCacheObject>): Promise<void> {
client.query<Entries, EntriesVariables>({
query: entriesQuery,
variables,
fetchPolicy: 'network-only'
});
}
async function queryEntriesFromCache (variables: EntriesVariables, client: ApolloClient<NormalizedCacheObject>): Promise<void> {
const {data, partial} = await client.query<Entries, EntriesVariables>({
query: entriesQuery,
variables,
fetchPolicy: 'cache-only'
});
console.log('');
console.log('variables:')
console.log(variables)
console.log('data:')
console.log(data)
console.log('partial:')
console.log(partial)
}
function watchQueryEntries (variables: EntriesVariables, client: ApolloClient<NormalizedCacheObject>): void {
client.watchQuery<Entries, EntriesVariables>({
query: entriesQuery,
variables,
fetchPolicy: 'cache-only'
}).subscribe({
next({data, partial}) {
console.log('');
console.log(chalk.magenta('entries watchQuery'));
console.log('variables:')
console.log(variables)
console.log('data:')
console.log(data)
console.log('partial:')
console.log(partial)
}
});
}
async function removeEntryFailed (variables: RemoveEntryFailedVariables, client: ApolloClient<NormalizedCacheObject>): Promise<void> {
await client.mutate<RemoveEntryFailed, RemoveEntryFailedVariables>({
mutation: removeEntryFailedMutation,
variables,
optimisticResponse: {
removeEntryFailed: variables.id
},
update (cache) {
cache.modify({
fields: {
entries(value: Reference[], {readField}) {
return value.filter(entry => readField('id', entry) !== variables.id)
}
}
});
cache.gc();
}
})
}
async function removeEntry (variables: RemoveEntryVariables, client: ApolloClient<NormalizedCacheObject>): Promise<void> {
await client.mutate<RemoveEntry, RemoveEntryVariables>({
mutation: removeEntryMutation,
variables,
optimisticResponse: {
removeEntry: variables.id
},
update (cache) {
cache.modify({
fields: {
entries(value: Reference[], {readField}) {
return value.filter(entry => readField('id', entry) !== variables.id)
},
}
});
cache.gc();
}
})
}
function removeCachedEntriesQueries (client: ApolloClient<NormalizedCacheObject>): void {
client.cache.modify({
fields: {
entries() {
return undefined;
}
}
});
client.cache.gc();
}
async function wait (ms: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
function logCache (cache: ApolloCache<NormalizedCacheObject>) {
console.log('');
console.log(chalk.yellow('Cache:'));
// @ts-ignore
console.log(cache.data.data);
}
async function main(): Promise<void> {
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
entries: {
keyArgs: ["search"]
}
}
}
}
})
const client = new ApolloClient({
cache,
link: new HttpLink({
uri,
fetch: fetch as unknown as WindowOrWorkerGlobalScope['fetch']
})
});
console.log(chalk.blue('query entries from cache'));
await queryEntriesFromCache({search: '1'}, client);
console.log(chalk.blue('Setup watchQueries'));
// Setup cache-only watchQueries
watchQueryEntries({search: '1'}, client);
await wait(2000);
watchQueryEntries({search: '2'}, client);
await wait(2000);
logCache(client.cache);
// Trigger network-only entries queries
console.log(chalk.blue('\nTrigger queries'));
await queryEntries({search: '1'}, client);
await wait(2000);
await queryEntries({search: '2'}, client);
await wait(2000);
logCache(client.cache);
console.log(chalk.blue('\nremoveEntry'));
await removeEntry({id: '1'}, client);
logCache(client.cache);
await wait(2000);
console.log(chalk.blue('\nremoveEntryFailed'));
await removeEntryFailed({id: '2'}, client).catch(() => {});
logCache(client.cache);
await wait(2000);
console.log(chalk.blue('\nremoveCachedEntriesQueries'))
removeCachedEntriesQueries(client);
logCache(client.cache);
}
main();