Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Adding generateCacheKey to ApolloServerPluginResponseCache to allow for custom cache keys #6655

Merged
merged 11 commits into from Jul 11, 2022
Expand Up @@ -86,6 +86,13 @@ interface Options<TContext = Record<string, any>> {
shouldWriteToCache?(
requestContext: GraphQLRequestContext<TContext>,
): ValueOrPromise<boolean>;

// This hook allows one to replace the function that is used to create a cache
// key. If not it will fall back to what happens today.
kschrade marked this conversation as resolved.
Show resolved Hide resolved
generateCacheKey?(
requestContext: GraphQLRequestContext<TContext>,
keyData: unknown,
): string;
}

enum SessionMode {
Expand Down Expand Up @@ -165,10 +172,15 @@ export default function plugin(
async function cacheGet(
contextualCacheKeyFields: ContextualCacheKey,
): Promise<GraphQLResponse | null> {
const key = cacheKeyString({
const cacheKeyData = {
...baseCacheKey!,
...contextualCacheKeyFields,
});
};

const key = options.generateCacheKey
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
? options.generateCacheKey(requestContext, cacheKeyData)
: cacheKeyString(cacheKeyData);

const serializedValue = await cache.get(key);
if (serializedValue === undefined) {
return null;
Expand Down Expand Up @@ -278,10 +290,15 @@ export default function plugin(
const cacheSetInBackground = (
contextualCacheKeyFields: ContextualCacheKey,
): void => {
const key = cacheKeyString({
const cacheKeyData = {
...baseCacheKey!,
...contextualCacheKeyFields,
});
};

const key = options.generateCacheKey
? options.generateCacheKey(requestContext, cacheKeyData)
: cacheKeyString(cacheKeyData);

const value: CacheValue = {
data,
cachePolicy: policyIfCacheable,
Expand Down