refactor: rewrite test connection with RQ

This commit is contained in:
Rıdvan Akca
2024-04-10 12:58:05 +02:00
parent 17fb935ea0
commit 5c9d3ed134
7 changed files with 43 additions and 82 deletions

View File

@@ -1,38 +0,0 @@
import App from '../../models/app.js';
import Connection from '../../models/connection.js';
import globalVariable from '../../helpers/global-variable.js';
const testConnection = async (_parent, params, context) => {
const conditions = context.currentUser.can('update', 'Connection');
const userConnections = context.currentUser.$relatedQuery('connections');
const allConnections = Connection.query();
const connectionBaseQuery = conditions.isCreator
? userConnections
: allConnections;
let connection = await connectionBaseQuery
.clone()
.findOne({
id: params.id,
})
.throwIfNotFound();
const app = await App.findOneByKey(connection.key, false);
const $ = await globalVariable({ connection, app });
let isStillVerified;
try {
isStillVerified = !!(await app.auth.isStillVerified($));
} catch {
isStillVerified = false;
}
connection = await connection.$query().patchAndFetch({
formattedData: connection.formattedData,
verified: isStillVerified,
});
return connection;
};
export default testConnection;

View File

@@ -1,13 +1,11 @@
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 testConnection from './queries/test-connection.js';
const queryResolvers = { const queryResolvers = {
getAppAuthClient, getAppAuthClient,
getConnectedApps, getConnectedApps,
getDynamicData, getDynamicData,
testConnection,
}; };
export default queryResolvers; export default queryResolvers;

View File

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

View File

