diff --git a/apps/api/src/proxy/proxy.controller.ts b/apps/api/src/proxy/proxy.controller.ts index 4e04bec..e9c4361 100644 --- a/apps/api/src/proxy/proxy.controller.ts +++ b/apps/api/src/proxy/proxy.controller.ts @@ -4,9 +4,12 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { All, Controller, + Delete, + Get, HttpException, HttpStatus, Inject, + Query, Req, Res, } from '@nestjs/common'; @@ -23,6 +26,7 @@ export class ProxyController { constructor( @Inject(CACHE_MANAGER) private readonly cacheManager: RedisStore, ) {} + @All('/graphql') public async graphql(@Req() req: FastifyRequest, @Res() reply: FastifyReply) { const { operationName, query, variables } = req.body as GQLRequest; @@ -55,4 +59,49 @@ export class ProxyController { return reply.send(data); } + + @Get('/queries') + public async getQueriesList( + @Req() req: FastifyRequest, + @Res() reply: FastifyReply, + ) { + const list = await this.cacheManager.store.keys('*'); + const res = (Object.keys(queryTTL) as Array).reduce( + (acc, queryName) => { + const queries = list.filter((x) => x.split(' ').at(0) === queryName); + const ttl = queryTTL[queryName]; + acc[queryName] = { queries, ttl }; + + return acc; + }, + {} as Record, + ); + + return reply.send(res); + } + + @Delete('delete-query') + public async deleteQuery( + @Query('queryName') queryName: string, + @Res() reply: FastifyReply, + ) { + try { + await this.cacheManager.del(queryName); + + return reply.send('ok'); + } catch (error) { + throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @Delete('flush-all') + public async flushAll(@Res() reply: FastifyReply) { + try { + await this.cacheManager.store.reset(); + + return reply.send('ok'); + } catch (error) { + throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR); + } + } }