UNPKG

@graphql-mesh/plugin-response-cache

Version:
108 lines (104 loc) 4.51 kB
'use strict'; const responseCache = require('@envelop/response-cache'); const stringInterpolation = require('@graphql-mesh/string-interpolation'); const crossHelpers = require('@graphql-mesh/cross-helpers'); const utils = require('@graphql-mesh/utils'); function getDocumentString(args) { return utils.printWithCache(args.document); } function generateSessionIdFactory(sessionIdDef) { return function session(context) { return stringInterpolation.stringInterpolator.parse(sessionIdDef, { context, env: crossHelpers.process.env, }); }; } function generateEnabledFactory(ifDef) { return function enabled(context) { // eslint-disable-next-line no-new-func return new Function(`return ${ifDef}`)(); }; } function getBuildResponseCacheKey(cacheKeyDef) { return function buildResponseCacheKey(cacheKeyParameters) { let cacheKey = stringInterpolation.stringInterpolator.parse(cacheKeyDef, { ...cacheKeyParameters, env: crossHelpers.process.env, }); if (!cacheKey) { cacheKey = responseCache.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}`); }, async set(responseId, data, entities, ttl) { const ttlConfig = Number.isFinite(ttl) ? { ttl: ttl / 1000 } : undefined; await Promise.all([...entities].map(async ({ typename, id }) => { const entryId = `${typename}.${id}`; await meshCache.set(`response-cache:${entryId}:${responseId}`, {}, ttlConfig); await meshCache.set(`response-cache:${responseId}:${entryId}`, {}, ttlConfig); })); return meshCache.set(`response-cache:${responseId}`, data, ttlConfig); }, async invalidate(entitiesToRemove) { const responseIdsToCheck = new Set(); await Promise.all([...entitiesToRemove].map(async ({ typename, id }) => { const entryId = `${typename}.${id}`; const cacheEntriesToDelete = await meshCache.getKeysByPrefix(`response-cache:${entryId}:`); await Promise.all(cacheEntriesToDelete.map(cacheEntryName => { const [, , responseId] = cacheEntryName.split(':'); responseIdsToCheck.add(responseId); return meshCache.delete(entryId); })); })); await Promise.all([...responseIdsToCheck].map(async (responseId) => { const cacheEntries = await meshCache.getKeysByPrefix(`response-cache:${responseId}:`); if (cacheEntries.length === 0) { await meshCache.delete(`response-cache:${responseId}`); } })); }, }; } 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 responseCache.useResponseCache({ ttl: options.ttl, ignoredTypes: options.ignoredTypes, idFields: options.idFields, invalidateViaMutation: options.invalidateViaMutation, includeExtensionMetadata: options.includeExtensionMetadata, ttlPerType, ttlPerSchemaCoordinate, getDocumentString, session: options.sessionId ? generateSessionIdFactory(options.sessionId) : undefined, 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), }); } module.exports = useMeshResponseCache;