Skip to content

Commit fe5b558

Browse files
authored
[FE] Update remote table schema + refactor Tables list (#5548)
Closes #5062. Refactoring tables list to avoid rendering all toggles on each sync or schema update while using fresh data: - introducing id for RemoteTables in apollo cache - manually updating the cache for the record that was updated after a sync or schema update instead of fetching all tables again
1 parent 0d6fe7b commit fe5b558

File tree

13 files changed

+222
-57
lines changed

13 files changed

+222
-57
lines changed

packages/twenty-front/src/generated-metadata/gql.ts

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const documents = {
1818
"\n \n mutation createServer($input: CreateRemoteServerInput!) {\n createOneRemoteServer(input: $input) {\n ...RemoteServerFields\n }\n }\n": types.CreateServerDocument,
1919
"\n mutation deleteServer($input: RemoteServerIdInput!) {\n deleteOneRemoteServer(input: $input) {\n id\n }\n }\n": types.DeleteServerDocument,
2020
"\n \n mutation syncRemoteTable($input: RemoteTableInput!) {\n syncRemoteTable(input: $input) {\n ...RemoteTableFields\n }\n }\n": types.SyncRemoteTableDocument,
21+
"\n \n mutation syncRemoteTableSchemaChanges($input: RemoteTableInput!) {\n syncRemoteTableSchemaChanges(input: $input) {\n ...RemoteTableFields\n }\n }\n": types.SyncRemoteTableSchemaChangesDocument,
2122
"\n \n mutation unsyncRemoteTable($input: RemoteTableInput!) {\n unsyncRemoteTable(input: $input) {\n ...RemoteTableFields\n }\n }\n": types.UnsyncRemoteTableDocument,
2223
"\n \n mutation updateServer($input: UpdateRemoteServerInput!) {\n updateOneRemoteServer(input: $input) {\n ...RemoteServerFields\n }\n }\n": types.UpdateServerDocument,
2324
"\n \n query GetManyDatabaseConnections($input: RemoteServerTypeInput!) {\n findManyRemoteServersByType(input: $input) {\n ...RemoteServerFields\n }\n }\n": types.GetManyDatabaseConnectionsDocument,
@@ -68,6 +69,10 @@ export function graphql(source: "\n mutation deleteServer($input: RemoteServerI
6869
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
6970
*/
7071
export function graphql(source: "\n \n mutation syncRemoteTable($input: RemoteTableInput!) {\n syncRemoteTable(input: $input) {\n ...RemoteTableFields\n }\n }\n"): (typeof documents)["\n \n mutation syncRemoteTable($input: RemoteTableInput!) {\n syncRemoteTable(input: $input) {\n ...RemoteTableFields\n }\n }\n"];
72+
/**
73+
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
74+
*/
75+
export function graphql(source: "\n \n mutation syncRemoteTableSchemaChanges($input: RemoteTableInput!) {\n syncRemoteTableSchemaChanges(input: $input) {\n ...RemoteTableFields\n }\n }\n"): (typeof documents)["\n \n mutation syncRemoteTableSchemaChanges($input: RemoteTableInput!) {\n syncRemoteTableSchemaChanges(input: $input) {\n ...RemoteTableFields\n }\n }\n"];
7176
/**
7277
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
7378
*/

packages/twenty-front/src/generated-metadata/graphql.ts

+20
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ export type LoginToken = {
368368
export type Mutation = {
369369
__typename?: 'Mutation';
370370
activateWorkspace: Workspace;
371+
addUserToWorkspace: User;
371372
authorizeApp: AuthorizeApp;
372373
challenge: LoginToken;
373374
checkoutSession: SessionEntity;
@@ -391,6 +392,7 @@ export type Mutation = {
391392
renewToken: AuthTokens;
392393
signUp: LoginToken;
393394
syncRemoteTable: RemoteTable;
395+
syncRemoteTableSchemaChanges: RemoteTable;
394396
track: Analytics;
395397
unsyncRemoteTable: RemoteTable;
396398
updateBillingSubscription: UpdateBillingEntity;
@@ -412,6 +414,11 @@ export type MutationActivateWorkspaceArgs = {
412414
};
413415

414416

417+
export type MutationAddUserToWorkspaceArgs = {
418+
inviteHash: Scalars['String']['input'];
419+
};
420+
421+
415422
export type MutationAuthorizeAppArgs = {
416423
clientId: Scalars['String']['input'];
417424
codeChallenge?: InputMaybe<Scalars['String']['input']>;
@@ -523,6 +530,11 @@ export type MutationSyncRemoteTableArgs = {
523530
};
524531

525532

533+
export type MutationSyncRemoteTableSchemaChangesArgs = {
534+
input: RemoteTableInput;
535+
};
536+
537+
526538
export type MutationTrackArgs = {
527539
data: Scalars['JSON']['input'];
528540
type: Scalars['String']['input'];
@@ -1284,6 +1296,13 @@ export type SyncRemoteTableMutationVariables = Exact<{
12841296

12851297
export type SyncRemoteTableMutation = { __typename?: 'Mutation', syncRemoteTable: { __typename?: 'RemoteTable', id?: any | null, name: string, schema?: string | null, status: RemoteTableStatus, schemaPendingUpdates?: Array<DistantTableUpdate> | null } };
12861298

1299+
export type SyncRemoteTableSchemaChangesMutationVariables = Exact<{
1300+
input: RemoteTableInput;
1301+
}>;
1302+
1303+
1304+
export type SyncRemoteTableSchemaChangesMutation = { __typename?: 'Mutation', syncRemoteTableSchemaChanges: { __typename?: 'RemoteTable', id?: any | null, name: string, schema?: string | null, status: RemoteTableStatus, schemaPendingUpdates?: Array<DistantTableUpdate> | null } };
1305+
12871306
export type UnsyncRemoteTableMutationVariables = Exact<{
12881307
input: RemoteTableInput;
12891308
}>;
@@ -1390,6 +1409,7 @@ export const RemoteTableFieldsFragmentDoc = {"kind":"Document","definitions":[{"
13901409
export const CreateServerDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"createServer"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"CreateRemoteServerInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createOneRemoteServer"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"RemoteServerFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RemoteServerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteServer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperId"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperOptions"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperType"}},{"kind":"Field","name":{"kind":"Name","value":"userMappingOptions"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"user"}}]}},{"kind":"Field","name":{"kind":"Name","value":"updatedAt"}},{"kind":"Field","name":{"kind":"Name","value":"schema"}}]}}]} as unknown as DocumentNode<CreateServerMutation, CreateServerMutationVariables>;
13911410
export const DeleteServerDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"deleteServer"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteServerIdInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"deleteOneRemoteServer"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}}]} as unknown as DocumentNode<DeleteServerMutation, DeleteServerMutationVariables>;
13921411
export const SyncRemoteTableDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"syncRemoteTable"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteTableInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"syncRemoteTable"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"RemoteTableFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RemoteTableFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteTable"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"schema"}},{"kind":"Field","name":{"kind":"Name","value":"status"}},{"kind":"Field","name":{"kind":"Name","value":"schemaPendingUpdates"}}]}}]} as unknown as DocumentNode<SyncRemoteTableMutation, SyncRemoteTableMutationVariables>;
1412+
export const SyncRemoteTableSchemaChangesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"syncRemoteTableSchemaChanges"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteTableInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"syncRemoteTableSchemaChanges"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"RemoteTableFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RemoteTableFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteTable"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"schema"}},{"kind":"Field","name":{"kind":"Name","value":"status"}},{"kind":"Field","name":{"kind":"Name","value":"schemaPendingUpdates"}}]}}]} as unknown as DocumentNode<SyncRemoteTableSchemaChangesMutation, SyncRemoteTableSchemaChangesMutationVariables>;
13931413
export const UnsyncRemoteTableDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"unsyncRemoteTable"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteTableInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"unsyncRemoteTable"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"RemoteTableFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RemoteTableFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteTable"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"schema"}},{"kind":"Field","name":{"kind":"Name","value":"status"}},{"kind":"Field","name":{"kind":"Name","value":"schemaPendingUpdates"}}]}}]} as unknown as DocumentNode<UnsyncRemoteTableMutation, UnsyncRemoteTableMutationVariables>;
13941414
export const UpdateServerDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"updateServer"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UpdateRemoteServerInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateOneRemoteServer"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"RemoteServerFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RemoteServerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteServer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperId"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperOptions"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperType"}},{"kind":"Field","name":{"kind":"Name","value":"userMappingOptions"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"user"}}]}},{"kind":"Field","name":{"kind":"Name","value":"updatedAt"}},{"kind":"Field","name":{"kind":"Name","value":"schema"}}]}}]} as unknown as DocumentNode<UpdateServerMutation, UpdateServerMutationVariables>;
13951415
export const GetManyDatabaseConnectionsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetManyDatabaseConnections"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteServerTypeInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"findManyRemoteServersByType"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"RemoteServerFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RemoteServerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"RemoteServer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperId"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperOptions"}},{"kind":"Field","name":{"kind":"Name","value":"foreignDataWrapperType"}},{"kind":"Field","name":{"kind":"Name","value":"userMappingOptions"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"user"}}]}},{"kind":"Field","name":{"kind":"Name","value":"updatedAt"}},{"kind":"Field","name":{"kind":"Name","value":"schema"}}]}}]} as unknown as DocumentNode<GetManyDatabaseConnectionsQuery, GetManyDatabaseConnectionsQueryVariables>;

