Merge pull request #1766 from automatisch/AUT-705

refactor: rewrite useStepWithTestExecutions with RQ
This commit is contained in:
Ali BARIN
2024-04-10 13:30:04 +02:00
committed by GitHub
8 changed files with 32 additions and 83 deletions

View File

@@ -1,34 +0,0 @@
import { ref } from 'objection';
import ExecutionStep from '../../models/execution-step.js';
import Step from '../../models/step.js';
const getStepWithTestExecutions = async (_parent, params, context) => {
const conditions = context.currentUser.can('update', 'Flow');
const userSteps = context.currentUser.$relatedQuery('steps');
const allSteps = Step.query();
const stepBaseQuery = conditions.isCreator ? userSteps : allSteps;
const step = await stepBaseQuery
.clone()
.findOne({ 'steps.id': params.stepId })
.throwIfNotFound();
const previousStepsWithCurrentStep = await stepBaseQuery
.clone()
.withGraphJoined('executionSteps')
.where('flow_id', '=', step.flowId)
.andWhere('position', '<', step.position)
.andWhere(
'executionSteps.created_at',
'=',
ExecutionStep.query()
.max('created_at')
.where('step_id', '=', ref('steps.id'))
.andWhere('status', 'success')
)
.orderBy('steps.position', 'asc');
return previousStepsWithCurrentStep;
};
export default getStepWithTestExecutions;

View File

@@ -1,14 +1,12 @@
import getAppAuthClient from './queries/get-app-auth-client.ee.js'; import getAppAuthClient from './queries/get-app-auth-client.ee.js';
import getConnectedApps from './queries/get-connected-apps.js'; import getConnectedApps from './queries/get-connected-apps.js';
import getDynamicData from './queries/get-dynamic-data.js'; import getDynamicData from './queries/get-dynamic-data.js';
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
import testConnection from './queries/test-connection.js'; import testConnection from './queries/test-connection.js';
const queryResolvers = { const queryResolvers = {
getAppAuthClient, getAppAuthClient,
getConnectedApps, getConnectedApps,
getDynamicData, getDynamicData,
getStepWithTestExecutions,
testConnection, testConnection,
}; };

View File

