The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(api): make neq match NULL rows when compared against a real value (#27399)
Atharvaa97c26965008
diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.ts b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.tsindex e0089e5691f..0a76a16e6f8 100644--- a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.ts+++ b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.ts@@ -2,6 +2,11 @@ import { FieldMetadataType } from 'twenty-shared/types';import { computeWhereConditionParts } from 'src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts';+jest.mock('crypto', () => ({+ ...jest.requireActual('crypto'),+ randomBytes: () => Buffer.from('0000000000', 'hex'),+}));+describe('computeWhereConditionParts', () => {describe('isEmptyArray', () => {it.each([FieldMetadataType.ARRAY, FieldMetadataType.MULTI_SELECT])(@@ -40,4 +45,85 @@ describe('computeWhereConditionParts', () => {},);});++ describe('neq', () => {+ it.each([+ {+ description: 'should match NULL values when compared to a real value',+ key: 'jobTitle',+ value: 'Engineer',+ fieldMetadataType: FieldMetadataType.TEXT,+ expectedSql: `("person"."jobTitle" != :jobTitle0000000000 OR "person"."jobTitle" IS NULL)`,+ expectedParams: { jobTitle0000000000: 'Engineer' },+ },+ {+ description:+ 'should exclude NULL values when compared to an empty value',+ key: 'jobTitle',+ value: '',+ fieldMetadataType: FieldMetadataType.TEXT,+ expectedSql: `("person"."jobTitle" != :jobTitle0000000000 AND "person"."jobTitle" IS NOT NULL)`,+ expectedParams: { jobTitle0000000000: '' },+ },+ {+ description:+ 'should match NULL foreign keys when compared to a real id',+ key: 'companyId',+ value: '20202020-0000-4000-8000-000000000001',+ fieldMetadataType: FieldMetadataType.UUID,+ expectedSql: `("person"."companyId" != :companyId0000000000 OR "person"."companyId" IS NULL)`,+ expectedParams: {+ companyId0000000000: '20202020-0000-4000-8000-000000000001',+ },+ },+ {+ description: 'should match NULL dates when compared to a real date',+ key: 'createdAt',+ value: '2026-01-01T00:00:00.000Z',+ fieldMetadataType: FieldMetadataType.DATE_TIME,+ expectedSql: `("person"."createdAt" < :createdAt0000000000 OR "person"."createdAt" >= :createdAt0000000000::timestamptz + interval '1 millisecond' OR "person"."createdAt" IS NULL)`,+ expectedParams: { createdAt0000000000: '2026-01-01T00:00:00.000Z' },+ },+ {+ description: 'should exclude NULL text values when compared to null',+ key: 'jobTitle',+ value: null,+ fieldMetadataType: FieldMetadataType.TEXT,+ expectedSql: `("person"."jobTitle" != :jobTitle0000000000 AND "person"."jobTitle" IS NOT NULL)`,+ expectedParams: { jobTitle0000000000: '' },+ },+ {+ description: 'should match set foreign keys when compared to null',+ key: 'companyId',+ value: null,+ fieldMetadataType: FieldMetadataType.UUID,+ expectedSql: `"person"."companyId" IS NOT NULL`,+ expectedParams: {},+ },+ {+ description: 'should match set dates when compared to null',+ key: 'createdAt',+ value: null,+ fieldMetadataType: FieldMetadataType.DATE_TIME,+ expectedSql: `"person"."createdAt" IS NOT NULL`,+ expectedParams: {},+ },+ ])(+ '$description',+ ({ key, value, fieldMetadataType, expectedSql, expectedParams }) => {+ expect(+ computeWhereConditionParts({+ operator: 'neq',+ objectNameSingular: 'person',+ key,+ value,+ fieldMetadataType,+ }),+ ).toEqual({+ sql: expectedSql,+ params: expectedParams,+ });+ },+ );+ });});diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.ts b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.tsindex cdf1d8fb6ce..c5bb9209f82 100644--- a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.ts+++ b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.ts@@ -80,15 +80,29 @@ export const computeWhereConditionParts = ({params: { [`${key}${paramSuffix}`]: value },};case 'neq':+ if (hasNullEquivalentFieldValue) {+ return {+ sql: `(${fieldReference} != :${key}${paramSuffix} AND ${fieldReference} IS NOT NULL)`,+ params: { [`${key}${paramSuffix}`]: nullEquivalentFieldValue },+ };+ }++ if (value === null) {+ return {+ sql: `${fieldReference} IS NOT NULL`,+ params: {},+ };+ }+if (isDateTimeField) {return {- sql: `(${fieldReference} < :${key}${paramSuffix} OR ${fieldReference} >= :${key}${paramSuffix}::timestamptz + interval '1 millisecond')${hasNullEquivalentFieldValue ? ` AND ${fieldReference} IS NOT NULL` : ''}`,+ sql: `(${fieldReference} < :${key}${paramSuffix} OR ${fieldReference} >= :${key}${paramSuffix}::timestamptz + interval '1 millisecond' OR ${fieldReference} IS NULL)`,params: { [`${key}${paramSuffix}`]: value },};}return {- sql: `${fieldReference} != :${key}${paramSuffix}${hasNullEquivalentFieldValue ? ` AND ${fieldReference} IS NOT NULL` : ''}`,+ sql: `(${fieldReference} != :${key}${paramSuffix} OR ${fieldReference} IS NULL)`,params: { [`${key}${paramSuffix}`]: value },};case 'gt':diff --git a/packages/twenty-server/test/integration/graphql/suites/filter-by-relation-field.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/filter-by-relation-field.integration-spec.tsindex 8bde82647cd..839668be4d6 100644--- a/packages/twenty-server/test/integration/graphql/suites/filter-by-relation-field.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/filter-by-relation-field.integration-spec.ts@@ -246,6 +246,68 @@ describe('Filter by relation field (e2e)', () => {expect(ids).toEqual([TEST_PERSON_IDS.AIRBNB_DESIGNER]);});+ it.each([+ {+ description:+ 'a foreign key compared to a company, keeping people without one',+ filter: { companyId: { neq: TEST_COMPANY_IDS.AIRBNB } },+ expectedIds: [+ TEST_PERSON_IDS.STRIPE_ENGINEER,+ TEST_PERSON_IDS.NOTION_ENGINEER,+ TEST_PERSON_IDS.UNAFFILIATED,+ ],+ },+ {+ description:+ 'a company field compared to a value, keeping people without a company',+ filter: { company: { name: { neq: 'Airbnb' } } },+ expectedIds: [+ TEST_PERSON_IDS.STRIPE_ENGINEER,+ TEST_PERSON_IDS.NOTION_ENGINEER,+ TEST_PERSON_IDS.UNAFFILIATED,+ ],+ },+ {+ description:+ 'a foreign key compared to null, keeping only people with a company',+ filter: { companyId: { neq: null } },+ expectedIds: [+ TEST_PERSON_IDS.AIRBNB_ENGINEER,+ TEST_PERSON_IDS.AIRBNB_DESIGNER,+ TEST_PERSON_IDS.STRIPE_ENGINEER,+ TEST_PERSON_IDS.NOTION_ENGINEER,+ ],+ },+ ])(+ 'should filter people with neq on $description',+ async ({ filter, expectedIds }) => {+ const response = await makeGraphqlApiRequest({+ query: gql`+ query People($filter: PersonFilterInput) {+ people(filter: $filter, first: 10) {+ edges {+ node {+ id+ }+ }+ }+ }+ `,+ variables: {+ filter: { and: [{ id: { in: ALL_TEST_PERSON_IDS } }, filter] },+ },+ });++ expect(response.body.errors).toBeUndefined();++ const ids = response.body.data.people.edges.map(+ (edge: { node: { id: string } }) => edge.node.id,+ );++ expect(ids.sort()).toEqual([...expectedIds].sort());+ },+ );+it('should combine a relation filter with an order-by on the same relation (join dedupe)', async () => {const queryData = {query: gql`