mirror of
https://github.com/twentyhq/twenty
synced 2026-04-21 13:37:22 +00:00
## 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>
167 lines
5.1 KiB
TypeScript
167 lines
5.1 KiB
TypeScript
import { useCallback } from 'react';
|
|
|
|
import { UPDATE_ONE_LOGIC_FUNCTION } from '@/logic-functions/graphql/mutations/updateOneLogicFunction';
|
|
import { GET_LOGIC_FUNCTION_SOURCE_CODE } from '@/logic-functions/graphql/queries/getLogicFunctionSourceCode';
|
|
import { useMetadataErrorHandler } from '@/metadata-error-handler/hooks/useMetadataErrorHandler';
|
|
import { type MetadataRequestResult } from '@/object-metadata/types/MetadataRequestResult.type';
|
|
import { CREATE_ONE_LOGIC_FUNCTION } from '@/logic-functions/graphql/mutations/createOneLogicFunction';
|
|
import { DELETE_ONE_LOGIC_FUNCTION } from '@/logic-functions/graphql/mutations/deleteOneLogicFunction';
|
|
import { FIND_MANY_LOGIC_FUNCTIONS } from '@/logic-functions/graphql/queries/findManyLogicFunctions';
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|
import { useMutation } from '@apollo/client/react';
|
|
import { CombinedGraphQLErrors } from '@apollo/client/errors';
|
|
import { getOperationName } from '~/utils/getOperationName';
|
|
import { t } from '@lingui/core/macro';
|
|
import { CrudOperationType } from 'twenty-shared/types';
|
|
import {
|
|
type CreateOneLogicFunctionMutation,
|
|
type CreateOneLogicFunctionMutationVariables,
|
|
type DeleteOneLogicFunctionMutation,
|
|
type DeleteOneLogicFunctionMutationVariables,
|
|
type UpdateOneLogicFunctionMutation,
|
|
type UpdateOneLogicFunctionMutationVariables,
|
|
} from '~/generated-metadata/graphql';
|
|
|
|
export const usePersistLogicFunction = () => {
|
|
const { handleMetadataError } = useMetadataErrorHandler();
|
|
const { enqueueErrorSnackBar } = useSnackBar();
|
|
|
|
const [createLogicFunctionMutation] = useMutation<
|
|
CreateOneLogicFunctionMutation,
|
|
CreateOneLogicFunctionMutationVariables
|
|
>(CREATE_ONE_LOGIC_FUNCTION);
|
|
|
|
const [deleteLogicFunctionMutation] = useMutation<
|
|
DeleteOneLogicFunctionMutation,
|
|
DeleteOneLogicFunctionMutationVariables
|
|
>(DELETE_ONE_LOGIC_FUNCTION);
|
|
|
|
const [updateLogicFunctionSourceMutation] = useMutation<
|
|
UpdateOneLogicFunctionMutation,
|
|
UpdateOneLogicFunctionMutationVariables
|
|
>(UPDATE_ONE_LOGIC_FUNCTION);
|
|
|
|
const createLogicFunction = useCallback(
|
|
async (
|
|
variables: CreateOneLogicFunctionMutationVariables,
|
|
): Promise<
|
|
MetadataRequestResult<
|
|
Awaited<ReturnType<typeof createLogicFunctionMutation>>
|
|
>
|
|
> => {
|
|
try {
|
|
const result = await createLogicFunctionMutation({
|
|
variables,
|
|
awaitRefetchQueries: true,
|
|
refetchQueries: [getOperationName(FIND_MANY_LOGIC_FUNCTIONS) ?? ''],
|
|
});
|
|
|
|
return {
|
|
status: 'successful',
|
|
response: result,
|
|
};
|
|
} catch (error) {
|
|
if (CombinedGraphQLErrors.is(error)) {
|
|
handleMetadataError(error, {
|
|
primaryMetadataName: 'logicFunction',
|
|
operationType: CrudOperationType.CREATE,
|
|
});
|
|
} else {
|
|
enqueueErrorSnackBar({ message: t`An error occurred.` });
|
|
}
|
|
|
|
return {
|
|
status: 'failed',
|
|
error,
|
|
};
|
|
}
|
|
},
|
|
[createLogicFunctionMutation, handleMetadataError, enqueueErrorSnackBar],
|
|
);
|
|
|
|
const updateLogicFunction = useCallback(
|
|
async (
|
|
variables: UpdateOneLogicFunctionMutationVariables,
|
|
): Promise<
|
|
MetadataRequestResult<
|
|
Awaited<ReturnType<typeof updateLogicFunctionSourceMutation>>
|
|
>
|
|
> => {
|
|
try {
|
|
const result = await updateLogicFunctionSourceMutation({
|
|
variables,
|
|
});
|
|
|
|
return {
|
|
status: 'successful',
|
|
response: result,
|
|
};
|
|
} catch (error) {
|
|
if (CombinedGraphQLErrors.is(error)) {
|
|
handleMetadataError(error, {
|
|
primaryMetadataName: 'logicFunction',
|
|
operationType: CrudOperationType.UPDATE,
|
|
});
|
|
} else {
|
|
enqueueErrorSnackBar({ message: t`An error occurred.` });
|
|
}
|
|
|
|
return {
|
|
status: 'failed',
|
|
error,
|
|
};
|
|
}
|
|
},
|
|
[
|
|
updateLogicFunctionSourceMutation,
|
|
handleMetadataError,
|
|
enqueueErrorSnackBar,
|
|
],
|
|
);
|
|
|
|
const deleteLogicFunction = useCallback(
|
|
async (
|
|
variables: DeleteOneLogicFunctionMutationVariables,
|
|
): Promise<
|
|
MetadataRequestResult<
|
|
Awaited<ReturnType<typeof deleteLogicFunctionMutation>>
|
|
>
|
|
> => {
|
|
try {
|
|
const result = await deleteLogicFunctionMutation({
|
|
variables,
|
|
awaitRefetchQueries: true,
|
|
refetchQueries: [
|
|
getOperationName(GET_LOGIC_FUNCTION_SOURCE_CODE) ?? '',
|
|
],
|
|
});
|
|
|
|
return {
|
|
status: 'successful',
|
|
response: result,
|
|
};
|
|
} catch (error) {
|
|
if (CombinedGraphQLErrors.is(error)) {
|
|
handleMetadataError(error, {
|
|
primaryMetadataName: 'logicFunction',
|
|
operationType: CrudOperationType.DELETE,
|
|
});
|
|
} else {
|
|
enqueueErrorSnackBar({ message: t`An error occurred.` });
|
|
}
|
|
|
|
return {
|
|
status: 'failed',
|
|
error,
|
|
};
|
|
}
|
|
},
|
|
[deleteLogicFunctionMutation, handleMetadataError, enqueueErrorSnackBar],
|
|
);
|
|
|
|
return {
|
|
createLogicFunction,
|
|
updateLogicFunction,
|
|
deleteLogicFunction,
|
|
};
|
|
};
|