From 24c66add69886dc284d1019b106fa9f270dad336 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Mon, 11 Jul 2022 12:14:18 -0700 Subject: [PATCH] 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. --- .../src/ApolloServerPluginResponseCache.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts b/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts index 2f5f123cf45..93e36e584b4 100644 --- a/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts +++ b/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts @@ -95,14 +95,12 @@ interface Options> { // 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, @@ -113,18 +111,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. // @@ -161,7 +169,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 { @@ -175,7 +183,7 @@ export default function plugin( } async function cacheGet( - contextualCacheKeyFields: ContextualCacheKey, + contextualCacheKeyFields: ContextualCacheKeyData, ): Promise { const cacheKeyData = { ...baseCacheKey!, @@ -291,7 +299,7 @@ export default function plugin( } const cacheSetInBackground = ( - contextualCacheKeyFields: ContextualCacheKey, + contextualCacheKeyFields: ContextualCacheKeyData, ): void => { const cacheKeyData = { ...baseCacheKey!,