From a4013d47899c902049b67f40d51bd13278fbbad9 Mon Sep 17 00:00:00 2001 From: Marie Stoppa Date: Thu, 30 May 2024 18:31:28 +0200 Subject: [PATCH 1/2] Update cache manually instead of using refetchQuery --- .../hooks/useCreateOneDatabaseConnection.ts | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts b/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts index 3221c305c77a..dd6773b29fa8 100644 --- a/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts +++ b/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts @@ -1,5 +1,4 @@ import { ApolloClient, useMutation } from '@apollo/client'; -import { getOperationName } from '@apollo/client/utilities'; import { CREATE_ONE_DATABASE_CONNECTION } from '@/databases/graphql/mutations/createOneDatabaseConnection'; import { GET_MANY_DATABASE_CONNECTIONS } from '@/databases/graphql/queries/findManyDatabaseConnections'; @@ -9,6 +8,7 @@ import { CreateServerMutation, CreateServerMutationVariables, } from '~/generated-metadata/graphql'; +import { isDefined } from '~/utils/isDefined'; export const useCreateOneDatabaseConnection = () => { const apolloMetadataClient = useApolloMetadataClient(); @@ -27,8 +27,27 @@ export const useCreateOneDatabaseConnection = () => { variables: { input, }, - awaitRefetchQueries: true, - refetchQueries: [getOperationName(GET_MANY_DATABASE_CONNECTIONS) ?? ''], + update: (cache, { data }) => { + if (!data?.createOneRemoteServer) return; + + const getManyDatabaseConnectionsQuery = { + query: GET_MANY_DATABASE_CONNECTIONS, + variables: { + input: { foreignDataWrapperType: input.foreignDataWrapperType }, + }, + }; + + if (isDefined(cache.readQuery(getManyDatabaseConnectionsQuery))) { + cache.updateQuery(getManyDatabaseConnectionsQuery, (cachedQuery) => ({ + findManyRemoteServersByType: [ + ...cachedQuery.findManyRemoteServersByType, + data.createOneRemoteServer, + ], + })); + + return; + } + }, }); }; From 91ee4dbfb58cab385e7c8353103a1d3d1f94e2dc Mon Sep 17 00:00:00 2001 From: Marie Stoppa Date: Thu, 30 May 2024 18:37:03 +0200 Subject: [PATCH 2/2] Rename variable --- .../databases/hooks/useCreateOneDatabaseConnection.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts b/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts index dd6773b29fa8..8155f348def3 100644 --- a/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts +++ b/packages/twenty-front/src/modules/databases/hooks/useCreateOneDatabaseConnection.ts @@ -28,7 +28,8 @@ export const useCreateOneDatabaseConnection = () => { input, }, update: (cache, { data }) => { - if (!data?.createOneRemoteServer) return; + const createdRemoteServer = data?.createOneRemoteServer; + if (!createdRemoteServer) return; const getManyDatabaseConnectionsQuery = { query: GET_MANY_DATABASE_CONNECTIONS, @@ -41,7 +42,7 @@ export const useCreateOneDatabaseConnection = () => { cache.updateQuery(getManyDatabaseConnectionsQuery, (cachedQuery) => ({ findManyRemoteServersByType: [ ...cachedQuery.findManyRemoteServersByType, - data.createOneRemoteServer, + createdRemoteServer, ], }));