Skip to content

Commit

Permalink
Cache supegraph in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
agologan committed Oct 31, 2023
1 parent 96eb639 commit 5738ead
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
43 changes: 43 additions & 0 deletions src/helpers/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
import { logger } from '../logger';

export function asyncWrap(fn) {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
}

export function cache(key) {
return (req, res, next) => {
const locals = req.app.locals;
if (!locals.cache) {
locals.cache = {};
}

const cached = locals.cache[key];
if (cached) {
logger.info(`Sending cached: ${key}`);
res.set(cached.headers);
return res.send(cached.body);
} else {
res.__send = res.send;
res.send = (body) => {
locals.cache[key] = {
body,
headers: res.getHeaders(),
};
res.__send(body);
logger.info(`Cached: ${key}`);
};
}
next();
};
}

export function invalidate(key) {
return (req, res, next) => {
const locals = req.app.locals;
if (!locals.cache) {
return next();
}

logger.info(`Invalidating: ${key}`);
delete locals.cache[key];

return next();
};
}
6 changes: 5 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ export const logger = createLogger({

function buildPrettyFormat() {
return format.combine(
format.errors({ stack: true }),
format.colorize(),
format.timestamp(),
format.printf(({ timestamp, level, message, stack }) => {
return `[${timestamp}] ${level}: ${message} ${stack}`;
if (stack) {
return `[${timestamp}] ${level}: ${message} ${stack}`;
}
return `[${timestamp}] ${level}: ${message}`;
})
);
}
Expand Down
18 changes: 14 additions & 4 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express';
// eslint-disable-next-line new-cap
const router = express.Router();
import { json } from 'body-parser';
import { asyncWrap } from '../helpers/middleware';
import { asyncWrap, cache, invalidate } from '../helpers/middleware';

import parseMiddleware from '../middleware/parse-request';
import { indexHtml, assetRouter } from './assets';
Expand Down Expand Up @@ -46,18 +46,28 @@ router.get(
router.get('/', indexHtml());
assetRouter(router);

const supergraphKey = 'supergraph';

router.get('/persisted_query', asyncWrap(persistedQuery.get));
router.post('/persisted_query', asyncWrap(persistedQuery.create));

router.get('/schema/latest', asyncWrap(schema.composeLatest));
router.get('/schema/supergraph', asyncWrap(schema.supergraph));
router.get(
'/schema/supergraph',
cache(supergraphKey),
asyncWrap(schema.supergraph)
);
router.post('/schema/compose', asyncWrap(schema.compose));
router.post('/schema/push', asyncWrap(schema.push));
router.post('/schema/push', invalidate(supergraphKey), asyncWrap(schema.push));
router.post('/schema/diff', asyncWrap(schema.diff));

router.delete('/schema/:schemaId', asyncWrap(schema.remove));
router.post('/schema/validate', asyncWrap(schema.validate));

router.delete('/service/:name', asyncWrap(service.remove));
router.delete(
'/service/:name',
invalidate(supergraphKey),
asyncWrap(service.remove)
);

export default router;

0 comments on commit 5738ead

Please sign in to comment.