export const contextCacheSymbol = Symbol.for('GiraphQL.contextCache'); export function initContextCache() { return { [contextCacheSymbol]: {}, }; } export type ContextCache = ( context: C, ...args: Args ) => T; export function createContextCache( create: (context: C, ...args: Args) => T, ): ContextCache { const cache = new WeakMap(); return (context, ...args) => { const cacheKey = (context as { [contextCacheSymbol]: object })[contextCacheSymbol] || context; if (cache.has(cacheKey)) { return cache.get(cacheKey)!; } const entry = create(context, ...args); cache.set(cacheKey, entry); return entry; }; }