mirror of
https://github.com/twentyhq/twenty
synced 2026-04-21 21:47:38 +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>
111 lines
3.1 KiB
TypeScript
111 lines
3.1 KiB
TypeScript
import { EXECUTE_ONE_LOGIC_FUNCTION } from '@/logic-functions/graphql/mutations/executeOneLogicFunction';
|
|
import { useAtomFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilyStateValue';
|
|
import { useSetAtomFamilyState } from '@/ui/utilities/state/jotai/hooks/useSetAtomFamilyState';
|
|
import { logicFunctionTestDataFamilyState } from '@/workflow/workflow-steps/workflow-actions/code-action/states/logicFunctionTestDataFamilyState';
|
|
import { useMutation } from '@apollo/client/react';
|
|
import { useState } from 'react';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
import { LogicFunctionExecutionStatus } from '~/generated-metadata/graphql';
|
|
import { sleep } from '~/utils/sleep';
|
|
|
|
type ExecuteOneLogicFunctionInput = {
|
|
id: string;
|
|
payload: object;
|
|
};
|
|
|
|
type ExecuteOneLogicFunctionResult = {
|
|
data?: object | null;
|
|
logs: string;
|
|
duration: number;
|
|
status: LogicFunctionExecutionStatus;
|
|
error?: {
|
|
errorType: string;
|
|
errorMessage: string;
|
|
stackTrace: string;
|
|
} | null;
|
|
};
|
|
|
|
export const useExecuteLogicFunction = ({
|
|
logicFunctionId,
|
|
callback,
|
|
}: {
|
|
logicFunctionId: string;
|
|
callback?: (result: object) => void;
|
|
}) => {
|
|
const [isExecuting, setIsExecuting] = useState(false);
|
|
const [executeOneLogicFunctionMutation] = useMutation<
|
|
{ executeOneLogicFunction: ExecuteOneLogicFunctionResult },
|
|
{ input: ExecuteOneLogicFunctionInput }
|
|
>(EXECUTE_ONE_LOGIC_FUNCTION);
|
|
|
|
const logicFunctionTestData = useAtomFamilyStateValue(
|
|
logicFunctionTestDataFamilyState,
|
|
logicFunctionId,
|
|
);
|
|
|
|
const setLogicFunctionTestData = useSetAtomFamilyState(
|
|
logicFunctionTestDataFamilyState,
|
|
logicFunctionId,
|
|
);
|
|
|
|
const updateLogicFunctionInput = (input: object) => {
|
|
setLogicFunctionTestData((prev) => ({
|
|
...prev,
|
|
input,
|
|
}));
|
|
};
|
|
|
|
const executeLogicFunction = async () => {
|
|
if (isExecuting) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsExecuting(true);
|
|
await sleep(200); // Delay artificially to avoid flashing the UI
|
|
const result = await executeOneLogicFunctionMutation({
|
|
variables: {
|
|
input: {
|
|
id: logicFunctionId,
|
|
payload: logicFunctionTestData.input,
|
|
},
|
|
},
|
|
});
|
|
|
|
setIsExecuting(false);
|
|
|
|
const executionResult = result?.data?.executeOneLogicFunction;
|
|
|
|
if (isDefined(executionResult?.data)) {
|
|
callback?.(executionResult.data);
|
|
}
|
|
|
|
setLogicFunctionTestData((prev) => ({
|
|
...prev,
|
|
language: 'json',
|
|
output: {
|
|
data: executionResult?.data
|
|
? JSON.stringify(executionResult.data, null, 4)
|
|
: undefined,
|
|
logs: executionResult?.logs || '',
|
|
duration: executionResult?.duration,
|
|
status: (executionResult?.status ??
|
|
LogicFunctionExecutionStatus.IDLE) as LogicFunctionExecutionStatus,
|
|
error: executionResult?.error
|
|
? JSON.stringify(executionResult.error, null, 4)
|
|
: undefined,
|
|
},
|
|
}));
|
|
} catch (error) {
|
|
setIsExecuting(false);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
return {
|
|
executeLogicFunction,
|
|
updateLogicFunctionInput,
|
|
logicFunctionTestData,
|
|
isExecuting,
|
|
};
|
|
};
|