@@ -2,7 +2,6 @@ type Query {
getAppAuthClient(id: String!): AppAuthClient getAppAuthClient(id: String!): AppAuthClient
getConnectedApps(name: String): [App] getConnectedApps(name: String): [App]
testConnection(id: String!): Connection testConnection(id: String!): Connection
getStepWithTestExecutions(stepId: String!): [Step]
getDynamicData( getDynamicData(
stepId: String! stepId: String!
key: String! key: String!

View File

@@ -1,6 +1,5 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import * as React from 'react'; import * as React from 'react';
import { useLazyQuery } from '@apollo/client';
import Stack from '@mui/material/Stack'; import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography'; import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box'; import Box from '@mui/material/Box';
@@ -24,7 +23,7 @@ import ChooseConnectionSubstep from 'components/ChooseConnectionSubstep';
import Form from 'components/Form'; import Form from 'components/Form';
import FlowStepContextMenu from 'components/FlowStepContextMenu'; import FlowStepContextMenu from 'components/FlowStepContextMenu';
import AppIcon from 'components/AppIcon'; import AppIcon from 'components/AppIcon';
import { GET_STEP_WITH_TEST_EXECUTIONS } from 'graphql/queries/get-step-with-test-executions';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import useApps from 'hooks/useApps'; import useApps from 'hooks/useApps';
import { import {
@@ -40,6 +39,7 @@ import useTriggers from 'hooks/useTriggers';
import useActions from 'hooks/useActions'; import useActions from 'hooks/useActions';
import useTriggerSubsteps from 'hooks/useTriggerSubsteps'; import useTriggerSubsteps from 'hooks/useTriggerSubsteps';
import useActionSubsteps from 'hooks/useActionSubsteps'; import useActionSubsteps from 'hooks/useActionSubsteps';
import useStepWithTestExecutions from 'hooks/useStepWithTestExecutions';
const validIcon = <CheckCircleIcon color="success" />; const validIcon = <CheckCircleIcon color="success" />;
const errorIcon = <ErrorIcon color="error" />; const errorIcon = <ErrorIcon color="error" />;
@@ -126,28 +126,16 @@ function FlowStep(props) {
const { data: apps } = useApps(useAppsOptions); const { data: apps } = useApps(useAppsOptions);
const [ const { data: stepWithTestExecutions, refetch } = useStepWithTestExecutions(
getStepWithTestExecutions, step.id,
{ data: stepWithTestExecutionsData, called: stepWithTestExecutionsCalled }, );
] = useLazyQuery(GET_STEP_WITH_TEST_EXECUTIONS, { const stepWithTestExecutionsData = stepWithTestExecutions?.data;
fetchPolicy: 'network-only',
});
React.useEffect(() => { React.useEffect(() => {
if (!stepWithTestExecutionsCalled && !collapsed && !isTrigger) { if (!collapsed && !isTrigger) {
getStepWithTestExecutions({ refetch(step.id);
variables: {
stepId: step.id,
},
});
} }
}, [ }, [collapsed, refetch, step.id, isTrigger]);
collapsed,
stepWithTestExecutionsCalled,
getStepWithTestExecutions,
step.id,
isTrigger,
]);
const app = apps?.data?.find((currentApp) => currentApp.key === step.appKey); const app = apps?.data?.find((currentApp) => currentApp.key === step.appKey);
@@ -274,9 +262,7 @@ function FlowStep(props) {
<Collapse in={!collapsed} unmountOnExit> <Collapse in={!collapsed} unmountOnExit>
<Content> <Content>
<List> <List>
<StepExecutionsProvider <StepExecutionsProvider value={stepWithTestExecutionsData}>
value={stepWithTestExecutionsData?.getStepWithTestExecutions}
>
<Form <Form
defaultValues={step} defaultValues={step}
onSubmit={handleSubmit} onSubmit={handleSubmit}

View File

@@ -4,6 +4,7 @@ import { useMutation } from '@apollo/client';
import Menu from '@mui/material/Menu'; import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem'; import MenuItem from '@mui/material/MenuItem';
import { DELETE_STEP } from 'graphql/mutations/delete-step'; import { DELETE_STEP } from 'graphql/mutations/delete-step';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { useQueryClient } from '@tanstack/react-query'; import { useQueryClient } from '@tanstack/react-query';
@@ -12,10 +13,7 @@ function FlowStepContextMenu(props) {
const { stepId, onClose, anchorEl, deletable, flowId } = props; const { stepId, onClose, anchorEl, deletable, flowId } = props;
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const [deleteStep] = useMutation(DELETE_STEP);
const [deleteStep] = useMutation(DELETE_STEP, {
refetchQueries: ['GetStepWithTestExecutions'],
});
const deleteActionHandler = React.useCallback( const deleteActionHandler = React.useCallback(
async (event) => { async (event) => {

View File

@@ -49,7 +49,6 @@ function TestSubstep(props) {
const [executeFlow, { data, error, loading, called, reset }] = useMutation( const [executeFlow, { data, error, loading, called, reset }] = useMutation(
EXECUTE_FLOW, EXECUTE_FLOW,
{ {
refetchQueries: ['GetStepWithTestExecutions'],
context: { autoSnackbar: false }, context: { autoSnackbar: false },
}, },
); );
@@ -85,7 +84,7 @@ function TestSubstep(props) {
await queryClient.invalidateQueries({ await queryClient.invalidateQueries({
queryKey: ['flow', flowId], queryKey: ['flow', flowId],
}); });
}, [onSubmit, onContinue, isCompleted, step.id, queryClient, flowId]); }, [onSubmit, onContinue, isCompleted, queryClient, flowId]);
const onToggle = expanded ? onCollapse : onExpand; const onToggle = expanded ? onCollapse : onExpand;

View File

@@ -1,16 +0,0 @@
import { gql } from '@apollo/client';
export const GET_STEP_WITH_TEST_EXECUTIONS = gql`
query GetStepWithTestExecutions($stepId: String!) {
getStepWithTestExecutions(stepId: $stepId) {
id
appKey
executionSteps {
id
executionId
stepId
status
dataOut
}
}
}
`;

View File

@@ -0,0 +1,19 @@
import { useQuery } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useStepWithTestExecutions(stepId) {
const query = useQuery({
queryKey: ['stepWithTestExecutions', stepId],
queryFn: async ({ signal }) => {
const { data } = await api.get(`/v1/steps/${stepId}/previous-steps`, {
signal,
});
return data;
},
enabled: false,
});
return query;
}