From 11a7db567211f81900247eb07f6e150439ac0399 Mon Sep 17 00:00:00 2001 From: Charles Bochet Date: Thu, 25 Apr 2024 14:45:14 +0200 Subject: [PATCH] Fix workspace schema caching when user is not logged in (#5173) In this PR: - Follow up on #5170 as we did not take into account not logged in users - only apply throttler on root fields to avoid performance overhead --- ...raphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch | 16 ++++++++-------- .../graphql-config/hooks/use-throttler.ts | 6 +++++- yarn.lock | 4 ++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/twenty-server/patches/@graphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch b/packages/twenty-server/patches/@graphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch index b545bd0742..59f4e9ff07 100644 --- a/packages/twenty-server/patches/@graphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch +++ b/packages/twenty-server/patches/@graphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch @@ -31,8 +31,8 @@ index 16843949d8589a299d8195b0a349ac4dac0bacbf..21e7fe2bbcba36b04a274be9d2219fd3 const yoga = (0, graphql_yoga_1.createYoga)({ ...options, + schema: async (request) => { -+ const workspaceId = request.req.workspace.id -+ const workspaceCacheVersion = request.req.cacheVersion ++ const workspaceId = request.req.workspace?.id ?? 'anonymous' ++ const workspaceCacheVersion = request.req.cacheVersion ?? '0' + const url = request.req.baseUrl + + const cacheKey = `${workspaceId}-${workspaceCacheVersion}-${url}` @@ -77,8 +77,8 @@ index 16843949d8589a299d8195b0a349ac4dac0bacbf..21e7fe2bbcba36b04a274be9d2219fd3 const yoga = (0, graphql_yoga_1.createYoga)({ ...options, + schema: async (request) => { -+ const workspaceId = request.req.workspace.id -+ const workspaceCacheVersion = request.req.cacheVersion ++ const workspaceId = request.req.workspace?.id ?? 'anonymous' ++ const workspaceCacheVersion = request.req.cacheVersion ?? '0' + const url = request.req.baseUrl + + const cacheKey = `${workspaceId}-${workspaceCacheVersion}-${url}` @@ -143,8 +143,8 @@ index 7068c519320b379917c46763cd280b1cdd3e48f0..418e1030373fc1e0fb85a932ac8da9b3 const yoga = createYoga({ ...options, + schema: async (request) => { -+ const workspaceId = request.req.workspace.id -+ const workspaceCacheVersion = request.req.cacheVersion ++ const workspaceId = request.req.workspace?.id ?? 'anonymous' ++ const workspaceCacheVersion = request.req.cacheVersion ?? '0' + const url = request.req.baseUrl + + const cacheKey = `${workspaceId}-${workspaceCacheVersion}-${url}` @@ -189,8 +189,8 @@ index 7068c519320b379917c46763cd280b1cdd3e48f0..418e1030373fc1e0fb85a932ac8da9b3 const yoga = createYoga({ ...options, + schema: async (request) => { -+ const workspaceId = request.req.workspace.id -+ const workspaceCacheVersion = request.req.cacheVersion ++ const workspaceId = request.req.workspace?.id ?? 'anonymous' ++ const workspaceCacheVersion = request.req.cacheVersion ?? '0' + const url = request.req.baseUrl + + const cacheKey = `${workspaceId}-${workspaceCacheVersion}-${url}` diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-throttler.ts b/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-throttler.ts index 6959ef8f2b..9eff5fc9ef 100644 --- a/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-throttler.ts +++ b/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-throttler.ts @@ -40,6 +40,10 @@ export const useThrottler = ( addPlugin( useOnResolve(async ({ args, root, context, info }) => { if (options.limit && options.ttl) { + if (root !== undefined) { + return; + } + const id = options.identifyFn(context); const errorMessage = await context.rateLimiterFn( @@ -47,7 +51,7 @@ export const useThrottler = ( { max: options?.limit, window: `${options?.ttl}s`, - message: interpolate('Too much request.', { + message: interpolate('Too many requests.', { id, }), }, diff --git a/yarn.lock b/yarn.lock index 42f094a799..8141f12a55 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6830,14 +6830,14 @@ __metadata: "@graphql-yoga/nestjs@patch:@graphql-yoga/nestjs@2.1.0#./patches/@graphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch::locator=twenty-server%40workspace%3Apackages%2Ftwenty-server": version: 2.1.0 - resolution: "@graphql-yoga/nestjs@patch:@graphql-yoga/nestjs@npm%3A2.1.0#./patches/@graphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch::version=2.1.0&hash=6db821&locator=twenty-server%40workspace%3Apackages%2Ftwenty-server" + resolution: "@graphql-yoga/nestjs@patch:@graphql-yoga/nestjs@npm%3A2.1.0#./patches/@graphql-yoga-nestjs-npm-2.1.0-cb509e6047.patch::version=2.1.0&hash=6403da&locator=twenty-server%40workspace%3Apackages%2Ftwenty-server" peerDependencies: "@nestjs/common": ^10.0.0 "@nestjs/core": ^10.0.0 "@nestjs/graphql": ^12.0.0 graphql: ^15.0.0 || ^16.0.0 graphql-yoga: ^4.0.4 - checksum: 512ed39d8a0b9e238b31b0a9fc0a4ee2c8980f7f38f218635beede535bcec9fbf11731ff2f986c0d551a003094c6935748cee721d3a46339b3a6e0467a699e1d + checksum: 3f7b8839cb9de4c05b1bae0c3eaa242f70e24c8258d193dedacdd6f06c1d29330ffcd233c6bbb7ff2288f1a55d97a4a333a0538d2ec5da40911d422e1c28e6d6 languageName: node linkType: hard