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,11 +1,8 @@
import { useApolloClient, useMutation } from '@apollo/client'; 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 { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache'; import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/activateWorkflowVersion'; import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/activateWorkflowVersion';
import { WorkflowVersion } from '@/workflow/types/Workflow';
import { import {
ActivateWorkflowVersionMutation, ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables, ActivateWorkflowVersionMutationVariables,
@ -20,8 +17,8 @@ export const useActivateWorkflowVersion = () => {
client: apolloClient, client: apolloClient,
}); });
const { objectMetadataItem: objectMetadataItemWorkflowVersion } = const { findManyRecordsQuery: findManyWorkflowVersionsQuery } =
useObjectMetadataItem({ useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion, objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
}); });
@ -36,55 +33,14 @@ export const useActivateWorkflowVersion = () => {
variables: { variables: {
workflowVersionId, workflowVersionId,
}, },
update: () => { refetchQueries: [
modifyRecordFromCache({ {
cache: apolloClient.cache, query: findManyWorkflowVersionsQuery,
recordId: workflowVersionId, variables: {
objectMetadataItem: objectMetadataItemWorkflowVersion, id: workflowId,
fieldModifiers: {
status: () => 'ACTIVE',
}, },
}); },
],
const cacheSnapshot = apolloClient.cache.extract();
const allWorkflowVersions: Array<WorkflowVersion> = Object.values(
cacheSnapshot,
).filter(
(item) =>
item.__typename === 'WorkflowVersion' &&
item.workflowId === workflowId,
);
const previousActiveWorkflowVersions = allWorkflowVersions.filter(
(version) =>
version.status === 'ACTIVE' && version.id !== workflowVersionId,
);
for (const workflowVersion of previousActiveWorkflowVersions) {
apolloClient.cache.modify({
id: apolloClient.cache.identify(workflowVersion),
fields: {
status: () => {
return workflowVersion.id !== workflowVersionId &&
workflowVersion.status === 'ACTIVE'
? 'ARCHIVED'
: workflowVersion.status;
},
},
});
triggerUpdateRecordOptimisticEffect({
cache: apolloClient.cache,
objectMetadataItem: objectMetadataItemWorkflowVersion,
currentRecord: workflowVersion,
updatedRecord: {
...workflowVersion,
status: 'ARCHIVED',
},
objectMetadataItems: [objectMetadataItemWorkflowVersion],
});
}
},
}); });
}; };

View File

@ -1,12 +1,8 @@
import { useApolloClient, useMutation } from '@apollo/client'; 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 { 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 { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/deactivateWorkflowVersion';
import { WorkflowVersion } from '@/workflow/types/Workflow';
import { isDefined } from 'twenty-ui';
import { import {
DeactivateWorkflowVersionMutation, DeactivateWorkflowVersionMutation,
DeactivateWorkflowVersionMutationVariables, DeactivateWorkflowVersionMutationVariables,
@ -21,8 +17,8 @@ export const useDeactivateWorkflowVersion = () => {
client: apolloClient, client: apolloClient,
}); });
const { objectMetadataItem: objectMetadataItemWorkflowVersion } = const { findManyRecordsQuery: findManyWorkflowVersionsQuery } =
useObjectMetadataItem({ useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion, objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
}); });
@ -31,40 +27,14 @@ export const useDeactivateWorkflowVersion = () => {
variables: { variables: {
workflowVersionId, workflowVersionId,
}, },
update: () => { refetchQueries: [
modifyRecordFromCache({ {
cache: apolloClient.cache, query: findManyWorkflowVersionsQuery,
recordId: workflowVersionId, variables: {
objectMetadataItem: objectMetadataItemWorkflowVersion, id: workflowVersionId,
fieldModifiers: {
status: () => 'DEACTIVATED',
}, },
}); },
],
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],
});
},
}); });
}; };