Files
twenty/packages/twenty-front/src/modules/workflow/hooks/useDeactivateWorkflowVersion.ts
T
Félix Malfait b470cb21a1 Upgrade Apollo Client to v4 and refactor error handling (#18584)
## Summary
This PR upgrades Apollo Client from v3.10.0 to v4 and refactors error
handling patterns across the codebase to use a new centralized
`useSnackBarOnQueryError` hook.

## Key Changes

- **Dependency Update**: Upgraded `@apollo/client` from `^3.10.0` to
`^3.11.0` in root package.json
- **New Hook**: Added `useSnackBarOnQueryError` hook for centralized
Apollo query error handling with snack bar notifications
- **Error Handling Refactor**: Updated 100+ files to use the new error
handling pattern:
  - Removed direct `ApolloError` imports where no longer needed
- Replaced manual error handling logic with `useSnackBarOnQueryError`
hook
- Simplified error handling in hooks and components across multiple
modules
- **GraphQL Codegen**: Updated codegen configuration files to work with
Apollo Client v3.11.0
- **Type Definitions**: Added TypeScript declaration file for
`apollo-upload-client` module
- **Test Updates**: Updated test files to reflect new error handling
patterns

## Notable Implementation Details

- The new `useSnackBarOnQueryError` hook provides a consistent way to
handle Apollo query errors with automatic snack bar notifications
- Changes span across multiple feature areas: auth, object records,
settings, workflows, billing, and more
- All changes maintain backward compatibility while improving code
maintainability and reducing duplication
- Jest configuration updated to work with the new Apollo Client version

https://claude.ai/code/session_019WGZ6Rd7sEHuBg9sTrXRqJ

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-13 14:59:46 +01:00

132 lines
4.5 KiB
TypeScript

import { useMutation } from '@apollo/client/react';
import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect';
import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { CoreObjectNameSingular } from 'twenty-shared/types';
import { getRecordFromCache } from '@/object-record/cache/utils/getRecordFromCache';
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
import { useObjectPermissions } from '@/object-record/hooks/useObjectPermissions';
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/deactivateWorkflowVersion';
import {
type Workflow,
type WorkflowStatus,
type WorkflowVersion,
} from '@/workflow/types/Workflow';
import { isDefined } from 'twenty-shared/utils';
import {
type DeactivateWorkflowVersionMutation,
type DeactivateWorkflowVersionMutationVariables,
} from '~/generated/graphql';
export const useDeactivateWorkflowVersion = () => {
const apolloCoreClient = useApolloCoreClient();
const { objectPermissionsByObjectMetadataId } = useObjectPermissions();
const { objectMetadataItems } = useObjectMetadataItems();
const [mutate] = useMutation<
DeactivateWorkflowVersionMutation,
DeactivateWorkflowVersionMutationVariables
>(DEACTIVATE_WORKFLOW_VERSION, {
client: apolloCoreClient,
});
const { upsertRecordsInStore } = useUpsertRecordsInStore();
const { objectMetadataItem: objectMetadataItemWorkflow } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.Workflow,
});
const { objectMetadataItem: objectMetadataItemWorkflowVersion } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});
const deactivateWorkflowVersion = async ({
workflowVersionId,
}: {
workflowVersionId: string;
}) => {
await mutate({
variables: {
workflowVersionId,
},
update: () => {
modifyRecordFromCache({
cache: apolloCoreClient.cache,
recordId: workflowVersionId,
objectMetadataItem: objectMetadataItemWorkflowVersion,
fieldModifiers: {
status: () => 'DEACTIVATED',
},
});
const cacheSnapshot = apolloCoreClient.cache.extract() as Record<
string,
Record<string, unknown>
>;
const workflowVersion = (
Object.values(cacheSnapshot) as Array<Record<string, unknown>>
).find(
(item) =>
item.__typename === 'WorkflowVersion' &&
item.id === workflowVersionId,
) as WorkflowVersion | undefined;
if (!isDefined(workflowVersion)) {
return;
}
triggerUpdateRecordOptimisticEffect({
cache: apolloCoreClient.cache,
objectMetadataItem: objectMetadataItemWorkflowVersion,
currentRecord: workflowVersion,
updatedRecord: {
...workflowVersion,
status: 'DEACTIVATED',
},
objectMetadataItems,
objectPermissionsByObjectMetadataId,
upsertRecordsInStore,
});
const cachedWorkflow = getRecordFromCache<Workflow>({
objectMetadataItem: objectMetadataItemWorkflow,
cache: apolloCoreClient.cache,
objectMetadataItems,
objectPermissionsByObjectMetadataId,
recordId: workflowVersion.workflowId,
});
const newStatuses = new Set(
[...(cachedWorkflow?.statuses ?? []), 'DEACTIVATED'].filter(
(status) => status !== 'ACTIVE',
),
);
if (isDefined(cachedWorkflow)) {
modifyRecordFromCache({
cache: apolloCoreClient.cache,
recordId: workflowVersion.workflowId,
objectMetadataItem: objectMetadataItemWorkflow,
fieldModifiers: {
statuses: () => Array.from(newStatuses),
},
});
upsertRecordsInStore({
partialRecords: [
{
...cachedWorkflow,
statuses: Array.from(newStatuses) as WorkflowStatus[],
},
],
});
}
},
});
};
return { deactivateWorkflowVersion };
};