Skip to content

Commit

Permalink
Follow-up to #6655 (#6664)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
glasser authored Jul 11, 2022
1 parent a4411bd commit 24c66ad
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,12 @@ interface Options<TContext = Record<string, any>> {
// 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<Record<string, any>>,
keyData: unknown,
): string;
}

type GenerateCacheKeyFunction = (
requestContext: GraphQLRequestContext<Record<string, any>>,
keyData: unknown,
) => string;

enum SessionMode {
NoSession,
Private,
Expand All @@ -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<Record<string, any>>,
keyData: CacheKeyData,
) => string;

interface CacheValue {
// Note: we only store data responses in the cache, not errors.
//
Expand Down Expand Up @@ -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 {
Expand All @@ -175,7 +183,7 @@ export default function plugin(
}

async function cacheGet(
contextualCacheKeyFields: ContextualCacheKey,
contextualCacheKeyFields: ContextualCacheKeyData,
): Promise<GraphQLResponse | null> {
const cacheKeyData = {
...baseCacheKey!,
Expand Down Expand Up @@ -291,7 +299,7 @@ export default function plugin(
}

const cacheSetInBackground = (
contextualCacheKeyFields: ContextualCacheKey,
contextualCacheKeyFields: ContextualCacheKeyData,
): void => {
const cacheKeyData = {
...baseCacheKey!,
Expand Down

0 comments on commit 24c66ad

Please sign in to comment.