UNPKG

@graphql-mesh/plugin-response-cache

Version:
117 lines (116 loc) 4.83 kB
import { defaultBuildResponseCacheKey } from '@envelop/response-cache'; import { process } from '@graphql-mesh/cross-helpers'; import { stringInterpolator } from '@graphql-mesh/string-interpolation'; import { useResponseCache } from '@graphql-yoga/plugin-response-cache'; function generateSessionIdFactory(sessionIdDef) { if (sessionIdDef == null) { return function voidSession() { return null; }; } return function session(context) { return stringInterpolator.parse(sessionIdDef, { context, env: process.env, }); }; } function generateEnabledFactory(ifDef) { return function enabled(context) { // eslint-disable-next-line no-new-func return new Function('context', `return ${ifDef}`)(context); }; } function getBuildResponseCacheKey(cacheKeyDef) { return function buildResponseCacheKey(cacheKeyParameters) { let cacheKey = stringInterpolator.parse(cacheKeyDef, { ...cacheKeyParameters, env: process.env, }); if (!cacheKey) { cacheKey = defaultBuildResponseCacheKey(cacheKeyParameters); } return cacheKey; }; } function getShouldCacheResult(shouldCacheResultDef) { return function shouldCacheResult({ result }) { // eslint-disable-next-line no-new-func return new Function(`return ${shouldCacheResultDef}`)(); }; } function getCacheForResponseCache(meshCache) { return { get(responseId) { return meshCache.get(`response-cache:${responseId}`); }, set(responseId, data, entities, ttl) { const ttlConfig = Number.isFinite(ttl) ? { ttl: ttl / 1000 } : undefined; const jobs = []; jobs.push(meshCache.set(`response-cache:${responseId}`, data, ttlConfig)); for (const { typename, id } of entities) { const entryId = `${typename}.${id}`; jobs.push(meshCache.set(`response-cache:${entryId}:${responseId}`, {}, ttlConfig)); jobs.push(meshCache.set(`response-cache:${responseId}:${entryId}`, {}, ttlConfig)); } return Promise.all(jobs); }, invalidate(entitiesToRemove) { const responseIdsToCheck = new Set(); const entitiesToRemoveJobs = []; for (const { typename, id } of entitiesToRemove) { const entryId = `${typename}.${id}`; entitiesToRemoveJobs.push(meshCache.getKeysByPrefix(`response-cache:${entryId}:`).then(cacheEntriesToDelete => Promise.all(cacheEntriesToDelete.map(cacheEntryName => { const [, , responseId] = cacheEntryName.split(':'); responseIdsToCheck.add(responseId); return meshCache.delete(entryId); })))); } return Promise.all(entitiesToRemoveJobs).then(() => { const responseIdsToCheckJobs = []; for (const responseId of responseIdsToCheck) { responseIdsToCheckJobs.push(meshCache.getKeysByPrefix(`response-cache:${responseId}:`).then(cacheEntries => { if (cacheEntries.length === 0) { return meshCache.delete(`response-cache:${responseId}`); } return undefined; })); } }); }, }; } export default function useMeshResponseCache(options) { const ttlPerType = {}; const ttlPerSchemaCoordinate = {}; if (options.ttlPerCoordinate) { for (const ttlConfig of options.ttlPerCoordinate) { if (ttlConfig.coordinate.includes('.')) { ttlPerSchemaCoordinate[ttlConfig.coordinate] = ttlConfig.ttl; } else { ttlPerType[ttlConfig.coordinate] = ttlConfig.ttl; } } } return useResponseCache({ ttl: options.ttl, ignoredTypes: options.ignoredTypes, idFields: options.idFields, invalidateViaMutation: options.invalidateViaMutation, includeExtensionMetadata: options.includeExtensionMetadata != null ? options.includeExtensionMetadata : process.env.DEBUG === '1', ttlPerType, ttlPerSchemaCoordinate, session: generateSessionIdFactory(options.sessionId), enabled: options.if ? generateEnabledFactory(options.if) : undefined, buildResponseCacheKey: options.cacheKey ? getBuildResponseCacheKey(options.cacheKey) : undefined, shouldCacheResult: options.shouldCacheResult ? getShouldCacheResult(options.shouldCacheResult) : undefined, cache: getCacheForResponseCache(options.cache), }); }