const cacheToken = contextToken<Map<string, unknown>>("cache");
const cacheMiddleware: MiddlewareFn = async ({ args, next, context }) => {
const cacheKey = JSON.stringify(args);
const cache = context.getOr(cacheToken, () => new Map());
if (cache.has(cacheKey)) return cache.get(cacheKey);
const result = await next(args);
cache.set(cacheKey, result);
context.put(cacheToken, cache);
return result;
};
Middleware implementation as a function.
A function-based middleware that receives
MiddlewareArgsand returns the result of processing. This is the most common and flexible way to define middleware.