From d8e16cbfd12e894cafea33f965259289b110b0d9 Mon Sep 17 00:00:00 2001 From: Weiko Date: Mon, 23 Sep 2024 15:12:49 +0200 Subject: [PATCH] Fix IN filter with empty array (#7202) ## Context The api currently allows empty array in the IN filter but the expected behaviour is not very clear. Typeorm seems to return all records when it is empty which could lead to undesired result. Instead we decided to throw an error. I've updated the FE accordingly to skip calls when array is empty. Screenshot 2024-09-23 at 14 20 28 --- .../SettingsAccountsMessageChannelsContainer.tsx | 1 + .../graphql-query-filter-field.parser.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/twenty-front/src/modules/settings/accounts/components/SettingsAccountsMessageChannelsContainer.tsx b/packages/twenty-front/src/modules/settings/accounts/components/SettingsAccountsMessageChannelsContainer.tsx index 3bbe93deb..d2d15d02f 100644 --- a/packages/twenty-front/src/modules/settings/accounts/components/SettingsAccountsMessageChannelsContainer.tsx +++ b/packages/twenty-front/src/modules/settings/accounts/components/SettingsAccountsMessageChannelsContainer.tsx @@ -44,6 +44,7 @@ export const SettingsAccountsMessageChannelsContainer = () => { in: accounts.map((account) => account.id), }, }, + skip: !accounts.length, }); const tabs = [ diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query-filter/graphql-query-filter-field.parser.ts b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query-filter/graphql-query-filter-field.parser.ts index e3f4a8348..fe37c4d44 100644 --- a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query-filter/graphql-query-filter-field.parser.ts +++ b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query-filter/graphql-query-filter-field.parser.ts @@ -1,4 +1,3 @@ -import { isArray } from 'class-validator'; import { ObjectLiteral, WhereExpressionBuilder } from 'typeorm'; import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface'; @@ -49,8 +48,13 @@ export class GraphqlQueryFilterFieldParser { } const [[operator, value]] = Object.entries(filterValue); - if (operator === 'in' && (!isArray(value) || value.length === 0)) { - return; + if (operator === 'in') { + if (!Array.isArray(value) || value.length === 0) { + throw new GraphqlQueryRunnerException( + `Invalid filter value for field ${key}. Expected non-empty array`, + GraphqlQueryRunnerExceptionCode.INVALID_QUERY_INPUT, + ); + } } const { sql, params } = this.computeWhereConditionParts(