feat: fetch database connection tables in Settings/Integrations/Datab… (#4882)

…ase/Connection

Closes #4758

---------

Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
Thaïs
2024-04-09 14:22:15 +02:00
committed by GitHub
parent 19df43156e
commit 704f7f6d8e
21 changed files with 275 additions and 87 deletions

View File

@ -0,0 +1,12 @@
import { gql } from '@apollo/client';
export const DATABASE_CONNECTION_FRAGMENT = gql`
fragment RemoteServerFields on RemoteServer {
id
createdAt
foreignDataWrapperId
foreignDataWrapperOptions
foreignDataWrapperType
updatedAt
}
`;

View File

@ -1,12 +1,12 @@
import { gql } from '@apollo/client';
import { DATABASE_CONNECTION_FRAGMENT } from '@/databases/graphql/fragments/databaseConnectionFragment';
export const CREATE_ONE_DATABASE_CONNECTION = gql`
${DATABASE_CONNECTION_FRAGMENT}
mutation createServer($input: CreateRemoteServerInput!) {
createOneRemoteServer(input: $input) {
id
foreignDataWrapperId
foreignDataWrapperOptions
foreignDataWrapperType
...RemoteServerFields
}
}
`;

View File

@ -0,0 +1,11 @@
import { gql } from '@apollo/client';
export const GET_MANY_DATABASE_CONNECTION_TABLES = gql`
query GetManyDatabaseConnectionTables($input: RemoteServerIdInput!) {
findAvailableRemoteTablesByServerId(input: $input) {
name
schema
status
}
}
`;

View File

@ -1,14 +1,12 @@
import { gql } from '@apollo/client';
import { DATABASE_CONNECTION_FRAGMENT } from '@/databases/graphql/fragments/databaseConnectionFragment';
export const GET_MANY_DATABASE_CONNECTIONS = gql`
${DATABASE_CONNECTION_FRAGMENT}
query GetManyDatabaseConnections($input: RemoteServerTypeInput!) {
findManyRemoteServersByType(input: $input) {
id
createdAt
foreignDataWrapperId
foreignDataWrapperOptions
foreignDataWrapperType
updatedAt
...RemoteServerFields
}
}
`;

View File

@ -0,0 +1,12 @@
import { gql } from '@apollo/client';
import { DATABASE_CONNECTION_FRAGMENT } from '@/databases/graphql/fragments/databaseConnectionFragment';
export const GET_ONE_DATABASE_CONNECTION = gql`
${DATABASE_CONNECTION_FRAGMENT}
query GetOneDatabaseConnection($input: RemoteServerIdInput!) {
findOneRemoteServerById(input: $input) {
...RemoteServerFields
}
}
`;

View File

@ -0,0 +1,47 @@
import { useQuery } from '@apollo/client';
import { GET_ONE_DATABASE_CONNECTION } from '@/databases/graphql/queries/findOneDatabaseConnection';
import { getForeignDataWrapperType } from '@/databases/utils/getForeignDataWrapperType';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import {
GetOneDatabaseConnectionQuery,
GetOneDatabaseConnectionQueryVariables,
} from '~/generated-metadata/graphql';
type UseGetDatabaseConnectionParams = {
databaseKey: string;
connectionId: string;
skip?: boolean;
};
export const useGetDatabaseConnection = ({
databaseKey,
connectionId,
skip,
}: UseGetDatabaseConnectionParams) => {
const apolloMetadataClient = useApolloMetadataClient();
const foreignDataWrapperType = getForeignDataWrapperType(databaseKey);
const { data, loading } = useQuery<
GetOneDatabaseConnectionQuery,
GetOneDatabaseConnectionQueryVariables
>(GET_ONE_DATABASE_CONNECTION, {
client: apolloMetadataClient ?? undefined,
skip: skip || !apolloMetadataClient || !foreignDataWrapperType,
variables: {
input: {
id: connectionId,
},
},
});
const connection = data?.findOneRemoteServerById ?? null;
return {
connection:
connection?.foreignDataWrapperType === foreignDataWrapperType
? connection
: null,
loading,
};
};

View File

@ -0,0 +1,37 @@
import { useQuery } from '@apollo/client';
import { GET_MANY_DATABASE_CONNECTION_TABLES } from '@/databases/graphql/queries/findManyDatabaseConnectionTables';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import {
GetManyDatabaseConnectionTablesQuery,
GetManyDatabaseConnectionTablesQueryVariables,
} from '~/generated-metadata/graphql';
type UseGetDatabaseConnectionTablesParams = {
connectionId: string;
skip?: boolean;
};
export const useGetDatabaseConnectionTables = ({
connectionId,
skip,
}: UseGetDatabaseConnectionTablesParams) => {
const apolloMetadataClient = useApolloMetadataClient();
const { data } = useQuery<
GetManyDatabaseConnectionTablesQuery,
GetManyDatabaseConnectionTablesQueryVariables
>(GET_MANY_DATABASE_CONNECTION_TABLES, {
client: apolloMetadataClient ?? undefined,
skip: skip || !apolloMetadataClient,
variables: {
input: {
id: connectionId,
},
},
});
return {
tables: data?.findAvailableRemoteTablesByServerId || [],
};
};