Skip to content

Commit 1cbbfe4

Browse files
glassertrevor-scheer
authored andcommitted
Follow-up to #6655 (#6664)
- Rename BaseCacheKey and ContextualCacheKey with "Data" on end, since the cache key is a string. - Reintroduce CacheKey(Data) removed in #6655; make the GenerateCacheKeyFunction type be defined in terms of it. Now calls to the `const generateCacheKey` function are type-safe (ie, the second arg is a CacheKeyData rather than unknown). - Describe generateCacheKey hook as a method rather than a field, like the other hooks in Options.
1 parent ba88f7c commit 1cbbfe4

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

packages/plugin-response-cache/src/ApolloServerPluginResponseCache.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,12 @@ export interface ApolloServerPluginResponseCacheOptions<
106106
// and that all relevant data will be found by the kind of iteration performed by
107107
// `JSON.stringify`, but you should not assume anything about the particular fields on
108108
// `keyData`.
109-
generateCacheKey?: GenerateCacheKeyFunction;
109+
generateCacheKey?(
110+
requestContext: GraphQLRequestContext<Record<string, any>>,
111+
keyData: unknown,
112+
): string;
110113
}
111114

112-
type GenerateCacheKeyFunction = (
113-
requestContext: GraphQLRequestContext<Record<string, any>>,
114-
keyData: unknown,
115-
) => string;
116-
117115
enum SessionMode {
118116
NoSession,
119117
Private,
@@ -124,18 +122,28 @@ function sha(s: string) {
124122
return createHash('sha256').update(s).digest('hex');
125123
}
126124

127-
interface BaseCacheKey {
125+
interface BaseCacheKeyData {
128126
source: string;
129127
operationName: string | null;
130128
variables: { [name: string]: any };
131129
extra: any;
132130
}
133131

134-
interface ContextualCacheKey {
132+
interface ContextualCacheKeyData {
135133
sessionMode: SessionMode;
136134
sessionId?: string | null;
137135
}
138136

137+
// We split the CacheKey type into two pieces just for convenience in the code
138+
// below. Note that we don't actually export this type publicly (the
139+
// generateCacheKey hook gets an `unknown` argument).
140+
type CacheKeyData = BaseCacheKeyData & ContextualCacheKeyData;
141+
142+
type GenerateCacheKeyFunction = (
143+
requestContext: GraphQLRequestContext<Record<string, any>>,
144+
keyData: CacheKeyData,
145+
) => string;
146+
139147
interface CacheValue {
140148
// Note: we only store data responses in the cache, not errors.
141149
//
@@ -174,7 +182,7 @@ export default function plugin<TContext extends BaseContext>(
174182
options.generateCacheKey ?? ((_, key) => sha(JSON.stringify(key)));
175183

176184
let sessionId: string | null = null;
177-
let baseCacheKey: BaseCacheKey | null = null;
185+
let baseCacheKey: BaseCacheKeyData | null = null;
178186
let age: number | null = null;
179187

180188
return {
@@ -188,7 +196,7 @@ export default function plugin<TContext extends BaseContext>(
188196
}
189197

190198
async function cacheGet(
191-
contextualCacheKeyFields: ContextualCacheKey,
199+
contextualCacheKeyFields: ContextualCacheKeyData,
192200
): Promise<GraphQLResponse | null> {
193201
const cacheKeyData = {
194202
...baseCacheKey!,
@@ -309,7 +317,7 @@ export default function plugin<TContext extends BaseContext>(
309317
}
310318

311319
const cacheSetInBackground = (
312-
contextualCacheKeyFields: ContextualCacheKey,
320+
contextualCacheKeyFields: ContextualCacheKeyData,
313321
): void => {
314322
const cacheKeyData = {
315323
...baseCacheKey!,

0 commit comments

Comments
 (0)