Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize metadata queries #7013

Merged
merged 17 commits into from
Sep 13, 2024
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { RelationType } from '@/settings/data-model/types/RelationType';
import {
FieldMetadataType,
RelationMetadataType,
} from '~/generated-metadata/graphql';
import { FieldMetadataType } from '~/generated-metadata/graphql';

export const getRelationDefinition = ({
objectMetadataItems,
Expand All @@ -17,46 +13,25 @@ export const getRelationDefinition = ({
return null;
}

const relationMetadataItem =
fieldMetadataItemOnSourceRecord.fromRelationMetadata ||
fieldMetadataItemOnSourceRecord.toRelationMetadata;
const relationDefinition = fieldMetadataItemOnSourceRecord.relationDefinition;

if (!relationMetadataItem) return null;

const relationSourceFieldMetadataItemId =
'toFieldMetadataId' in relationMetadataItem
? relationMetadataItem.toFieldMetadataId
: relationMetadataItem.fromFieldMetadataId;

if (!relationSourceFieldMetadataItemId) return null;

// TODO: precise naming, is it relationTypeFromTargetPointOfView or relationTypeFromSourcePointOfView ?
const relationType =
relationMetadataItem.relationType === RelationMetadataType.OneToMany &&
fieldMetadataItemOnSourceRecord.toRelationMetadata
? ('MANY_TO_ONE' satisfies RelationType)
: (relationMetadataItem.relationType as RelationType);

const targetObjectMetadataNameSingular =
'toObjectMetadata' in relationMetadataItem
? relationMetadataItem.toObjectMetadata.nameSingular
: relationMetadataItem.fromObjectMetadata.nameSingular;
if (!relationDefinition) return null;

const targetObjectMetadataItem = objectMetadataItems.find(
(item) => item.nameSingular === targetObjectMetadataNameSingular,
(item) => item.id === relationDefinition.targetObjectMetadata.id,
);

if (!targetObjectMetadataItem) return null;

const fieldMetadataItemOnTargetRecord = targetObjectMetadataItem.fields.find(
(field) => field.id === relationSourceFieldMetadataItemId,
(field) => field.id === relationDefinition.sourceFieldMetadata.id,
);

if (!fieldMetadataItemOnTargetRecord) return null;

return {
fieldMetadataItemOnTargetRecord,
targetObjectMetadataItem,
relationType,
relationType: relationDefinition.direction,
};
};
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 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ReactNode } from 'react';
import { MockedProvider } from '@apollo/client/testing';
import { act, renderHook } from '@testing-library/react';
import { ReactNode } from 'react';
import { RecoilRoot } from 'recoil';

import { useCreateOneRelationMetadataItem } from '@/object-metadata/hooks/useCreateOneRelationMetadataItem';
import { RelationMetadataType } from '~/generated/graphql';
import { RelationDefinitionType } from '~/generated/graphql';

import {
query,
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('useCreateOneRelationMetadataItem', () => {

await act(async () => {
const res = await result.current.createOneRelationMetadataItem({
relationType: RelationMetadataType.OneToOne,
relationType: RelationDefinitionType.OneToOne,
field: {
label: 'label',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { useRecoilCallback } from 'recoil';

import { objectMetadataItemFamilySelector } from '@/object-metadata/states/objectMetadataItemFamilySelector';
import { RelationType } from '@/settings/data-model/types/RelationType';
import {
FieldMetadataType,
RelationMetadataType,
} from '~/generated-metadata/graphql';
import { FieldMetadataType } from '~/generated-metadata/graphql';

import { FieldMetadataItem } from '../types/FieldMetadataItem';

Expand All @@ -17,39 +13,19 @@ export const useGetRelationMetadata = () =>
}: {
fieldMetadataItem: Pick<
FieldMetadataItem,
'fromRelationMetadata' | 'toRelationMetadata' | 'type'
'type' | 'relationDefinition'
>;
}) => {
if (fieldMetadataItem.type !== FieldMetadataType.Relation) return null;

const relationMetadata =
fieldMetadataItem.fromRelationMetadata ||
fieldMetadataItem.toRelationMetadata;
const relationDefinition = fieldMetadataItem.relationDefinition;

if (!relationMetadata) return null;

const relationFieldMetadataId =
'toFieldMetadataId' in relationMetadata
? relationMetadata.toFieldMetadataId
: relationMetadata.fromFieldMetadataId;

if (!relationFieldMetadataId) return null;

const relationType =
relationMetadata.relationType === RelationMetadataType.OneToMany &&
fieldMetadataItem.toRelationMetadata
? 'MANY_TO_ONE'
: (relationMetadata.relationType as RelationType);

const relationObjectMetadataNameSingular =
'toObjectMetadata' in relationMetadata
? relationMetadata.toObjectMetadata.nameSingular
: relationMetadata.fromObjectMetadata.nameSingular;
if (!relationDefinition) return null;

const relationObjectMetadataItem = snapshot
.getLoadable(
objectMetadataItemFamilySelector({
objectName: relationObjectMetadataNameSingular,
objectName: relationDefinition.targetObjectMetadata.nameSingular,
objectNameType: 'singular',
}),
)
Expand All @@ -59,15 +35,15 @@ export const useGetRelationMetadata = () =>

const relationFieldMetadataItem =
relationObjectMetadataItem.fields.find(
(field) => field.id === relationFieldMetadataId,
(field) => field.id === relationDefinition.targetFieldMetadata.id,
);

if (!relationFieldMetadataItem) return null;

return {
relationFieldMetadataItem,
relationObjectMetadataItem,
relationType,
relationType: relationDefinition.direction,
};
},
[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ThemeColor } from 'twenty-ui';
import {
Field,
Object as MetadataObject,
Relation,
RelationDefinition,
RelationDefinitionType,
} from '~/generated-metadata/graphql';
Expand All @@ -18,31 +17,9 @@ export type FieldMetadataItemOption = {

export type FieldMetadataItem = Omit<
Field,
| '__typename'
| 'fromRelationMetadata'
| 'toRelationMetadata'
| 'defaultValue'
| 'options'
| 'settings'
| 'relationDefinition'
'__typename' | 'defaultValue' | 'options' | 'settings' | 'relationDefinition'
> & {
__typename?: string;
fromRelationMetadata?:
| (Pick<Relation, 'id' | 'toFieldMetadataId' | 'relationType'> & {
toObjectMetadata: Pick<
Relation['toObjectMetadata'],
'id' | 'nameSingular' | 'namePlural' | 'isSystem' | 'isRemote'
>;
})
| null;
toRelationMetadata?:
| (Pick<Relation, 'id' | 'fromFieldMetadataId' | 'relationType'> & {
fromObjectMetadata: Pick<
Relation['fromObjectMetadata'],
'id' | 'nameSingular' | 'namePlural' | 'isSystem' | 'isRemote'
>;
})
| null;
defaultValue?: any;
options?: FieldMetadataItemOption[] | null;
relationDefinition?: {
Expand Down
Loading
Loading