Skip to content

Commit

Permalink
refactor(front): handle ObjectRecord and not grapql record or connection
Browse files Browse the repository at this point in the history
  • Loading branch information
prastoin committed Jan 24, 2025
1 parent 8d6f5aa commit fcf12a6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { triggerDetachRelationOptimisticEffect } from '@/apollo/optimistic-effec
import { CORE_OBJECT_NAMES_TO_DELETE_ON_TRIGGER_RELATION_DETACH } from '@/apollo/types/coreObjectNamesToDeleteOnRelationDetach';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { getRecordFromRecordNode } from '@/object-record/cache/utils/getRecordFromRecordNode';
import { getRecordsFromRecordConnection } from '@/object-record/cache/utils/getRecordsFromRecordConnection';
import { isObjectRecordConnection } from '@/object-record/cache/utils/isObjectRecordConnection';
import { RecordGqlConnection } from '@/object-record/graphql/types/RecordGqlConnection';
import { RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { hasManyTargetRecords } from '@/object-record/utils/hasManyTargetRecords';
import isEmpty from 'lodash.isempty';
import { FieldMetadataType } from '~/generated-metadata/graphql';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
Expand Down Expand Up @@ -83,9 +85,6 @@ export const triggerUpdateRelationsOptimisticEffect = ({
) {
return;
}
const isRecordConnection = hasManyTargetRecords(relationDefinition);
// TODO refactor typing as possible ? review RecordGqlConnection | RecordGqlNode | null
// TODO this should transpile from RecordGqlNode to ObjectRecord ( find utils already exising )
const extractTargetRecordsFromRelation = (
value: RecordGqlConnection | RecordGqlNode | null,
): ObjectRecord[] => {
Expand All @@ -95,11 +94,11 @@ export const triggerUpdateRelationsOptimisticEffect = ({
return [];
}

if (isRecordConnection) {
return value.edges.map(({ node }: any) => node as RecordGqlNode);
if (isObjectRecordConnection(relationDefinition, value)) {
return getRecordsFromRecordConnection({recordConnection: value})
}

return [value] as RecordGqlNode[];
return [getRecordFromRecordNode({recordNode: value})];
};
const targetRecordsToDetachFrom = extractTargetRecordsFromRelation(
currentFieldValueOnSourceRecord,
Expand All @@ -121,7 +120,7 @@ export const triggerUpdateRelationsOptimisticEffect = ({
recordsToDestroy: targetRecordsToDetachFrom,
objectMetadataItems,
});
// Could be an invariant ?
// Could it be an invariant ?
} else if (isDefined(currentSourceRecord)) {
targetRecordsToDetachFrom.forEach((targetRecordToDetachFrom) => {
triggerDetachRelationOptimisticEffect({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { peopleQueryResult } from '~/testing/mock-data/people';

import { isObjectRecordConnection } from '@/object-record/cache/utils/isObjectRecordConnection';

// TODO refactor consuming relation from mockss
describe('isObjectRecordConnection', () => {
it('should work with query result', () => {
const validQueryResult = peopleQueryResult.people;

const isValidQueryResult = isObjectRecordConnection(
'person',
{},
validQueryResult,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import { z } from 'zod';

import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
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 = (
objectNameSingular: string,
relationDefinition: NonNullable<FieldMetadataItem['relationDefinition']>,
value: unknown,
): value is RecordGqlConnection => {
const objectConnectionTypeName = `${capitalize(
objectNameSingular,
)}Connection`;
const objectEdgeTypeName = `${capitalize(objectNameSingular)}Edge`;

const objectConnectionSchema = z.object({
__typename: z.literal(objectConnectionTypeName).optional(),
edges: z.array(
z.object({
__typename: z.literal(objectEdgeTypeName).optional(),
node: z.object({
id: z.string().uuid(),
}),
}),
),
});

const connectionValidation = objectConnectionSchema.safeParse(value);

return connectionValidation.success;
};
switch (relationDefinition.direction) {
case RelationDefinitionType.MANY_TO_MANY:
case RelationDefinitionType.ONE_TO_MANY: {
return true;
}
case RelationDefinitionType.MANY_TO_ONE:
case RelationDefinitionType.ONE_TO_ONE: {
return false;
}
default: {
return assertUnreachable(relationDefinition.direction);
}
}
}

This file was deleted.

0 comments on commit fcf12a6

Please sign in to comment.