apps/api: do not cache graphql responses with errors

This commit is contained in:
vchikalkin 2024-03-18 17:19:54 +03:00
parent f695835c61
commit 2fea400060
2 changed files with 10 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { queryTTL } from './lib/config';
import type { GQLRequest } from './types';
import type { GQLRequest, GQLResponse } from './types';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import {
All,
@ -42,13 +42,14 @@ export class ProxyController {
method: req.method,
});
if (!response.ok)
const data = (await response.json()) as GQLResponse;
if (!response.ok || data?.error || data?.errors?.length)
throw new HttpException(
response.statusText,
response.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
const data = await response.json();
const ttl = queryTTL[operationName];
if (data && ttl !== false)
await this.cacheManager.set(key, data, { ttl: ttl || env.CACHE_TTL });

View File

@ -3,3 +3,9 @@ export type GQLRequest = {
query: string;
variables: string;
};
export type GQLResponse = {
data: unknown;
error?: unknown;
errors?: unknown[];
};