Feat/put target object identifier on use activities (#4682)
When writing to the normalized cache (record), it's crucial to use _refs for relationships to avoid many problems. Essentially, we only deal with level 0 and generate all fields to be comfortable with their defaults. When writing in queries (which should be very rare, the only cases are prefetch and the case of activities due to the nested query; I've reduced this to a single file for activities usePrepareFindManyActivitiesQuery 🙂), it's important to use queryFields to avoid bugs. I've implemented them on the side of query generation and record generation. When doing an updateOne / createOne, etc., it's necessary to distinguish between optimistic writing (which we actually want to do with _refs) and the server response without refs. This allows for a clean write in the optimistic cache without worrying about nesting (as the first point). To simplify the whole activities part, write to the normalized cache first. Then, base queries on it in an idempotent manner. This way, there's no need to worry about the current page or action. The normalized cache is up-to-date, so I update the queries. Same idea as for optimisticEffects, actually. Finally, I've triggered optimisticEffects rather than the manual update of many queries. --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
@ -2,7 +2,6 @@ import { isNonEmptyString } from '@sniptt/guards';
|
||||
|
||||
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
|
||||
import { FieldMetadataType } from '~/generated/graphql';
|
||||
import { capitalize } from '~/utils/string/capitalize';
|
||||
|
||||
export const generateEmptyFieldValue = (
|
||||
fieldMetadataItem: FieldMetadataItem,
|
||||
@ -53,8 +52,6 @@ export const generateEmptyFieldValue = (
|
||||
return true;
|
||||
}
|
||||
case FieldMetadataType.Relation: {
|
||||
// TODO: refactor with relationDefiniton once the PR is merged : https://github.com/twentyhq/twenty/pull/4378
|
||||
// so we can directly check the relation type from this field point of view.
|
||||
if (
|
||||
!isNonEmptyString(
|
||||
fieldMetadataItem.fromRelationMetadata?.toObjectMetadata
|
||||
@ -64,12 +61,7 @@ export const generateEmptyFieldValue = (
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
__typename: `${capitalize(
|
||||
fieldMetadataItem.fromRelationMetadata.toObjectMetadata.nameSingular,
|
||||
)}Connection`,
|
||||
edges: [],
|
||||
};
|
||||
return [];
|
||||
}
|
||||
case FieldMetadataType.Currency: {
|
||||
return {
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
export const mapPaginatedRecordsToRecords = <
|
||||
RecordType extends { id: string } & Record<string, any>,
|
||||
RecordTypeQuery extends {
|
||||
[objectNamePlural: string]: {
|
||||
edges: RecordEdge[];
|
||||
};
|
||||
},
|
||||
RecordEdge extends {
|
||||
node: RecordType;
|
||||
},
|
||||
>({
|
||||
pagedRecords,
|
||||
objectNamePlural,
|
||||
}: {
|
||||
pagedRecords: RecordTypeQuery | undefined;
|
||||
objectNamePlural: string;
|
||||
}) => {
|
||||
const formattedRecords: RecordType[] =
|
||||
pagedRecords?.[objectNamePlural]?.edges?.map((recordEdge: RecordEdge) => ({
|
||||
...recordEdge.node,
|
||||
})) ?? [];
|
||||
|
||||
return formattedRecords;
|
||||
};
|
||||
@ -0,0 +1,35 @@
|
||||
import { isUndefined } from '@sniptt/guards';
|
||||
|
||||
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
|
||||
import { generateEmptyFieldValue } from '@/object-record/utils/generateEmptyFieldValue';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
export const prefillRecord = <T extends ObjectRecord>({
|
||||
objectMetadataItem,
|
||||
input,
|
||||
depth = 1,
|
||||
}: {
|
||||
objectMetadataItem: ObjectMetadataItem;
|
||||
input: Record<string, unknown>;
|
||||
depth?: number;
|
||||
}) => {
|
||||
return Object.fromEntries(
|
||||
objectMetadataItem.fields
|
||||
.filter(
|
||||
(fieldMetadataItem) =>
|
||||
depth > 0 || fieldMetadataItem.type !== 'RELATION',
|
||||
)
|
||||
.map((fieldMetadataItem) => {
|
||||
const inputValue = input[fieldMetadataItem.name];
|
||||
|
||||
return [
|
||||
fieldMetadataItem.name,
|
||||
isUndefined(inputValue)
|
||||
? generateEmptyFieldValue(fieldMetadataItem)
|
||||
: inputValue,
|
||||
];
|
||||
})
|
||||
.filter(isDefined),
|
||||
) as T;
|
||||
};
|
||||
@ -3,6 +3,7 @@ import { isString } from '@sniptt/guards';
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { isFieldRelationValue } from '@/object-record/record-field/types/guards/isFieldRelationValue';
|
||||
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
|
||||
import { sanitizeLink } from '@/object-record/utils/sanitizeLinkRecordInput';
|
||||
import { FieldMetadataType } from '~/generated/graphql';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
@ -12,7 +13,7 @@ export const sanitizeRecordInput = ({
|
||||
recordInput,
|
||||
}: {
|
||||
objectMetadataItem: ObjectMetadataItem;
|
||||
recordInput: Record<string, unknown>;
|
||||
recordInput: Partial<ObjectRecord>;
|
||||
}) => {
|
||||
const filteredResultRecord = Object.fromEntries(
|
||||
Object.entries(recordInput)
|
||||
@ -23,6 +24,10 @@ export const sanitizeRecordInput = ({
|
||||
|
||||
if (!fieldMetadataItem) return undefined;
|
||||
|
||||
if (!fieldMetadataItem.isNullable && fieldValue == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (
|
||||
fieldMetadataItem.type === FieldMetadataType.Relation &&
|
||||
isFieldRelationValue(fieldValue)
|
||||
|
||||
Reference in New Issue
Block a user