Files
twenty_crm/packages/twenty-front/src/modules/databases/hooks/useSyncRemoteTableSchemaChanges.ts
nitin d2ddd6f473 Separate system operations from core objects in GraphQL endpoints (#12977)
Moves system-level operations (auth, billing, admin) to use the
/metadata endpoint instead of /graphql.

This cleans up the endpoint separation so /graphql is purely for core
objects (Company, People, etc.) and /metadata handles all system
operations.

Part of prep work for webhook/API key core migration.
2025-07-01 18:29:32 +02:00

73 lines
2.5 KiB
TypeScript

import { useApolloClient, useMutation } from '@apollo/client';
import { useCallback } from 'react';
import { SYNC_REMOTE_TABLE_SCHEMA_CHANGES } from '@/databases/graphql/mutations/syncRemoteTableSchemaChanges';
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRemoteTableFromCache';
import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient';
import { useFindManyObjectMetadataItems } from '@/object-metadata/hooks/useFindManyObjectMetadataItems';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import {
RemoteTableInput,
SyncRemoteTableSchemaChangesMutation,
SyncRemoteTableSchemaChangesMutationVariables,
} from '~/generated-metadata/graphql';
import { isDefined } from 'twenty-shared/utils';
export const useSyncRemoteTableSchemaChanges = () => {
const apolloMetadataClient = useApolloCoreClient();
const apolloClient = useApolloClient();
const { refetch: refetchObjectMetadataItems } =
useFindManyObjectMetadataItems();
const { findManyRecordsQuery: findManyViewsQuery } = useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.View,
});
const [mutate, mutationInformation] = useMutation<
SyncRemoteTableSchemaChangesMutation,
SyncRemoteTableSchemaChangesMutationVariables
>(SYNC_REMOTE_TABLE_SCHEMA_CHANGES, {
client: apolloMetadataClient,
});
const syncRemoteTableSchemaChanges = useCallback(
async (input: RemoteTableInput) => {
const remoteTable = await mutate({
variables: {
input,
},
update: (cache, { data }) => {
if (isDefined(data)) {
modifyRemoteTableFromCache({
cache: cache,
remoteTableName: input.name,
fieldModifiers: {
schemaPendingUpdates: () =>
data.syncRemoteTableSchemaChanges.schemaPendingUpdates || [],
status: () => data.syncRemoteTableSchemaChanges.status,
},
});
}
},
});
await refetchObjectMetadataItems();
await apolloClient.query({
query: findManyViewsQuery,
fetchPolicy: 'network-only',
});
return remoteTable;
},
[mutate, refetchObjectMetadataItems, findManyViewsQuery, apolloClient],
);
return {
syncRemoteTableSchemaChanges,
isLoading: mutationInformation.loading,
};
};