@graphql-mesh/plugin-response-cache
Version:
120 lines (119 loc) • 5.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const response_cache_1 = require("@envelop/response-cache");
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
const plugin_response_cache_1 = require("@graphql-yoga/plugin-response-cache");
function generateSessionIdFactory(sessionIdDef) {
if (sessionIdDef == null) {
return function voidSession() {
return null;
};
}
return function session(context) {
return string_interpolation_1.stringInterpolator.parse(sessionIdDef, {
context,
env: cross_helpers_1.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 = string_interpolation_1.stringInterpolator.parse(cacheKeyDef, {
...cacheKeyParameters,
env: cross_helpers_1.process.env,
});
if (!cacheKey) {
cacheKey = (0, response_cache_1.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;
}));
}
});
},
};
}
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 (0, plugin_response_cache_1.useResponseCache)({
ttl: options.ttl,
ignoredTypes: options.ignoredTypes,
idFields: options.idFields,
invalidateViaMutation: options.invalidateViaMutation,
includeExtensionMetadata: options.includeExtensionMetadata != null
? options.includeExtensionMetadata
: cross_helpers_1.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),
});
}
exports.default = useMeshResponseCache;