-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachePlugin.ts
393 lines (339 loc) · 13.3 KB
/
CachePlugin.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import {
defaultFieldResolver,
DirectiveNode,
DocumentNode,
ExecutionArgs,
getNamedType,
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLSchema,
OperationDefinitionNode,
ResponsePath
} from "graphql";
import { SchemaTransform, mapSchema, MapperKind, getDirectives } from "@graphql-tools/utils";
import gql from "graphql-tag";
import canonicalize from "canonicalize";
import { createHash } from "crypto";
import { Executable, GraphQLPlugin, MaybePromise, ExecutableResult } from "./GraphQLPlugin";
import { ExecutionResult } from "graphql/execution/execute";
import { ServiceContext } from ".";
type CacheScope = "PUBLIC" | "PRIVATE";
type CacheTTL = "SHORT" | "MID" | "LONG";
/**
* Interface of the cache service used to serve the plugin.
*/
export interface StringCache {
get(key: string): string | undefined;
put(key: string, value: string, ttl: number): void;
}
/**
* Response cache plugin, inspired by and adapted from the Apollo response cache.
*
* Use the `@cache()` directive on field declarations to set up cache behavior.
* The full response will be cached according to the most restrictive setting
* found while traversing the query.
*
* To bypass the cache during query execution, add the `@noCache` directive
* to the query.
*/
export class CachePlugin implements GraphQLPlugin {
private readonly cache: StringCache;
private readonly ttlToSeconds: { [key in CacheTTL]: number } = {
SHORT: 30,
MID: 300,
LONG: 3600,
};
private readonly keyExtractor?: CacheKeyExtractor;
private readonly sessionIDExtractor?: SessionIDExtractor;
constructor(conf?: {
cache?: StringCache;
ttlConfig?: { [key in CacheTTL]: number };
extraCacheKeys?: CacheKeyExtractor;
sessionID?: SessionIDExtractor;
}) {
this.ttlToSeconds = {
...this.ttlToSeconds,
...conf?.ttlConfig
};
this.cache = conf?.cache || new InMemoryCache();
this.keyExtractor = conf?.extraCacheKeys;
this.sessionIDExtractor = conf?.sessionID;
}
directives(): (DocumentNode | string)[] {
return [gql`
enum CacheScope {
PUBLIC,
PRIVATE
}
enum CacheTTL {
SHORT,
MID,
LONG
}
directive @cache(ttl: CacheTTL!, scope: CacheScope! = PUBLIC) on FIELD_DEFINITION | OBJECT | INTERFACE
directive @noCache on QUERY
`];
}
transforms(): SchemaTransform[] {
return [
(schema: GraphQLSchema) => mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const directives = getDirectives(schema, fieldConfig);
if ("cache" in directives) {
const { resolve = defaultFieldResolver } = fieldConfig;
fieldConfig.resolve = async (source, args, context: CachePluginContext, info) => {
const hints = context.cachePlugin.hints;
let hint: CacheHint = {};
const ttlDefault = 0;
// If this field's resolver returns an object or interface, look for
// hints on that return type.
const targetType = getNamedType(info.returnType);
if (targetType.astNode &&
(
targetType instanceof GraphQLObjectType ||
targetType instanceof GraphQLInterfaceType
)) {
hint = mergeHints(
hint,
this.hintFromDirectives(targetType.astNode.directives)
);
}
// Look for hints on the field itself (on its parent type), taking
// precedence over previously calculated hints.
const fieldDef = info.parentType.getFields()[info.fieldName];
if (fieldDef.astNode) {
hint = mergeHints(
hint,
this.hintFromDirectives(fieldDef.astNode.directives),
);
}
// If this resolver returns an object or is a root field and we haven't
// seen an explicit ttl hint, set the ttl to 0 (uncached) or the
// default if specified in the constructor. (Non-object fields by
// default are assumed to inherit their cacheability from their parents.
// But on the other hand, while root non-object fields can get explicit
// hints from their definition on the Query/Mutation object, if that
// doesn't exist then there's no parent field that would assign the
// default ttl, so we do it here.)
if (
(targetType instanceof GraphQLObjectType ||
targetType instanceof GraphQLInterfaceType ||
!info.path.prev) &&
hint.ttl === undefined
) {
hint.ttl = ttlDefault;
}
if (hint.ttl !== undefined || hint.scope !== undefined) {
addHint(hints, info.path, hint);
}
return resolve(source, args, context, info);
};
}
return fieldConfig;
},
}),
];
}
wrapper = (next: Executable): Executable => {
return async (args: ExecutionArgs) => {
const operation: OperationDefinitionNode | undefined =
args.document.definitions
.find((def): def is OperationDefinitionNode => def.kind === "OperationDefinition");
const isQuery = operation?.operation === "query";
const shouldCache = !(operation?.directives?.find((directive) => directive.name.value === "noCache") != undefined);
let keyData: KeyData = {};
let hints: MapResponsePathHints | undefined;
let sessionID: string | undefined;
if (isQuery) {
const source = args.document.loc?.source.body;
const variables = args.variableValues;
const operationName = args.operationName;
const context: CachePluginContext = args.contextValue;
sessionID = await this.sessionIDExtractor?.(context);
const extraKeys = await this.keyExtractor?.(context) ?? {};
keyData = {
source,
variables,
operationName,
extraKeys,
sessionID,
};
if (shouldCache) {
const cached = this.cacheGet(keyData, sessionID);
if (cached) {
return cached;
}
}
hints = new Map();
context.cachePlugin = { hints };
}
const result = await next(args);
if (!result.errors && isQuery && hints) {
const policy = computeOverallCachePolicy(hints);
if (policy) {
if (policy.scope === "PRIVATE") {
if (!this.sessionIDExtractor) {
throw new Error("Cannot cache private scope items without session ID");
}
if (!sessionID) {
// Do not cache private data for non-logged in users.
return result;
}
keyData.sessionMode = SessionMode.sessionPrivate;
} else {
if (sessionID) {
keyData.sessionMode = SessionMode.sessionPublic;
} else {
keyData.sessionMode = SessionMode.noSession;
}
}
this.cachePut(keyData, policy.ttl, result);
}
}
return result;
};
}
private cachePut(keyData: KeyData, ttl: number, result: ExecutionResult): void {
const key = cacheKeyFromData(keyData);
const serialized = JSON.stringify(result);
this.cache.put(key, serialized, ttl);
}
private cacheGet(keyData: KeyData, sessionID: string | undefined): ExecutableResult | undefined {
let cachedString: string | undefined;
if (!sessionID) {
keyData.sessionMode = SessionMode.noSession;
const key = cacheKeyFromData(keyData);
cachedString = this.cache.get(key);
} else {
// We don't know yet if the scope is private, so we have to check that first
keyData.sessionMode = SessionMode.sessionPrivate;
const key = cacheKeyFromData(keyData);
cachedString = this.cache.get(key);
if (!cachedString) {
keyData.sessionMode = SessionMode.sessionPublic;
const key = cacheKeyFromData(keyData);
cachedString = this.cache.get(key);
}
}
return cachedString ? JSON.parse(cachedString) : undefined;
}
private hintFromDirectives(
directives: ReadonlyArray<DirectiveNode> | undefined,
): CacheHint | undefined {
if (!directives) return undefined;
const cacheControlDirective = directives.find(
directive => directive.name.value === "cache",
);
if (!cacheControlDirective?.arguments) return undefined;
const ttlArgument = cacheControlDirective.arguments.find(
argument => argument.name.value === "ttl",
);
const scopeArgument = cacheControlDirective.arguments.find(
argument => argument.name.value === "scope",
);
const ttl = ttlArgument?.value?.kind === "EnumValue"
? this.ttlToSeconds[ttlArgument.value.value as CacheTTL]
: undefined;
const scope = scopeArgument?.value?.kind === "EnumValue"
? (scopeArgument.value.value as CacheScope)
: "PUBLIC";
return {
ttl,
scope,
};
}
}
enum SessionMode {
noSession,
sessionPrivate,
sessionPublic,
}
type KeyData = Record<string, unknown> & { sessionMode?: SessionMode };
type CacheKeyExtractor = (ctx: ServiceContext) => MaybePromise<Record<string, unknown>>;
type SessionIDExtractor = (ctx: ServiceContext) => MaybePromise<string>;
interface CachePluginContext extends ServiceContext {
cachePlugin: {
hints: MapResponsePathHints;
}
}
interface CacheHint {
ttl?: number;
scope?: CacheScope;
}
type MapResponsePathHints = Map<ResponsePath, CacheHint>;
function cacheKeyFromData(keyData: KeyData): string {
const keyString = canonicalize(keyData);
return sha(keyString);
}
function sha(s: string) {
return createHash("sha256")
.update(s)
.digest("hex");
}
function mergeHints(
hint: CacheHint,
otherHint: CacheHint | undefined,
): CacheHint {
if (!otherHint) return hint;
return {
ttl: otherHint.ttl !== undefined ? otherHint.ttl : hint.ttl,
scope: otherHint.scope || hint.scope,
};
}
function computeOverallCachePolicy(
hints: MapResponsePathHints,
): Required<CacheHint> | undefined {
let lowestMaxAge: number | undefined = undefined;
let scope: CacheScope = "PUBLIC";
for (const hint of hints.values()) {
if (hint.ttl !== undefined) {
lowestMaxAge =
lowestMaxAge !== undefined
? Math.min(lowestMaxAge, hint.ttl)
: hint.ttl;
}
if (hint.scope === "PRIVATE") {
scope = "PRIVATE";
}
}
return lowestMaxAge
? {
ttl: lowestMaxAge,
scope,
}
: undefined;
}
function addHint(hints: MapResponsePathHints, path: ResponsePath, hint: CacheHint) {
const existingCacheHint = hints.get(path);
if (existingCacheHint) {
hints.set(path, mergeHints(existingCacheHint, hint));
} else {
hints.set(path, hint);
}
}
interface CacheEntry {
value: string;
expiry: number;
}
class InMemoryCache implements StringCache {
private entries = new Map<string, CacheEntry>();
get(key: string): string | undefined {
const found = this.entries.get(key);
if (found) {
const now = new Date();
if (now.getTime() > found.expiry) {
this.entries.delete(key);
return undefined;
}
return found.value;
}
return undefined;
}
put(key: string, value: string, ttl: number) {
this.entries.set(key, {
value,
expiry: new Date().getTime() + ttl * 1000,
});
}
}
export default CachePlugin;