diff --git a/packages/plugin-response-cache/src/ApolloServerPluginResponseCache.ts b/packages/plugin-response-cache/src/ApolloServerPluginResponseCache.ts index 515ebb48244..ce1a8fe11b6 100644 --- a/packages/plugin-response-cache/src/ApolloServerPluginResponseCache.ts +++ b/packages/plugin-response-cache/src/ApolloServerPluginResponseCache.ts @@ -106,14 +106,12 @@ export interface ApolloServerPluginResponseCacheOptions< // and that all relevant data will be found by the kind of iteration performed by // `JSON.stringify`, but you should not assume anything about the particular fields on // `keyData`. - generateCacheKey?: GenerateCacheKeyFunction; + generateCacheKey?( + requestContext: GraphQLRequestContext>, + keyData: unknown, + ): string; } -type GenerateCacheKeyFunction = ( - requestContext: GraphQLRequestContext>, - keyData: unknown, -) => string; - enum SessionMode { NoSession, Private, @@ -124,18 +122,28 @@ function sha(s: string) { return createHash('sha256').update(s).digest('hex'); } -interface BaseCacheKey { +interface BaseCacheKeyData { source: string; operationName: string | null; variables: { [name: string]: any }; extra: any; } -interface ContextualCacheKey { +interface ContextualCacheKeyData { sessionMode: SessionMode; sessionId?: string | null; } +// We split the CacheKey type into two pieces just for convenience in the code +// below. Note that we don't actually export this type publicly (the +// generateCacheKey hook gets an `unknown` argument). +type CacheKeyData = BaseCacheKeyData & ContextualCacheKeyData; + +type GenerateCacheKeyFunction = ( + requestContext: GraphQLRequestContext>, + keyData: CacheKeyData, +) => string; + interface CacheValue { // Note: we only store data responses in the cache, not errors. // @@ -174,7 +182,7 @@ export default function plugin( options.generateCacheKey ?? ((_, key) => sha(JSON.stringify(key))); let sessionId: string | null = null; - let baseCacheKey: BaseCacheKey | null = null; + let baseCacheKey: BaseCacheKeyData | null = null; let age: number | null = null; return { @@ -188,7 +196,7 @@ export default function plugin( } async function cacheGet( - contextualCacheKeyFields: ContextualCacheKey, + contextualCacheKeyFields: ContextualCacheKeyData, ): Promise { const cacheKeyData = { ...baseCacheKey!, @@ -309,7 +317,7 @@ export default function plugin( } const cacheSetInBackground = ( - contextualCacheKeyFields: ContextualCacheKey, + contextualCacheKeyFields: ContextualCacheKeyData, ): void => { const cacheKeyData = { ...baseCacheKey!,