/
collectCacheControlHints.ts
56 lines (52 loc) 路 1.68 KB
/
collectCacheControlHints.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
import type { CacheHint } from '@apollo/cache-control-types';
import type { GraphQLSchema } from 'graphql';
import { ApolloServer } from '../../..';
import {
ApolloServerPluginCacheControl,
ApolloServerPluginCacheControlOptions,
} from '../../../plugin/cacheControl';
export async function collectCacheControlHintsAndPolicyIfCacheable(
schema: GraphQLSchema,
source: string,
options: ApolloServerPluginCacheControlOptions = {},
): Promise<{
hints: Map<string, CacheHint>;
policyIfCacheable: Required<CacheHint> | null;
}> {
const cacheHints = new Map<string, CacheHint>();
const server = new ApolloServer({
schema,
plugins: [
ApolloServerPluginCacheControl({
...options,
__testing__cacheHints: cacheHints,
}),
{
async requestDidStart() {
return {
async willSendResponse({ response, overallCachePolicy }) {
if (!response.result.extensions) {
response.result.extensions = {};
}
response.result.extensions.__policyIfCacheable__ =
overallCachePolicy.policyIfCacheable();
},
};
},
},
],
});
await server.start();
const response = await server.executeOperation({ query: source });
await server.stop();
expect(response.result.errors).toBeUndefined();
return {
hints: cacheHints,
policyIfCacheable: response.result.extensions!.__policyIfCacheable__ as any,
};
}
export async function collectCacheControlHints(
...args: Parameters<typeof collectCacheControlHintsAndPolicyIfCacheable>
): Promise<Map<string, CacheHint>> {
return (await collectCacheControlHintsAndPolicyIfCacheable(...args)).hints;
}