[BUG] Fix record relation optimistic mutation (#9881)
# Introduction It seems like optimistic caching isn't working as expected for any record relation mutation, CREATE UPDATE DELETE. It should not have an impact on the destroy We included a new `computeOptimisticRecordInput` that will calculate if a relation is added or detach. Updated the `triggerCreateRecordsOptimisticEffect` signature we should have a look to each of its call to determine if it should be checking cache or not Related to #9580 --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -11,6 +11,8 @@ import { isRecordMatchingFilter } from '@/object-record/record-filter/utils/isRe
|
||||
|
||||
import { CachedObjectRecordQueryVariables } from '@/apollo/types/CachedObjectRecordQueryVariables';
|
||||
import { encodeCursor } from '@/apollo/utils/encodeCursor';
|
||||
import { getRecordFromCache } from '@/object-record/cache/utils/getRecordFromCache';
|
||||
import { getRecordNodeFromRecord } from '@/object-record/cache/utils/getRecordNodeFromRecord';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName';
|
||||
|
||||
@ -19,28 +21,49 @@ import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName';
|
||||
We need to refactor how the record creation works in the RecordTable so the created record row is temporarily displayed with a local state,
|
||||
then we'll be able to uncomment the code below so the cached lists are updated coherently with the variables.
|
||||
*/
|
||||
type TriggerCreateRecordsOptimisticEffectArgs = {
|
||||
cache: ApolloCache<object>;
|
||||
objectMetadataItem: ObjectMetadataItem;
|
||||
recordsToCreate: RecordGqlNode[];
|
||||
objectMetadataItems: ObjectMetadataItem[];
|
||||
shouldMatchRootQueryFilter?: boolean;
|
||||
checkForRecordInCache?: boolean;
|
||||
};
|
||||
export const triggerCreateRecordsOptimisticEffect = ({
|
||||
cache,
|
||||
objectMetadataItem,
|
||||
recordsToCreate,
|
||||
objectMetadataItems,
|
||||
shouldMatchRootQueryFilter,
|
||||
}: {
|
||||
cache: ApolloCache<unknown>;
|
||||
objectMetadataItem: ObjectMetadataItem;
|
||||
recordsToCreate: RecordGqlNode[];
|
||||
objectMetadataItems: ObjectMetadataItem[];
|
||||
shouldMatchRootQueryFilter?: boolean;
|
||||
}) => {
|
||||
recordsToCreate.forEach((record) =>
|
||||
checkForRecordInCache = false,
|
||||
}: TriggerCreateRecordsOptimisticEffectArgs) => {
|
||||
const getRecordNodeFromCache = (recordId: string): RecordGqlNode | null => {
|
||||
const cachedRecord = getRecordFromCache({
|
||||
cache,
|
||||
objectMetadataItem,
|
||||
objectMetadataItems,
|
||||
recordId,
|
||||
});
|
||||
return getRecordNodeFromRecord({
|
||||
objectMetadataItem,
|
||||
objectMetadataItems,
|
||||
record: cachedRecord,
|
||||
computeReferences: false,
|
||||
});
|
||||
};
|
||||
|
||||
recordsToCreate.forEach((record) => {
|
||||
const currentSourceRecord = checkForRecordInCache
|
||||
? getRecordNodeFromCache(record.id)
|
||||
: null;
|
||||
triggerUpdateRelationsOptimisticEffect({
|
||||
cache,
|
||||
sourceObjectMetadataItem: objectMetadataItem,
|
||||
currentSourceRecord: null,
|
||||
currentSourceRecord,
|
||||
updatedSourceRecord: record,
|
||||
objectMetadataItems,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
cache.modify<StoreObject>({
|
||||
fields: {
|
||||
|
||||
@ -7,7 +7,6 @@ import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
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 { ApolloCache } from '@apollo/client';
|
||||
import { isArray } from '@sniptt/guards';
|
||||
import { FieldMetadataType } from '~/generated-metadata/graphql';
|
||||
@ -17,8 +16,8 @@ import { isDefined } from '~/utils/isDefined';
|
||||
type triggerUpdateRelationsOptimisticEffectArgs = {
|
||||
cache: ApolloCache<unknown>;
|
||||
sourceObjectMetadataItem: ObjectMetadataItem;
|
||||
currentSourceRecord: ObjectRecord | null;
|
||||
updatedSourceRecord: ObjectRecord | null;
|
||||
currentSourceRecord: RecordGqlNode | null;
|
||||
updatedSourceRecord: RecordGqlNode | null;
|
||||
objectMetadataItems: ObjectMetadataItem[];
|
||||
};
|
||||
export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
|
||||
Reference in New Issue
Block a user