Skip to content

Commit

Permalink
✨ Allowing extraCacheKeyData to provide a cache key prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
kschrade authored Jul 7, 2022
1 parent b208205 commit 28d2462
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ interface Options<TContext = Record<string, any>> {
// be anything that can be JSON-stringified.
extraCacheKeyData?(
requestContext: GraphQLRequestContext<TContext>,
): ValueOrPromise<any>;
): ValueOrPromise<any & { cacheKeyPrefix?: string | null }>;

// If this hook is defined and returns false, the plugin will not read
// responses from the cache.
Expand Down Expand Up @@ -102,7 +102,9 @@ interface BaseCacheKey {
source: string;
operationName: string | null;
variables: { [name: string]: any };
extra: any;
// Note: There is a special key, `cacheKeyPrefix`, that will be prepended to
// the cache key.
extra: any & { cacheKeyPrefix?: string | null };
}

interface ContextualCacheKey {
Expand All @@ -128,8 +130,11 @@ interface CacheValue {

type CacheKey = BaseCacheKey & ContextualCacheKey;

function cacheKeyString(key: CacheKey) {
return sha(JSON.stringify(key));
export function getCacheKeyString(key: CacheKey, hashKey = sha) {
const shaValue = hashKey(JSON.stringify(key));
return key.extra?.cacheKeyPrefix
? key.extra?.cacheKeyPrefix + shaValue
: shaValue;
}

function isGraphQLQuery(requestContext: GraphQLRequestContext<any>) {
Expand Down Expand Up @@ -165,7 +170,7 @@ export default function plugin(
async function cacheGet(
contextualCacheKeyFields: ContextualCacheKey,
): Promise<GraphQLResponse | null> {
const key = cacheKeyString({
const key = getCacheKeyString({
...baseCacheKey!,
...contextualCacheKeyFields,
});
Expand Down Expand Up @@ -278,7 +283,7 @@ export default function plugin(
const cacheSetInBackground = (
contextualCacheKeyFields: ContextualCacheKey,
): void => {
const key = cacheKeyString({
const key = getCacheKeyString({
...baseCacheKey!,
...contextualCacheKeyFields,
});
Expand Down

0 comments on commit 28d2462

Please sign in to comment.