From 177c6283d666a80fbe9a37e2420cf0885447dcef Mon Sep 17 00:00:00 2001 From: Jorge Chacon Date: Thu, 12 May 2022 14:28:02 -0600 Subject: [PATCH 1/2] Add Dynamic Prefix To Cache Key Adding hook to pass a function with acess to requestContext to return a dynamic prefix for each key --- .../src/ApolloServerPluginResponseCache.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts b/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts index 71da54b6fc4..f9bd4b9cd52 100644 --- a/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts +++ b/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts @@ -72,6 +72,12 @@ interface Options> { requestContext: GraphQLRequestContext, ): ValueOrPromise; + // Define this hook if you want the cache key to use a prefix before the hash + // Example: {operationName}:{keyHash} + dynamicPrefix?( + requestContext: GraphQLRequestContext, + ): ValueOrPromise; + // If this hook is defined and returns false, the plugin will not read // responses from the cache. shouldReadFromCache?( @@ -133,6 +139,8 @@ function isGraphQLQuery(requestContext: GraphQLRequestContext) { return requestContext.operation?.operation === 'query'; } +let dynamicPrefix: string = ''; + export default function plugin( options: Options = Object.create(null), ): ApolloServerPlugin { @@ -166,7 +174,7 @@ export default function plugin( ...baseCacheKey!, ...contextualCacheKeyFields, }); - const serializedValue = await cache.get(key); + const serializedValue = await cache.get((dynamicPrefix + key)); if (serializedValue === undefined) { return null; } @@ -189,6 +197,11 @@ export default function plugin( extraCacheKeyData = await options.extraCacheKeyData(requestContext); } + if (options.dynamicPrefix) { + dynamicPrefix = await options.dynamicPrefix(requestContext); + } + + baseCacheKey = { source: requestContext.source!, operationName: requestContext.operationName, @@ -293,7 +306,7 @@ export default function plugin( // still calls `cache.set` synchronously (ie, that it writes to // InMemoryLRUCache synchronously). cache - .set(key, serializedValue, { ttl: policyIfCacheable.maxAge }) + .set((dynamicPrefix + key), serializedValue, { ttl: policyIfCacheable.maxAge }) .catch(logger.warn); }; From c29bec46d2f95a1085534c05757ef1bda002addb Mon Sep 17 00:00:00 2001 From: Jorge Chacon Date: Wed, 25 May 2022 11:31:44 -0600 Subject: [PATCH 2/2] Fixing prettier issues --- .../src/ApolloServerPluginResponseCache.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts b/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts index f9bd4b9cd52..cce84c14f59 100644 --- a/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts +++ b/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts @@ -72,9 +72,9 @@ interface Options> { requestContext: GraphQLRequestContext, ): ValueOrPromise; - // Define this hook if you want the cache key to use a prefix before the hash - // Example: {operationName}:{keyHash} - dynamicPrefix?( + // Define this hook if you want the cache key to use a prefix before the hash + // Example: {operationName}:{keyHash} + dynamicPrefix?( requestContext: GraphQLRequestContext, ): ValueOrPromise; @@ -174,7 +174,7 @@ export default function plugin( ...baseCacheKey!, ...contextualCacheKeyFields, }); - const serializedValue = await cache.get((dynamicPrefix + key)); + const serializedValue = await cache.get(dynamicPrefix + key); if (serializedValue === undefined) { return null; } @@ -201,7 +201,6 @@ export default function plugin( dynamicPrefix = await options.dynamicPrefix(requestContext); } - baseCacheKey = { source: requestContext.source!, operationName: requestContext.operationName, @@ -306,7 +305,9 @@ export default function plugin( // still calls `cache.set` synchronously (ie, that it writes to // InMemoryLRUCache synchronously). cache - .set((dynamicPrefix + key), serializedValue, { ttl: policyIfCacheable.maxAge }) + .set(dynamicPrefix + key, serializedValue, { + ttl: policyIfCacheable.maxAge, + }) .catch(logger.warn); };