Skip to content

The open alternative to Salesforce, designed for AI.

Only fetch visible junction relations on record index queries (#26198)

Charles Bochetd5afb05087a3
diff --git a/packages/twenty-front/src/modules/object-record/hooks/__tests__/useRecordIndexTableQuery.test.tsx b/packages/twenty-front/src/modules/object-record/hooks/__tests__/useRecordIndexTableQuery.test.tsx
index b43704b03e8..9660d042718 100644
--- a/packages/twenty-front/src/modules/object-record/hooks/__tests__/useRecordIndexTableQuery.test.tsx
+++ b/packages/twenty-front/src/modules/object-record/hooks/__tests__/useRecordIndexTableQuery.test.tsx
@@ -57,19 +57,6 @@ const mocks: MockedResponse[] = [
extension
url
}
- caredForPets {
- edges {
- node {
- __typename
- id
- pet {
- __typename
- id
- name
- }
- }
- }
- }
createdAt
deletedAt
id
@@ -77,54 +64,7 @@ const mocks: MockedResponse[] = [
firstName
lastName
}
- noteTargets {
- edges {
- node {
- __typename
- id
- note {
- __typename
- id
- title
- }
- }
- }
- }
position
- previousCompanies {
- edges {
- node {
- __typename
- company {
- __typename
- domainName {
- primaryLinkUrl
- primaryLinkLabel
- secondaryLinks {
- label
- url
- }
- }
- id
- name
- }
- id
- }
- }
- }
- taskTargets {
- edges {
- node {
- __typename
- id
- task {
- __typename
- id
- title
- }
- }
- }
- }
updatedAt
}
cursor
diff --git a/packages/twenty-front/src/modules/object-record/record-board/components/RecordBoardQueryEffect.tsx b/packages/twenty-front/src/modules/object-record/record-board/components/RecordBoardQueryEffect.tsx
index eda346b7230..b12deca0289 100644
--- a/packages/twenty-front/src/modules/object-record/record-board/components/RecordBoardQueryEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-board/components/RecordBoardQueryEffect.tsx
@@ -1,3 +1,4 @@
+import { useRecordBoardQueryIdentifier } from '@/object-record/record-board/hooks/useRecordBoardQueryIdentifier';
import { useTriggerRecordBoardFetchMore } from '@/object-record/record-board/hooks/useTriggerRecordBoardFetchMore';
import { useTriggerRecordBoardInitialQuery } from '@/object-record/record-board/hooks/useTriggerRecordBoardInitialQuery';
import { lastRecordBoardQueryIdentifierComponentState } from '@/object-record/record-board/states/lastRecordBoardQueryIdentifierComponentState';
@@ -8,11 +9,8 @@ import { recordBoardShouldFetchMoreComponentState } from '@/object-record/record
import { recordBoardHasColumnsToFetchMoreComponentSelector } from '@/object-record/record-board/states/selectors/recordBoardHasColumnsToFetchMoreComponentSelector';
import { isDraggingRecordComponentState } from '@/object-record/record-drag/states/isDraggingRecordComponentState';
import { recordGroupIdsComponentState } from '@/object-record/record-group/states/recordGroupIdsComponentState';
-import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
-import { useRecordIndexGroupCommonQueryVariables } from '@/object-record/record-index/hooks/useRecordIndexGroupCommonQueryVariables';
import { recordIndexRecordGroupsAreInInitialLoadingComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupsAreInInitialLoadingComponentState';
-import { getQueryIdentifier } from '@/object-record/utils/getQueryIdentifier';
import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
import { useAtomComponentSelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorValue';
import { useAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentState';
@@ -23,8 +21,6 @@ import { useEffect } from 'react';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
export const RecordBoardQueryEffect = () => {
- const { objectMetadataItem } = useRecordIndexContextOrThrow();
-
const isDraggingRecord = useAtomComponentStateValue(
isDraggingRecordComponentState,
);
@@ -44,14 +40,7 @@ export const RecordBoardQueryEffect = () => {
recordBoardCurrentGroupByQueryOffsetComponentState,
);
- const { combinedFilters, orderBy } =
- useRecordIndexGroupCommonQueryVariables();
-
- const queryIdentifier = getQueryIdentifier({
- objectNameSingular: objectMetadataItem.nameSingular,
- filter: combinedFilters,
- orderBy,
- });
+ const queryIdentifier = useRecordBoardQueryIdentifier();
const queryIdentifierHasChanged =
queryIdentifier !== lastRecordBoardQueryIdentifier;
diff --git a/packages/twenty-front/src/modules/object-record/record-board/hooks/useRecordBoardQueryIdentifier.ts b/packages/twenty-front/src/modules/object-record/record-board/hooks/useRecordBoardQueryIdentifier.ts
new file mode 100644
index 00000000000..2d136b43727
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-board/hooks/useRecordBoardQueryIdentifier.ts
@@ -0,0 +1,21 @@
+import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
+import { useRecordIndexGroupCommonQueryVariables } from '@/object-record/record-index/hooks/useRecordIndexGroupCommonQueryVariables';
+import { getQueryIdentifier } from '@/object-record/utils/getQueryIdentifier';
+
+export const useRecordBoardQueryIdentifier = () => {
+ const { objectMetadataItem } = useRecordIndexContextOrThrow();
+
+ const { combinedFilters, orderBy, recordGqlFields } =
+ useRecordIndexGroupCommonQueryVariables();
+
+ // Newly shown fields are only fetched when this identifier changes
+ const fetchedFieldNames = Object.keys(recordGqlFields).sort().join(',');
+
+ const baseQueryIdentifier = getQueryIdentifier({
+ objectNameSingular: objectMetadataItem.nameSingular,
+ filter: combinedFilters,
+ orderBy,
+ });
+
+ return `${baseQueryIdentifier}|fields:${fetchedFieldNames}`;
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-board/hooks/useTriggerRecordBoardInitialQuery.ts b/packages/twenty-front/src/modules/object-record/record-board/hooks/useTriggerRecordBoardInitialQuery.ts
index 6aa3bf742ef..ca178b60c56 100644
--- a/packages/twenty-front/src/modules/object-record/record-board/hooks/useTriggerRecordBoardInitialQuery.ts
+++ b/packages/twenty-front/src/modules/object-record/record-board/hooks/useTriggerRecordBoardInitialQuery.ts
@@ -1,17 +1,16 @@
import { getRecordsFromRecordConnection } from '@/object-record/cache/utils/getRecordsFromRecordConnection';
import { RECORD_BOARD_QUERY_PAGE_SIZE } from '@/object-record/record-board/constants/RecordBoardQueryPageSize';
+import { useRecordBoardQueryIdentifier } from '@/object-record/record-board/hooks/useRecordBoardQueryIdentifier';
import { useSetRecordIdsForColumn } from '@/object-record/record-board/hooks/useSetRecordIdsForColumn';
import { lastRecordBoardQueryIdentifierComponentState } from '@/object-record/record-board/states/lastRecordBoardQueryIdentifierComponentState';
import { recordBoardCurrentGroupByQueryOffsetComponentState } from '@/object-record/record-board/states/recordBoardCurrentGroupByQueryOffsetComponentState';
import { recordBoardShouldFetchMoreInColumnComponentFamilyState } from '@/object-record/record-board/states/recordBoardShouldFetchMoreInColumnComponentFamilyState';
import { recordGroupDefinitionsComponentSelector } from '@/object-record/record-group/states/selectors/recordGroupDefinitionsComponentSelector';
import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
-import { useRecordIndexGroupCommonQueryVariables } from '@/object-record/record-index/hooks/useRecordIndexGroupCommonQueryVariables';
import { useRecordIndexGroupsRecordsLazyGroupBy } from '@/object-record/record-index/hooks/useRecordIndexGroupsRecordsLazyGroupBy';
import { recordIndexGroupFieldMetadataItemComponentState } from '@/object-record/record-index/states/recordIndexGroupFieldMetadataComponentState';
import { recordIndexRecordGroupsAreInInitialLoadingComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupsAreInInitialLoadingComponentState';
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
-import { getQueryIdentifier } from '@/object-record/utils/getQueryIdentifier';
import { getGroupByQueryResultGqlFieldName } from '@/page-layout/utils/getGroupByQueryResultGqlFieldName';
import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
@@ -55,14 +54,7 @@ export const useTriggerRecordBoardInitialQuery = () => {
recordBoardCurrentGroupByQueryOffsetComponentState,
);
- const { combinedFilters, orderBy } =
- useRecordIndexGroupCommonQueryVariables();
-
- const queryIdentifier = getQueryIdentifier({
- objectNameSingular: objectMetadataItem.nameSingular,
- filter: combinedFilters,
- orderBy,
- });
+ const queryIdentifier = useRecordBoardQueryIdentifier();
const { setRecordIdsForColumn } = useSetRecordIdsForColumn();
const { upsertRecordsInStore } = useUpsertRecordsInStore();
diff --git a/packages/twenty-front/src/modules/object-record/record-field/hooks/useRelevantRecordsGqlFields.ts b/packages/twenty-front/src/modules/object-record/record-field/hooks/useRelevantRecordsGqlFields.ts
index 6e46b19fd9a..e250e9feceb 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/hooks/useRelevantRecordsGqlFields.ts
+++ b/packages/twenty-front/src/modules/object-record/record-field/hooks/useRelevantRecordsGqlFields.ts
@@ -4,8 +4,6 @@ import { getImageIdentifierFieldMetadataItem } from '@/object-metadata/utils/get
import { getLabelIdentifierFieldMetadataItem } from '@/object-metadata/utils/getLabelIdentifierFieldMetadataItem';
import { hasObjectMetadataItemPositionField } from '@/object-metadata/utils/hasObjectMetadataItemPositionField';
import { generateDepthRecordGqlFieldsFromFields } from '@/object-record/graphql/record-gql-fields/utils/generateDepthRecordGqlFieldsFromFields';
-import { getFieldRelations } from '@/object-record/record-field/ui/utils/junction/getFieldRelations';
-import { getJunctionObjectMetadataIds } from '@/object-record/record-field/ui/utils/junction/getJunctionObjectMetadataIds';
import { visibleRecordFieldsComponentSelector } from '@/object-record/record-field/states/visibleRecordFieldsComponentSelector';
import { currentRecordFiltersComponentState } from '@/object-record/record-filter/states/currentRecordFiltersComponentState';
import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
@@ -72,22 +70,6 @@ export const useRelevantRecordsGqlFields = ({
depth: 1,
});
- // Junction records are the only way to reach what they link to, so they are always
- // fetched, whether or not the field holding them is visible.
- const junctionObjectMetadataIds =
- getJunctionObjectMetadataIds(objectMetadataItems);
-
- const junctionRelationGqlFields = generateDepthRecordGqlFieldsFromFields({
- objectMetadataItems,
- sourceObjectMetadataItem: objectMetadataItem,
- fields: objectMetadataItem.fields.filter((fieldMetadataItem) =>
- getFieldRelations(fieldMetadataItem).some((relation) =>
- junctionObjectMetadataIds.has(relation.targetObjectMetadata.id),
- ),
- ),
- depth: 1,
- });
-
const labelIdentifierFieldMetadataItem =
getLabelIdentifierFieldMetadataItem(objectMetadataItem);
const imageIdentifierFieldMetadataItem =
@@ -105,7 +87,6 @@ export const useRelevantRecordsGqlFields = ({
: {}),
...(hasPosition ? { position: true } : {}),
...allDepthOneGqlFields,
- ...junctionRelationGqlFields,
createdAt: true,
updatedAt: true,
deletedAt: true,