packages/twenty-front/src/modules/apollo/hooks/useApolloFactory.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ export const useApolloFactory = (options: Partial<Options<any>> = {}) => {
3030
const apolloClient = useMemo(() => {
3131
apolloRef.current = new ApolloFactory({
3232
uri: `${REACT_APP_SERVER_BASE_URL}/graphql`,
33-
cache: new InMemoryCache(),
33+
cache: new InMemoryCache({
34+
typePolicies: {
35+
RemoteTable: {
36+
keyFields: ['name'],
37+
},
38+
},
39+
}),
3440
headers: {
3541
...(currentWorkspace?.currentCacheVersion && {
3642
'X-Schema-Version': currentWorkspace.currentCacheVersion,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { gql } from '@apollo/client';
2+
3+
import { REMOTE_TABLE_FRAGMENT } from '@/databases/graphql/fragments/remoteTableFragment';
4+
5+
export const SYNC_REMOTE_TABLE_SCHEMA_CHANGES = gql`
6+
${REMOTE_TABLE_FRAGMENT}
7+
mutation syncRemoteTableSchemaChanges($input: RemoteTableInput!) {
8+
syncRemoteTableSchemaChanges(input: $input) {
9+
...RemoteTableFields
10+
}
11+
}
12+
`;

packages/twenty-front/src/modules/databases/hooks/useSyncRemoteTable.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useCallback } from 'react';
22
import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';
33

44
import { SYNC_REMOTE_TABLE } from '@/databases/graphql/mutations/syncRemoteTable';
5-
import { GET_MANY_REMOTE_TABLES } from '@/databases/graphql/queries/findManyRemoteTables';
5+
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRemoteTableFromCache';
66
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
77
import { useFindManyObjectMetadataItems } from '@/object-metadata/hooks/useFindManyObjectMetadataItems';
88
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
@@ -12,6 +12,7 @@ import {
1212
SyncRemoteTableMutation,
1313
SyncRemoteTableMutationVariables,
1414
} from '~/generated-metadata/graphql';
15+
import { isDefined } from '~/utils/isDefined';
1516

1617
export const useSyncRemoteTable = () => {
1718
const apolloMetadataClient = useApolloMetadataClient();
@@ -23,7 +24,6 @@ export const useSyncRemoteTable = () => {
2324
const { findManyRecordsQuery: findManyViewsQuery } = useFindManyRecordsQuery({
2425
objectNameSingular: CoreObjectNameSingular.View,
2526
});
26-
2727
const [mutate] = useMutation<
2828
SyncRemoteTableMutation,
2929
SyncRemoteTableMutationVariables
@@ -37,20 +37,19 @@ export const useSyncRemoteTable = () => {
3737
variables: {
3838
input,
3939
},
40-
awaitRefetchQueries: true,
41-
refetchQueries: [
42-
{
43-
query: GET_MANY_REMOTE_TABLES,
44-
variables: {
45-
input: {
46-
id: input.remoteServerId,
40+
update: (cache, { data }) => {
41+
if (isDefined(data)) {
42+
modifyRemoteTableFromCache({
43+
cache: cache,
44+
remoteTableName: input.name,
45+
fieldModifiers: {
46+
status: () => data.syncRemoteTable.status,
4747
},
48-
},
49-
},
50-
],
48+
});
49+
}
50+
},
5151
});
5252

53-
// TODO: we should return the tables with the columns and store in cache instead of refetching
5453
await refetchObjectMetadataItems();
5554
await apolloClient.query({
5655
query: findManyViewsQuery,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { useCallback } from 'react';
2+
import { ApolloClient, useMutation } from '@apollo/client';
3+
4+
import { SYNC_REMOTE_TABLE_SCHEMA_CHANGES } from '@/databases/graphql/mutations/syncRemoteTableSchemaChanges';
5+
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRemoteTableFromCache';
6+
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
7+
import {
8+
RemoteTableInput,
9+
SyncRemoteTableSchemaChangesMutation,
10+
SyncRemoteTableSchemaChangesMutationVariables,
11+
} from '~/generated-metadata/graphql';
12+
import { isDefined } from '~/utils/isDefined';
13+
14+
export const useSyncRemoteTableSchemaChanges = () => {
15+
const apolloMetadataClient = useApolloMetadataClient();
16+
17+
const [mutate, mutationInformation] = useMutation<
18+
SyncRemoteTableSchemaChangesMutation,
19+
SyncRemoteTableSchemaChangesMutationVariables
20+
>(SYNC_REMOTE_TABLE_SCHEMA_CHANGES, {
21+
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
22+
});
23+
24+
const syncRemoteTableSchemaChanges = useCallback(
25+
async (input: RemoteTableInput) => {
26+
const remoteTable = await mutate({
27+
variables: {
28+
input,
29+
},
30+
update: (cache, { data }) => {
31+
if (isDefined(data)) {
32+
modifyRemoteTableFromCache({
33+
cache: cache,
34+
remoteTableName: input.name,
35+
fieldModifiers: {
36+
schemaPendingUpdates: () =>
37+
data.syncRemoteTableSchemaChanges.schemaPendingUpdates || [],
38+
},
39+
});
40+
}
41+
},
42+
});
43+
44+
return remoteTable;
45+
},
46+
[mutate],
47+
);
48+
49+
return {
50+
syncRemoteTableSchemaChanges,
51+
isLoading: mutationInformation.loading,
52+
};
53+
};

0 commit comments

Comments
 (0)