@@ -1,5 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import { useLazyQuery, useMutation } from '@apollo/client'; import { useMutation } from '@apollo/client';
import CheckCircleIcon from '@mui/icons-material/CheckCircle'; import CheckCircleIcon from '@mui/icons-material/CheckCircle';
import ErrorIcon from '@mui/icons-material/Error'; import ErrorIcon from '@mui/icons-material/Error';
import Skeleton from '@mui/material/Skeleton'; import Skeleton from '@mui/material/Skeleton';
@@ -14,11 +14,11 @@ import { DateTime } from 'luxon';
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar'; import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
import ConnectionContextMenu from 'components/AppConnectionContextMenu'; import ConnectionContextMenu from 'components/AppConnectionContextMenu';
import { DELETE_CONNECTION } from 'graphql/mutations/delete-connection'; import { DELETE_CONNECTION } from 'graphql/mutations/delete-connection';
import { TEST_CONNECTION } from 'graphql/queries/test-connection';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { ConnectionPropType } from 'propTypes/propTypes'; import { ConnectionPropType } from 'propTypes/propTypes';
import { CardContent, Typography } from './style'; import { CardContent, Typography } from './style';
import useConnectionFlows from 'hooks/useConnectionFlows'; import useConnectionFlows from 'hooks/useConnectionFlows';
import useTestConnection from 'hooks/useTestConnection';
const countTranslation = (value) => ( const countTranslation = (value) => (
<> <>
@@ -36,19 +36,18 @@ function AppConnectionRow(props) {
const contextButtonRef = React.useRef(null); const contextButtonRef = React.useRef(null);
const [anchorEl, setAnchorEl] = React.useState(null); const [anchorEl, setAnchorEl] = React.useState(null);
const [testConnection, { called: testCalled, loading: testLoading }] =
useLazyQuery(TEST_CONNECTION, {
fetchPolicy: 'network-only',
onCompleted: () => {
setTimeout(() => setVerificationVisible(false), 3000);
},
onError: () => {
setTimeout(() => setVerificationVisible(false), 3000);
},
});
const [deleteConnection] = useMutation(DELETE_CONNECTION); const [deleteConnection] = useMutation(DELETE_CONNECTION);
const { mutate: testConnection, isPending: isTestConnectionPending } =
useTestConnection(
{ connectionId: id },
{
onSettled: () => {
setTimeout(() => setVerificationVisible(false), 3000);
},
},
);
const handleClose = () => { const handleClose = () => {
setAnchorEl(null); setAnchorEl(null);
}; };
@@ -113,7 +112,7 @@ function AppConnectionRow(props) {
<Box> <Box>
<Stack direction="row" alignItems="center" spacing={1}> <Stack direction="row" alignItems="center" spacing={1}>
{verificationVisible && testCalled && testLoading && ( {verificationVisible && isTestConnectionPending && (
<> <>
<CircularProgress size={16} /> <CircularProgress size={16} />
<Typography variant="caption"> <Typography variant="caption">
@@ -122,8 +121,7 @@ function AppConnectionRow(props) {
</> </>
)} )}
{verificationVisible && {verificationVisible &&
testCalled && !isTestConnectionPending &&
!testLoading &&
verified && ( verified && (
<> <>
<CheckCircleIcon fontSize="small" color="success" /> <CheckCircleIcon fontSize="small" color="success" />
@@ -133,8 +131,7 @@ function AppConnectionRow(props) {
</> </>
)} )}
{verificationVisible && {verificationVisible &&
testCalled && !isTestConnectionPending &&
!testLoading &&
!verified && ( !verified && (
<> <>
<ErrorIcon fontSize="small" color="error" /> <ErrorIcon fontSize="small" color="error" />

View File

@@ -1,5 +1,4 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useLazyQuery } from '@apollo/client';
import Autocomplete from '@mui/material/Autocomplete'; import Autocomplete from '@mui/material/Autocomplete';
import Button from '@mui/material/Button'; import Button from '@mui/material/Button';
import Collapse from '@mui/material/Collapse'; import Collapse from '@mui/material/Collapse';
@@ -12,7 +11,6 @@ import AppAuthClientsDialog from 'components/AppAuthClientsDialog/index.ee';
import FlowSubstepTitle from 'components/FlowSubstepTitle'; import FlowSubstepTitle from 'components/FlowSubstepTitle';
import useAppConfig from 'hooks/useAppConfig.ee'; import useAppConfig from 'hooks/useAppConfig.ee';
import { EditorContext } from 'contexts/Editor'; import { EditorContext } from 'contexts/Editor';
import { TEST_CONNECTION } from 'graphql/queries/test-connection';
import useAuthenticateApp from 'hooks/useAuthenticateApp.ee'; import useAuthenticateApp from 'hooks/useAuthenticateApp.ee';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { import {
@@ -23,6 +21,7 @@ import {
import useStepConnection from 'hooks/useStepConnection'; import useStepConnection from 'hooks/useStepConnection';
import { useQueryClient } from '@tanstack/react-query'; import { useQueryClient } from '@tanstack/react-query';
import useAppConnections from 'hooks/useAppConnections'; import useAppConnections from 'hooks/useAppConnections';
import useTestConnection from 'hooks/useTestConnection';
const ADD_CONNECTION_VALUE = 'ADD_CONNECTION'; const ADD_CONNECTION_VALUE = 'ADD_CONNECTION';
const ADD_SHARED_CONNECTION_VALUE = 'ADD_SHARED_CONNECTION'; const ADD_SHARED_CONNECTION_VALUE = 'ADD_SHARED_CONNECTION';
@@ -72,14 +71,10 @@ function ChooseConnectionSubstep(props) {
const stepConnection = stepConnectionData?.data; const stepConnection = stepConnectionData?.data;
// TODO: show detailed error when connection test/verification fails // TODO: show detailed error when connection test/verification fails
const [ const { mutate: testConnection, isPending: isTestConnectionPending } =
testConnection, useTestConnection({
{ loading: testResultLoading, refetch: retestConnection }, connectionId: stepConnection?.id,
] = useLazyQuery(TEST_CONNECTION, { });
variables: {
id: stepConnection?.id,
},
});
React.useEffect(() => { React.useEffect(() => {
if (stepConnection?.id) { if (stepConnection?.id) {
@@ -201,11 +196,11 @@ function ChooseConnectionSubstep(props) {
React.useEffect(() => { React.useEffect(() => {
if (stepConnection?.id) { if (stepConnection?.id) {
retestConnection({ testConnection({
id: stepConnection?.id, id: stepConnection?.id,
}); });
} }
}, [stepConnection?.id, retestConnection]); }, [stepConnection?.id, testConnection]);
const onToggle = expanded ? onCollapse : onExpand; const onToggle = expanded ? onCollapse : onExpand;
@@ -215,7 +210,7 @@ function ChooseConnectionSubstep(props) {
expanded={expanded} expanded={expanded}
onClick={onToggle} onClick={onToggle}
title={name} title={name}
valid={testResultLoading ? null : stepConnection?.verified} valid={isTestConnectionPending ? null : stepConnection?.verified}
/> />
<Collapse in={expanded} timeout="auto" unmountOnExit> <Collapse in={expanded} timeout="auto" unmountOnExit>
<ListItem <ListItem
@@ -253,7 +248,7 @@ function ChooseConnectionSubstep(props) {
onClick={onSubmit} onClick={onSubmit}
sx={{ mt: 2 }} sx={{ mt: 2 }}
disabled={ disabled={
testResultLoading || isTestConnectionPending ||
!stepConnection?.verified || !stepConnection?.verified ||
editorContext.readOnly editorContext.readOnly
} }

View File

@@ -1,9 +0,0 @@
import { gql } from '@apollo/client';
export const TEST_CONNECTION = gql`
query TestConnection($id: String!) {
testConnection(id: $id) {
id
verified
}
}
`;

View File

@@ -0,0 +1,19 @@
import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useTestConnection(
{ connectionId },
{ onSettled } = {},
) {
const query = useMutation({
mutationFn: async () => {
const { data } = await api.post(`/v1/connections/${connectionId}/test`);
return data;
},
onSettled,
});
return query;
}