Refactor triggerUpdateRelationsOptimisticEffect to compute relationship from Metadatas (#9815)
# Introduction At the moment the relationships are inferred from the record data structure instead of its metadatas We should refactor the code that computes or not the necessity to detach a relation on a mutation We've refactored the `isObjectRecordConnection` method to be consuming a `relationDefintion` instead of "typeChecking" at the runtime the data structure using zod validation schema Related to #9580
This commit is contained in:
@ -1,5 +1,3 @@
|
|||||||
import { ApolloCache } from '@apollo/client';
|
|
||||||
|
|
||||||
import { triggerAttachRelationOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerAttachRelationOptimisticEffect';
|
import { triggerAttachRelationOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerAttachRelationOptimisticEffect';
|
||||||
import { triggerDestroyRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDestroyRecordsOptimisticEffect';
|
import { triggerDestroyRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDestroyRecordsOptimisticEffect';
|
||||||
import { triggerDetachRelationOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDetachRelationOptimisticEffect';
|
import { triggerDetachRelationOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDetachRelationOptimisticEffect';
|
||||||
@ -10,23 +8,26 @@ import { isObjectRecordConnection } from '@/object-record/cache/utils/isObjectRe
|
|||||||
import { RecordGqlConnection } from '@/object-record/graphql/types/RecordGqlConnection';
|
import { RecordGqlConnection } from '@/object-record/graphql/types/RecordGqlConnection';
|
||||||
import { RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';
|
import { RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';
|
||||||
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
|
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
|
||||||
|
import { ApolloCache } from '@apollo/client';
|
||||||
|
import { isArray } from '@sniptt/guards';
|
||||||
import { FieldMetadataType } from '~/generated-metadata/graphql';
|
import { FieldMetadataType } from '~/generated-metadata/graphql';
|
||||||
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
|
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
|
||||||
import { isDefined } from '~/utils/isDefined';
|
import { isDefined } from '~/utils/isDefined';
|
||||||
|
|
||||||
|
type triggerUpdateRelationsOptimisticEffectArgs = {
|
||||||
|
cache: ApolloCache<unknown>;
|
||||||
|
sourceObjectMetadataItem: ObjectMetadataItem;
|
||||||
|
currentSourceRecord: ObjectRecord | null;
|
||||||
|
updatedSourceRecord: ObjectRecord | null;
|
||||||
|
objectMetadataItems: ObjectMetadataItem[];
|
||||||
|
};
|
||||||
export const triggerUpdateRelationsOptimisticEffect = ({
|
export const triggerUpdateRelationsOptimisticEffect = ({
|
||||||
cache,
|
cache,
|
||||||
sourceObjectMetadataItem,
|
sourceObjectMetadataItem,
|
||||||
currentSourceRecord,
|
currentSourceRecord,
|
||||||
updatedSourceRecord,
|
updatedSourceRecord,
|
||||||
objectMetadataItems,
|
objectMetadataItems,
|
||||||
}: {
|
}: triggerUpdateRelationsOptimisticEffectArgs) => {
|
||||||
cache: ApolloCache<unknown>;
|
|
||||||
sourceObjectMetadataItem: ObjectMetadataItem;
|
|
||||||
currentSourceRecord: ObjectRecord | null;
|
|
||||||
updatedSourceRecord: ObjectRecord | null;
|
|
||||||
objectMetadataItems: ObjectMetadataItem[];
|
|
||||||
}) => {
|
|
||||||
return sourceObjectMetadataItem.fields.forEach(
|
return sourceObjectMetadataItem.fields.forEach(
|
||||||
(fieldMetadataItemOnSourceRecord) => {
|
(fieldMetadataItemOnSourceRecord) => {
|
||||||
const notARelationField =
|
const notARelationField =
|
||||||
@ -81,71 +82,55 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
|||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const extractTargetRecordsFromRelation = (
|
||||||
// TODO: replace this by a relation type check, if it's one to many,
|
value: RecordGqlConnection | RecordGqlNode | null,
|
||||||
// it's an object record connection (we can still check it though as a safeguard)
|
): RecordGqlNode[] => {
|
||||||
const currentFieldValueOnSourceRecordIsARecordConnection =
|
// TODO investigate on the root cause of array injection here, should never occurs
|
||||||
isObjectRecordConnection(
|
// Cache might be corrupted somewhere due to ObjectRecord and RecordGqlNode inclusion
|
||||||
targetObjectMetadata.nameSingular,
|
if (!isDefined(value) || isArray(value)) {
|
||||||
currentFieldValueOnSourceRecord,
|
return [];
|
||||||
);
|
|
||||||
|
|
||||||
const targetRecordsToDetachFrom =
|
|
||||||
currentFieldValueOnSourceRecordIsARecordConnection
|
|
||||||
? currentFieldValueOnSourceRecord.edges.map(
|
|
||||||
({ node }) => node as RecordGqlNode,
|
|
||||||
)
|
|
||||||
: [currentFieldValueOnSourceRecord].filter(isDefined);
|
|
||||||
|
|
||||||
const updatedFieldValueOnSourceRecordIsARecordConnection =
|
|
||||||
isObjectRecordConnection(
|
|
||||||
targetObjectMetadata.nameSingular,
|
|
||||||
updatedFieldValueOnSourceRecord,
|
|
||||||
);
|
|
||||||
|
|
||||||
const targetRecordsToAttachTo =
|
|
||||||
updatedFieldValueOnSourceRecordIsARecordConnection
|
|
||||||
? updatedFieldValueOnSourceRecord.edges.map(
|
|
||||||
({ node }) => node as RecordGqlNode,
|
|
||||||
)
|
|
||||||
: [updatedFieldValueOnSourceRecord].filter(isDefined);
|
|
||||||
|
|
||||||
const shouldDetachSourceFromAllTargets =
|
|
||||||
isDefined(currentSourceRecord) && targetRecordsToDetachFrom.length > 0;
|
|
||||||
|
|
||||||
if (shouldDetachSourceFromAllTargets) {
|
|
||||||
// TODO: see if we can de-hardcode this, put cascade delete in relation metadata item
|
|
||||||
// Instead of hardcoding it here
|
|
||||||
const shouldCascadeDeleteTargetRecords =
|
|
||||||
CORE_OBJECT_NAMES_TO_DELETE_ON_TRIGGER_RELATION_DETACH.includes(
|
|
||||||
targetObjectMetadata.nameSingular as CoreObjectNameSingular,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (shouldCascadeDeleteTargetRecords) {
|
|
||||||
triggerDestroyRecordsOptimisticEffect({
|
|
||||||
cache,
|
|
||||||
objectMetadataItem: fullTargetObjectMetadataItem,
|
|
||||||
recordsToDestroy: targetRecordsToDetachFrom,
|
|
||||||
objectMetadataItems,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
targetRecordsToDetachFrom.forEach((targetRecordToDetachFrom) => {
|
|
||||||
triggerDetachRelationOptimisticEffect({
|
|
||||||
cache,
|
|
||||||
sourceObjectNameSingular: sourceObjectMetadataItem.nameSingular,
|
|
||||||
sourceRecordId: currentSourceRecord.id,
|
|
||||||
fieldNameOnTargetRecord: targetFieldMetadata.name,
|
|
||||||
targetObjectNameSingular: targetObjectMetadata.nameSingular,
|
|
||||||
targetRecordId: targetRecordToDetachFrom.id,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isObjectRecordConnection(relationDefinition, value)) {
|
||||||
|
return value.edges.map(({ node }) => node);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [value];
|
||||||
|
};
|
||||||
|
const targetRecordsToDetachFrom = extractTargetRecordsFromRelation(
|
||||||
|
currentFieldValueOnSourceRecord,
|
||||||
|
);
|
||||||
|
const targetRecordsToAttachTo = extractTargetRecordsFromRelation(
|
||||||
|
updatedFieldValueOnSourceRecord,
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: see if we can de-hardcode this, put cascade delete in relation metadata item
|
||||||
|
// Instead of hardcoding it here
|
||||||
|
const shouldCascadeDeleteTargetRecords =
|
||||||
|
CORE_OBJECT_NAMES_TO_DELETE_ON_TRIGGER_RELATION_DETACH.includes(
|
||||||
|
targetObjectMetadata.nameSingular as CoreObjectNameSingular,
|
||||||
|
);
|
||||||
|
if (shouldCascadeDeleteTargetRecords) {
|
||||||
|
triggerDestroyRecordsOptimisticEffect({
|
||||||
|
cache,
|
||||||
|
objectMetadataItem: fullTargetObjectMetadataItem,
|
||||||
|
recordsToDestroy: targetRecordsToDetachFrom,
|
||||||
|
objectMetadataItems,
|
||||||
|
});
|
||||||
|
} else if (isDefined(currentSourceRecord)) {
|
||||||
|
targetRecordsToDetachFrom.forEach((targetRecordToDetachFrom) => {
|
||||||
|
triggerDetachRelationOptimisticEffect({
|
||||||
|
cache,
|
||||||
|
sourceObjectNameSingular: sourceObjectMetadataItem.nameSingular,
|
||||||
|
sourceRecordId: currentSourceRecord.id,
|
||||||
|
fieldNameOnTargetRecord: targetFieldMetadata.name,
|
||||||
|
targetObjectNameSingular: targetObjectMetadata.nameSingular,
|
||||||
|
targetRecordId: targetRecordToDetachFrom.id,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const shouldAttachSourceToAllTargets =
|
if (isDefined(updatedSourceRecord)) {
|
||||||
isDefined(updatedSourceRecord) && targetRecordsToAttachTo.length > 0;
|
|
||||||
|
|
||||||
if (shouldAttachSourceToAllTargets) {
|
|
||||||
targetRecordsToAttachTo.forEach((targetRecordToAttachTo) =>
|
targetRecordsToAttachTo.forEach((targetRecordToAttachTo) =>
|
||||||
triggerAttachRelationOptimisticEffect({
|
triggerAttachRelationOptimisticEffect({
|
||||||
cache,
|
cache,
|
||||||
|
|||||||
@ -1,27 +1,38 @@
|
|||||||
import { peopleQueryResult } from '~/testing/mock-data/people';
|
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
|
||||||
|
|
||||||
import { isObjectRecordConnection } from '@/object-record/cache/utils/isObjectRecordConnection';
|
import { isObjectRecordConnection } from '@/object-record/cache/utils/isObjectRecordConnection';
|
||||||
|
import { RelationDefinitionType } from '~/generated-metadata/graphql';
|
||||||
describe('isObjectRecordConnection', () => {
|
describe('isObjectRecordConnection', () => {
|
||||||
it('should work with query result', () => {
|
const relationDefinitionMap: { [K in RelationDefinitionType]: boolean } = {
|
||||||
const validQueryResult = peopleQueryResult.people;
|
[RelationDefinitionType.MANY_TO_MANY]: true,
|
||||||
|
[RelationDefinitionType.ONE_TO_MANY]: true,
|
||||||
|
[RelationDefinitionType.MANY_TO_ONE]: false,
|
||||||
|
[RelationDefinitionType.ONE_TO_ONE]: false,
|
||||||
|
};
|
||||||
|
|
||||||
const isValidQueryResult = isObjectRecordConnection(
|
it.each(Object.entries(relationDefinitionMap))(
|
||||||
'person',
|
'.$relation',
|
||||||
validQueryResult,
|
(relation, expected) => {
|
||||||
);
|
const emptyRecord = {};
|
||||||
|
const result = isObjectRecordConnection(
|
||||||
|
{
|
||||||
|
direction: relation,
|
||||||
|
} as NonNullable<FieldMetadataItem['relationDefinition']>,
|
||||||
|
emptyRecord,
|
||||||
|
);
|
||||||
|
|
||||||
expect(isValidQueryResult).toEqual(true);
|
expect(result).toEqual(expected);
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
it('should fail with invalid result', () => {
|
it('should throw on unknown relation direction', () => {
|
||||||
const invalidResult = { test: 123 };
|
const emptyRecord = {};
|
||||||
|
expect(() =>
|
||||||
const isValidQueryResult = isObjectRecordConnection(
|
isObjectRecordConnection(
|
||||||
'person',
|
{
|
||||||
invalidResult,
|
direction: 'UNKNOWN_TYPE',
|
||||||
);
|
} as any,
|
||||||
|
emptyRecord,
|
||||||
expect(isValidQueryResult).toEqual(false);
|
),
|
||||||
|
).toThrowError();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,30 +1,23 @@
|
|||||||
import { z } from 'zod';
|
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
|
||||||
|
|
||||||
import { RecordGqlConnection } from '@/object-record/graphql/types/RecordGqlConnection';
|
import { RecordGqlConnection } from '@/object-record/graphql/types/RecordGqlConnection';
|
||||||
import { capitalize } from 'twenty-shared';
|
import { assertUnreachable } from '@/workflow/utils/assertUnreachable';
|
||||||
|
import { RelationDefinitionType } from '~/generated-metadata/graphql';
|
||||||
|
|
||||||
export const isObjectRecordConnection = (
|
export const isObjectRecordConnection = (
|
||||||
objectNameSingular: string,
|
relationDefinition: NonNullable<FieldMetadataItem['relationDefinition']>,
|
||||||
value: unknown,
|
value: unknown,
|
||||||
): value is RecordGqlConnection => {
|
): value is RecordGqlConnection => {
|
||||||
const objectConnectionTypeName = `${capitalize(
|
switch (relationDefinition.direction) {
|
||||||
objectNameSingular,
|
case RelationDefinitionType.MANY_TO_MANY:
|
||||||
)}Connection`;
|
case RelationDefinitionType.ONE_TO_MANY: {
|
||||||
const objectEdgeTypeName = `${capitalize(objectNameSingular)}Edge`;
|
return true;
|
||||||
|
}
|
||||||
const objectConnectionSchema = z.object({
|
case RelationDefinitionType.MANY_TO_ONE:
|
||||||
__typename: z.literal(objectConnectionTypeName).optional(),
|
case RelationDefinitionType.ONE_TO_ONE: {
|
||||||
edges: z.array(
|
return false;
|
||||||
z.object({
|
}
|
||||||
__typename: z.literal(objectEdgeTypeName).optional(),
|
default: {
|
||||||
node: z.object({
|
return assertUnreachable(relationDefinition.direction);
|
||||||
id: z.string().uuid(),
|
}
|
||||||
}),
|
}
|
||||||
}),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
|
|
||||||
const connectionValidation = objectConnectionSchema.safeParse(value);
|
|
||||||
|
|
||||||
return connectionValidation.success;
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user