Use refetch instead of cache modify when activating/deactivating a workflow (#9937)

Workflow version statuses are too complex to handle in cache. You always
need to:
- modify entity in cache
- modify queries in cache
- do the same for all other version statuses that should also be updated

This complex behavior leads to bugs because of a cache wrongly updated.

Let's simply do a refetch versions on status update.
This commit is contained in:
Thomas Trompette
2025-01-30 17:30:50 +01:00
committed by GitHub
parent afed36ef5b
commit 460253ab98
2 changed files with 20 additions and 94 deletions

View File

@ -1,12 +1,8 @@
import { useApolloClient, useMutation } from '@apollo/client';
import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/deactivateWorkflowVersion';
import { WorkflowVersion } from '@/workflow/types/Workflow';
import { isDefined } from 'twenty-ui';
import {
DeactivateWorkflowVersionMutation,
DeactivateWorkflowVersionMutationVariables,
@ -21,8 +17,8 @@ export const useDeactivateWorkflowVersion = () => {
client: apolloClient,
});
const { objectMetadataItem: objectMetadataItemWorkflowVersion } =
useObjectMetadataItem({
const { findManyRecordsQuery: findManyWorkflowVersionsQuery } =
useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});
@ -31,40 +27,14 @@ export const useDeactivateWorkflowVersion = () => {
variables: {
workflowVersionId,
},
update: () => {
modifyRecordFromCache({
cache: apolloClient.cache,
recordId: workflowVersionId,
objectMetadataItem: objectMetadataItemWorkflowVersion,
fieldModifiers: {
status: () => 'DEACTIVATED',
refetchQueries: [
{
query: findManyWorkflowVersionsQuery,
variables: {
id: workflowVersionId,
},
});
const cacheSnapshot = apolloClient.cache.extract();
const workflowVersion: WorkflowVersion | undefined = Object.values(
cacheSnapshot,
).find(
(item) =>
item.__typename === 'WorkflowVersion' &&
item.id === workflowVersionId,
);
if (!isDefined(workflowVersion)) {
return;
}
triggerUpdateRecordOptimisticEffect({
cache: apolloClient.cache,
objectMetadataItem: objectMetadataItemWorkflowVersion,
currentRecord: workflowVersion,
updatedRecord: {
...workflowVersion,
status: 'DEACTIVATED',
},
objectMetadataItems: [objectMetadataItemWorkflowVersion],
});
},
},
],
});
};