[REFACTOR][BUG] Dynamically compute field to write in cache CREATE (#10130)

# Introduction
While importing records encountering missing expected fields when
writting a fragment from apollo cache

## Updates

### 1/ `createdBy` Default value
When inserting in cache in create single or many we will now make
optimistic behavior on the createdBy value

### 2/ `createRecordInCache` dynamically create `recordGrqlFields`
When creating an entry in cache, we will now dynamically generate fields
to be written in the fragment instead of expecting all of them. As by
nature record could be partial

### 3/ Strictly typed `RecordGqlFields`

# Conclusion
closes #9927
This commit is contained in:
Paul Rastoin
2025-02-13 17:43:54 +01:00
committed by GitHub
parent 58a62ec6f0
commit 5963c0f384
35 changed files with 495 additions and 107 deletions

View File

@ -1 +1,3 @@
export type RecordGqlFields = Record<string, any>;
export type RecordGqlFields = {
[k: string]: boolean | RecordGqlFields | undefined;
};

View File

@ -0,0 +1 @@
export type RecordGqlFieldsDeprecated = Record<string, any>;

View File

@ -1,3 +1,3 @@
import { RecordGqlFields } from '@/object-record/graphql/types/RecordGqlFields';
import { RecordGqlFieldsDeprecated } from '@/object-record/graphql/types/RecordGqlFieldsDeprecated';
export type RecordGqlOperationGqlRecordFields = RecordGqlFields;
export type RecordGqlOperationGqlRecordFields = RecordGqlFieldsDeprecated;

View File

@ -0,0 +1,88 @@
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields';
import {
getPersonObjectMetadataItem,
getPersonRecord,
} from '~/testing/mock-data/people';
describe('generateDepthOneRecordGqlFields', () => {
const objectMetadataItem = getPersonObjectMetadataItem();
it('Should handle basic call with both objectMetadataItem and record', () => {
const personRecord = getPersonRecord();
const result = generateDepthOneRecordGqlFields({
objectMetadataItem,
record: personRecord,
});
expect(result).toMatchInlineSnapshot(`
{
"attachments": false,
"avatarUrl": false,
"calendarEventParticipants": false,
"city": true,
"company": true,
"companyId": false,
"createdAt": true,
"createdBy": true,
"deletedAt": true,
"emails": false,
"favorites": false,
"id": true,
"intro": false,
"jobTitle": true,
"linkedinLink": true,
"messageParticipants": false,
"name": true,
"noteTargets": true,
"performanceRating": false,
"phones": true,
"pointOfContactForOpportunities": false,
"position": true,
"searchVector": false,
"taskTargets": true,
"timelineActivities": false,
"updatedAt": false,
"whatsapp": false,
"workPreference": false,
"xLink": true,
}
`);
});
it('Should handle basic call with standalone objectMetadataItem', () => {
const result = generateDepthOneRecordGqlFields({
objectMetadataItem,
});
expect(result).toMatchInlineSnapshot(`
{
"attachments": true,
"avatarUrl": true,
"calendarEventParticipants": true,
"city": true,
"company": true,
"companyId": true,
"createdAt": true,
"createdBy": true,
"deletedAt": true,
"emails": true,
"favorites": true,
"id": true,
"intro": true,
"jobTitle": true,
"linkedinLink": true,
"messageParticipants": true,
"name": true,
"noteTargets": true,
"performanceRating": true,
"phones": true,
"pointOfContactForOpportunities": true,
"position": true,
"searchVector": true,
"taskTargets": true,
"timelineActivities": true,
"updatedAt": true,
"whatsapp": true,
"workPreference": true,
"xLink": true,
}
`);
});
});

View File

@ -0,0 +1,10 @@
import { RecordGqlFields } from '@/object-record/graphql/types/RecordGqlFields';
import { isDefined } from 'twenty-shared';
export const isRecordGqlFieldsNode = (
recordGql: RecordGqlFields | boolean | undefined,
): recordGql is RecordGqlFields =>
isDefined(recordGql) &&
typeof recordGql === 'object' &&
recordGql !== null &&
!Array.isArray(recordGql);