Skip to content

Commit

Permalink
Optimize metadata queries (#7013)
Browse files Browse the repository at this point in the history
In this PR:

1. Refactor guards to avoid duplicated queries: WorkspaceAuthGuard and
UserAuthGuard only check for existence of workspace and user in the
request without querying the database
  • Loading branch information
charlesBochet committed Sep 13, 2024
1 parent cf8b116 commit 523df53
Show file tree
Hide file tree
Showing 132 changed files with 817 additions and 6,371 deletions.
4 changes: 2 additions & 2 deletions packages/twenty-front/src/generated-metadata/gql.ts

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions packages/twenty-front/src/generated-metadata/graphql.ts

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Apollo from '@apollo/client';
import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
Expand Down Expand Up @@ -219,7 +219,7 @@ export type ExecuteServerlessFunctionInput = {
/** Id of the serverless function to execute */
id: Scalars['UUID'];
/** Payload in JSON format */
payload?: InputMaybe<Scalars['JSON']>;
payload: Scalars['JSON'];
/** Version of the serverless function to execute */
version?: Scalars['String'];
};
Expand Down Expand Up @@ -338,6 +338,7 @@ export enum MessageChannelVisibility {

export type Mutation = {
__typename?: 'Mutation';
activateWorkflowVersion: Scalars['Boolean'];
activateWorkspace: Workspace;
addUserToWorkspace: User;
authorizeApp: AuthorizeApp;
Expand All @@ -347,15 +348,14 @@ export type Mutation = {
createOneObject: Object;
createOneServerlessFunction: ServerlessFunction;
createOneServerlessFunctionFromFile: ServerlessFunction;
deactivateWorkflowVersion: Scalars['Boolean'];
deleteCurrentWorkspace: Workspace;
deleteOneObject: Object;
deleteOneServerlessFunction: ServerlessFunction;
deleteUser: User;
disablePostgresProxy: PostgresCredentials;
disableWorkflowTrigger: Scalars['Boolean'];
emailPasswordResetLink: EmailPasswordResetLink;
enablePostgresProxy: PostgresCredentials;
enableWorkflowTrigger: Scalars['Boolean'];
exchangeAuthorizationCode: ExchangeAuthCode;
executeOneServerlessFunction: ServerlessFunctionExecutionResult;
generateApiKeyToken: ApiKeyToken;
Expand All @@ -382,6 +382,11 @@ export type Mutation = {
};


export type MutationActivateWorkflowVersionArgs = {
workflowVersionId: Scalars['String'];
};


export type MutationActivateWorkspaceArgs = {
data: ActivateWorkspaceInput;
};
Expand Down Expand Up @@ -423,6 +428,11 @@ export type MutationCreateOneServerlessFunctionFromFileArgs = {
};


export type MutationDeactivateWorkflowVersionArgs = {
workflowVersionId: Scalars['String'];
};


export type MutationDeleteOneObjectArgs = {
input: DeleteOneObjectInput;
};
Expand All @@ -433,21 +443,11 @@ export type MutationDeleteOneServerlessFunctionArgs = {
};


export type MutationDisableWorkflowTriggerArgs = {
workflowVersionId: Scalars['String'];
};


export type MutationEmailPasswordResetLinkArgs = {
email: Scalars['String'];
};


export type MutationEnableWorkflowTriggerArgs = {
workflowVersionId: Scalars['String'];
};


export type MutationExchangeAuthorizationCodeArgs = {
authorizationCode: Scalars['String'];
clientSecret?: InputMaybe<Scalars['String']>;
Expand Down Expand Up @@ -638,9 +638,10 @@ export type Query = {
currentWorkspace: Workspace;
findWorkspaceFromInviteHash: Workspace;
getAISQLQuery: AisqlQueryResult;
getAvailablePackages: Scalars['JSON'];
getPostgresCredentials?: Maybe<PostgresCredentials>;
getProductPrices: ProductPricesEntity;
getServerlessFunctionSourceCode: Scalars['String'];
getServerlessFunctionSourceCode?: Maybe<Scalars['String']>;
getTimelineCalendarEventsFromCompanyId: TimelineCalendarEventsWithTotal;
getTimelineCalendarEventsFromPersonId: TimelineCalendarEventsWithTotal;
getTimelineThreadsFromCompanyId: TimelineThreadsWithTotal;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ApolloCache } from '@apollo/client';

import { getRelationDefinition } from '@/apollo/optimistic-effect/utils/getRelationDefinition';
import { triggerAttachRelationOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerAttachRelationOptimisticEffect';
import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect';
import { triggerDetachRelationOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDetachRelationOptimisticEffect';
Expand Down Expand Up @@ -45,16 +44,23 @@ export const triggerUpdateRelationsOptimisticEffect = ({
return;
}

const relationDefinition = getRelationDefinition({
fieldMetadataItemOnSourceRecord,
objectMetadataItems,
});
const relationDefinition =
fieldMetadataItemOnSourceRecord.relationDefinition;

if (!relationDefinition) {
return;
}

const { targetObjectMetadataItem, fieldMetadataItemOnTargetRecord } =
relationDefinition;
const { targetObjectMetadata, targetFieldMetadata } = relationDefinition;

const fullTargetObjectMetadataItem = objectMetadataItems.find(
({ nameSingular }) =>
nameSingular === targetObjectMetadata.nameSingular,
);

if (!fullTargetObjectMetadataItem) {
return;
}

const currentFieldValueOnSourceRecord:
| RecordGqlConnection
Expand All @@ -80,7 +86,7 @@ export const triggerUpdateRelationsOptimisticEffect = ({
// it's an object record connection (we can still check it though as a safeguard)
const currentFieldValueOnSourceRecordIsARecordConnection =
isObjectRecordConnection(
targetObjectMetadataItem.nameSingular,
targetObjectMetadata.nameSingular,
currentFieldValueOnSourceRecord,
);

Expand All @@ -93,7 +99,7 @@ export const triggerUpdateRelationsOptimisticEffect = ({

const updatedFieldValueOnSourceRecordIsARecordConnection =
isObjectRecordConnection(
targetObjectMetadataItem.nameSingular,
targetObjectMetadata.nameSingular,
updatedFieldValueOnSourceRecord,
);

Expand All @@ -112,13 +118,13 @@ export const triggerUpdateRelationsOptimisticEffect = ({
// Instead of hardcoding it here
const shouldCascadeDeleteTargetRecords =
CORE_OBJECT_NAMES_TO_DELETE_ON_TRIGGER_RELATION_DETACH.includes(
targetObjectMetadataItem.nameSingular as CoreObjectNameSingular,
targetObjectMetadata.nameSingular as CoreObjectNameSingular,
);

if (shouldCascadeDeleteTargetRecords) {
triggerDeleteRecordsOptimisticEffect({
cache,
objectMetadataItem: targetObjectMetadataItem,
objectMetadataItem: fullTargetObjectMetadataItem,
recordsToDelete: targetRecordsToDetachFrom,
objectMetadataItems,
});
Expand All @@ -128,8 +134,8 @@ export const triggerUpdateRelationsOptimisticEffect = ({
cache,
sourceObjectNameSingular: sourceObjectMetadataItem.nameSingular,
sourceRecordId: currentSourceRecord.id,
fieldNameOnTargetRecord: fieldMetadataItemOnTargetRecord.name,
targetObjectNameSingular: targetObjectMetadataItem.nameSingular,
fieldNameOnTargetRecord: targetFieldMetadata.name,
targetObjectNameSingular: targetObjectMetadata.nameSingular,
targetRecordId: targetRecordToDetachFrom.id,
});
});
Expand All @@ -145,8 +151,8 @@ export const triggerUpdateRelationsOptimisticEffect = ({
cache,
sourceObjectNameSingular: sourceObjectMetadataItem.nameSingular,
sourceRecordId: updatedSourceRecord.id,
fieldNameOnTargetRecord: fieldMetadataItemOnTargetRecord.name,
targetObjectNameSingular: targetObjectMetadataItem.nameSingular,
fieldNameOnTargetRecord: targetFieldMetadata.name,
targetObjectNameSingular: targetObjectMetadata.nameSingular,
targetRecordId: targetRecordToAttachTo.id,
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { NavigationDrawerSection } from '@/ui/navigation/navigation-drawer/compo
import { NavigationDrawerSectionTitle } from '@/ui/navigation/navigation-drawer/components/NavigationDrawerSectionTitle';
import { useNavigationSection } from '@/ui/navigation/navigation-drawer/hooks/useNavigationSection';

import { currentUserState } from '@/auth/states/currentUserState';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { useFavorites } from '../hooks/useFavorites';

const StyledContainer = styled(NavigationDrawerSection)`
Expand All @@ -35,7 +35,7 @@ const StyledNavigationDrawerItem = styled(NavigationDrawerItem)`
`;

export const CurrentWorkspaceMemberFavorites = () => {
const currentUser = useRecoilValue(currentUserState);
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);

const { favorites, handleReorderFavorite } = useFavorites();
const loading = useIsPrefetchLoading();
Expand All @@ -44,12 +44,12 @@ export const CurrentWorkspaceMemberFavorites = () => {
useNavigationSection('Favorites');
const isNavigationSectionOpen = useRecoilValue(isNavigationSectionOpenState);

if (loading && isDefined(currentUser)) {
if (loading && isDefined(currentWorkspaceMember)) {
return <FavoritesSkeletonLoader />;
}

const currentWorkspaceMemberFavorites = favorites.filter(
(favorite) => favorite.workspaceMemberId === currentUser?.id,
(favorite) => favorite.workspaceMemberId === currentWorkspaceMember?.id,
);

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export type Favorite = {
avatarType: AvatarType;
link: string;
recordId: string;
workspaceMemberId: string;
__typename: 'Favorite';
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const sortFavorites = (
const relationObject = favorite[relationField.name];

const relationObjectNameSingular =
relationField.toRelationMetadata?.fromObjectMetadata.nameSingular ??
'';
relationField.relationDefinition?.targetObjectMetadata
.nameSingular ?? '';

const objectRecordIdentifier =
getObjectRecordIdentifierByNameSingular(
Expand All @@ -38,6 +38,7 @@ export const sortFavorites = (
link: hasLinkToShowPage
? objectRecordIdentifier.linkToShowPage
: '',
workspaceMemberId: favorite.workspaceMemberId,
} as Favorite;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,6 @@ export const FIND_MANY_OBJECT_METADATA_ITEMS = gql`
isNullable
createdAt
updatedAt
fromRelationMetadata {
id
relationType
toObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
isRemote
}
toFieldMetadataId
}
toRelationMetadata {
id
relationType
fromObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
isRemote
}
fromFieldMetadataId
}
defaultValue
options
relationDefinition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,27 @@ export const query = gql`
isNullable
createdAt
updatedAt
fromRelationMetadata {
id
relationType
toObjectMetadata {
relationDefinition {
relationId
direction
sourceObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
}
toFieldMetadataId
}
toRelationMetadata {
id
relationType
fromObjectMetadata {
sourceFieldMetadata {
id
name
}
targetObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
}
fromFieldMetadataId
targetFieldMetadata {
id
name
}
}
defaultValue
options
Expand Down
Loading

0 comments on commit 523df53

Please sign in to comment.