Throttling in common api (#15338)

This commit is contained in:
Etienne
2025-10-24 17:32:51 +02:00
committed by GitHub
parent d7bda9576f
commit 93ab1c4118
11 changed files with 295 additions and 14 deletions
@@ -17,7 +17,6 @@ import { JsonWebTokenError, TokenExpiredError } from 'jsonwebtoken';
import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';
import { useThrottler } from 'src/engine/api/graphql/graphql-config/hooks/use-throttler';
import { WorkspaceSchemaFactory } from 'src/engine/api/graphql/workspace-schema.factory';
import { type AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { CoreEngineModule } from 'src/engine/core-modules/core-engine.module';
@@ -55,13 +54,6 @@ export class GraphQLConfigService
const isDebugMode =
this.twentyConfigService.get('NODE_ENV') === NodeEnvironment.DEVELOPMENT;
const plugins = [
useThrottler({
ttl: this.twentyConfigService.get('API_RATE_LIMITING_TTL'),
limit: this.twentyConfigService.get('API_RATE_LIMITING_LIMIT'),
identifyFn: (context) => {
return context.req.user?.id ?? context.req.ip ?? 'anonymous';
},
}),
useGraphQLErrorHandlerHook({
metricsService: this.metricsService,
exceptionHandlerService: this.exceptionHandlerService,
@@ -32,8 +32,8 @@ export const metadataModuleFactory = async (
resolvers: { JSON: GraphQLJSON },
plugins: [
useThrottler({
ttl: twentyConfigService.get('API_RATE_LIMITING_TTL'),
limit: twentyConfigService.get('API_RATE_LIMITING_LIMIT'),
ttl: twentyConfigService.get('API_RATE_LIMITING_LONG_TTL_IN_MS') / 1000,
limit: twentyConfigService.get('API_RATE_LIMITING_LONG_LIMIT'),
identifyFn: (context) => {
return context.req.user?.id ?? context.req.ip ?? 'anonymous';
},
@@ -12,6 +12,8 @@ import { AuthException } from 'src/engine/core-modules/auth/auth.exception';
import { authGraphqlApiExceptionHandler } from 'src/engine/core-modules/auth/utils/auth-graphql-api-exception-handler.util';
import { RecordTransformerException } from 'src/engine/core-modules/record-transformer/record-transformer.exception';
import { recordTransformerGraphqlApiExceptionHandler } from 'src/engine/core-modules/record-transformer/utils/record-transformer-graphql-api-exception-handler.util';
import { ThrottlerException } from 'src/engine/core-modules/throttler/throttler.exception';
import { throttlerToGraphqlApiExceptionHandler } from 'src/engine/core-modules/throttler/utils/throttler-to-graphql-api-exception-handler.util';
import { PermissionsException } from 'src/engine/metadata-modules/permissions/permissions.exception';
import { permissionGraphqlApiExceptionHandler } from 'src/engine/metadata-modules/permissions/utils/permission-graphql-api-exception-handler.util';
import { TwentyORMException } from 'src/engine/twenty-orm/exceptions/twenty-orm.exception';
@@ -42,6 +44,8 @@ export const workspaceQueryRunnerGraphqlApiExceptionHandler = (
return authGraphqlApiExceptionHandler(error);
case error instanceof ApiKeyException:
return apiKeyGraphqlApiExceptionHandler(error);
case error instanceof ThrottlerException:
return throttlerToGraphqlApiExceptionHandler(error);
default:
throw error;
}