feat: new relation sync-metadata, twenty-orm, create/update (#10217)
Fix https://github.com/twentyhq/core-team-issues/issues/330#issue-2827026606 and https://github.com/twentyhq/core-team-issues/issues/327#issue-2827001814 What this PR does when `isNewRelationEnabled` is set to `true`: - [x] Drop the creation of the foreign key as a `FieldMetadata` - [x] Stop creating `RelationMetadata` - [x] Properly fill `FieldMetadata` of type `RELATION` during the sync command - [x] Use new relation settings in TwentyORM - [x] Properly create `FieldMetadata` relations when we create a new object - [x] Handle `database:reset` with new relations --------- Co-authored-by: Charles Bochet <charles@twenty.com> Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
This commit is contained in:
@ -15,7 +15,7 @@ import { RelationTypeV2Factory } from 'src/engine/api/graphql/workspace-schema-b
|
||||
import { TypeDefinitionsStorage } from 'src/engine/api/graphql/workspace-schema-builder/storages/type-definitions.storage';
|
||||
import { getResolverArgs } from 'src/engine/api/graphql/workspace-schema-builder/utils/get-resolver-args.util';
|
||||
import { objectContainsRelationField } from 'src/engine/api/graphql/workspace-schema-builder/utils/object-contains-relation-field';
|
||||
import { isFieldMetadataOfType } from 'src/engine/utils/is-field-metadata-of-type.util';
|
||||
import { isFieldMetadataInterfaceOfType } from 'src/engine/utils/is-field-metadata-of-type.util';
|
||||
|
||||
import { ArgsFactory } from './args.factory';
|
||||
|
||||
@ -108,7 +108,12 @@ export class ExtendObjectTypeDefinitionV2Factory {
|
||||
|
||||
for (const fieldMetadata of objectMetadata.fields) {
|
||||
// Ignore non-relation fields as they are already defined
|
||||
if (!isFieldMetadataOfType(fieldMetadata, FieldMetadataType.RELATION)) {
|
||||
if (
|
||||
!isFieldMetadataInterfaceOfType(
|
||||
fieldMetadata,
|
||||
FieldMetadataType.RELATION,
|
||||
)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@ -36,6 +36,7 @@ export class InputTypeDefinitionFactory {
|
||||
objectMetadata: ObjectMetadataInterface,
|
||||
kind: InputTypeDefinitionKind,
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
isNewRelationEnabled = false,
|
||||
): InputTypeDefinition {
|
||||
const inputType = new GraphQLInputObjectType({
|
||||
name: `${pascalCase(objectMetadata.nameSingular)}${kind.toString()}Input`,
|
||||
@ -58,6 +59,7 @@ export class InputTypeDefinitionFactory {
|
||||
kind,
|
||||
options,
|
||||
this.inputTypeFactory,
|
||||
isNewRelationEnabled,
|
||||
),
|
||||
and: {
|
||||
type: andOrType,
|
||||
@ -81,6 +83,7 @@ export class InputTypeDefinitionFactory {
|
||||
kind,
|
||||
options,
|
||||
this.inputTypeFactory,
|
||||
isNewRelationEnabled,
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
@ -30,6 +30,7 @@ export class ObjectTypeDefinitionFactory {
|
||||
objectMetadata: ObjectMetadataInterface,
|
||||
kind: ObjectTypeDefinitionKind,
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
isNewRelationEnabled = false,
|
||||
): ObjectTypeDefinition {
|
||||
return {
|
||||
target: objectMetadata.id,
|
||||
@ -42,6 +43,7 @@ export class ObjectTypeDefinitionFactory {
|
||||
kind,
|
||||
options,
|
||||
this.outputTypeFactory,
|
||||
isNewRelationEnabled,
|
||||
),
|
||||
}),
|
||||
};
|
||||
|
||||
@ -58,7 +58,7 @@ export class TypeMapperService {
|
||||
settings?: FieldMetadataSettings<FieldMetadataType>,
|
||||
isIdField?: boolean,
|
||||
): GraphQLScalarType | undefined {
|
||||
if (isIdField || settings?.isForeignKey) {
|
||||
if (isIdField || fieldMetadataType === FieldMetadataType.RELATION) {
|
||||
return GraphQLID;
|
||||
}
|
||||
const typeScalarMapping = new Map<FieldMetadataType, GraphQLScalarType>([
|
||||
@ -93,7 +93,7 @@ export class TypeMapperService {
|
||||
settings?: FieldMetadataSettings<FieldMetadataType>,
|
||||
isIdField?: boolean,
|
||||
): GraphQLInputObjectType | GraphQLScalarType | undefined {
|
||||
if (isIdField || settings?.isForeignKey) {
|
||||
if (isIdField || fieldMetadataType === FieldMetadataType.RELATION) {
|
||||
return IDFilterType;
|
||||
}
|
||||
|
||||
@ -132,6 +132,7 @@ export class TypeMapperService {
|
||||
): GraphQLInputType | undefined {
|
||||
const typeOrderByMapping = new Map<FieldMetadataType, GraphQLEnumType>([
|
||||
[FieldMetadataType.UUID, OrderByDirectionType],
|
||||
[FieldMetadataType.RELATION, OrderByDirectionType],
|
||||
[FieldMetadataType.TEXT, OrderByDirectionType],
|
||||
[FieldMetadataType.DATE_TIME, OrderByDirectionType],
|
||||
[FieldMetadataType.DATE, OrderByDirectionType],
|
||||
|
||||
@ -51,11 +51,16 @@ export class TypeDefinitionsGenerator {
|
||||
async generate(
|
||||
objectMetadataCollection: ObjectMetadataInterface[],
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
isNewRelationEnabled: boolean,
|
||||
) {
|
||||
// Generate composite type objects first because they can be used in dynamic objects
|
||||
await this.generateCompositeTypeDefs(options);
|
||||
// Generate metadata objects
|
||||
await this.generateMetadataTypeDefs(objectMetadataCollection, options);
|
||||
await this.generateMetadataTypeDefs(
|
||||
objectMetadataCollection,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -155,6 +160,7 @@ export class TypeDefinitionsGenerator {
|
||||
private async generateMetadataTypeDefs(
|
||||
dynamicObjectMetadataCollection: ObjectMetadataInterface[],
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
isNewRelationEnabled: boolean,
|
||||
) {
|
||||
this.logger.log(
|
||||
`Generating metadata objects: [${dynamicObjectMetadataCollection
|
||||
@ -164,9 +170,17 @@ export class TypeDefinitionsGenerator {
|
||||
|
||||
// Generate dynamic objects
|
||||
this.generateEnumTypeDefs(dynamicObjectMetadataCollection, options);
|
||||
this.generateObjectTypeDefs(dynamicObjectMetadataCollection, options);
|
||||
this.generateObjectTypeDefs(
|
||||
dynamicObjectMetadataCollection,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
);
|
||||
this.generatePaginationTypeDefs(dynamicObjectMetadataCollection, options);
|
||||
this.generateInputTypeDefs(dynamicObjectMetadataCollection, options);
|
||||
this.generateInputTypeDefs(
|
||||
dynamicObjectMetadataCollection,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
);
|
||||
await this.generateExtendedObjectTypeDefs(
|
||||
dynamicObjectMetadataCollection,
|
||||
options,
|
||||
@ -176,12 +190,14 @@ export class TypeDefinitionsGenerator {
|
||||
private generateObjectTypeDefs(
|
||||
objectMetadataCollection: ObjectMetadataInterface[] | CompositeType[],
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
isNewRelationEnabled: boolean,
|
||||
) {
|
||||
const objectTypeDefs = objectMetadataCollection.map((objectMetadata) =>
|
||||
this.objectTypeDefinitionFactory.create(
|
||||
objectMetadata,
|
||||
ObjectTypeDefinitionKind.Plain,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
),
|
||||
);
|
||||
|
||||
@ -209,6 +225,7 @@ export class TypeDefinitionsGenerator {
|
||||
private generateInputTypeDefs(
|
||||
objectMetadataCollection: ObjectMetadataInterface[],
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
isNewRelationEnabled: boolean,
|
||||
) {
|
||||
const inputTypeDefs = objectMetadataCollection
|
||||
.map((objectMetadata) => {
|
||||
@ -226,24 +243,28 @@ export class TypeDefinitionsGenerator {
|
||||
objectMetadata,
|
||||
InputTypeDefinitionKind.Create,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
),
|
||||
// Input type for update
|
||||
this.inputTypeDefinitionFactory.create(
|
||||
optionalExtendedObjectMetadata,
|
||||
InputTypeDefinitionKind.Update,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
),
|
||||
// Filter input type
|
||||
this.inputTypeDefinitionFactory.create(
|
||||
optionalExtendedObjectMetadata,
|
||||
InputTypeDefinitionKind.Filter,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
),
|
||||
// OrderBy input type
|
||||
this.inputTypeDefinitionFactory.create(
|
||||
optionalExtendedObjectMetadata,
|
||||
InputTypeDefinitionKind.OrderBy,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
),
|
||||
];
|
||||
})
|
||||
|
||||
@ -8,11 +8,12 @@ import { FieldMetadataType } from 'twenty-shared/types';
|
||||
|
||||
import { WorkspaceBuildSchemaOptions } from 'src/engine/api/graphql/workspace-schema-builder/interfaces/workspace-build-schema-optionts.interface';
|
||||
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
|
||||
import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
|
||||
|
||||
import { InputTypeDefinitionKind } from 'src/engine/api/graphql/workspace-schema-builder/factories/input-type-definition.factory';
|
||||
import { ObjectTypeDefinitionKind } from 'src/engine/api/graphql/workspace-schema-builder/factories/object-type-definition.factory';
|
||||
import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util';
|
||||
import { isRelationFieldMetadataType } from 'src/engine/utils/is-relation-field-metadata-type.util';
|
||||
import { isFieldMetadataInterfaceOfType } from 'src/engine/utils/is-field-metadata-of-type.util';
|
||||
|
||||
type TypeFactory<T extends InputTypeDefinitionKind | ObjectTypeDefinitionKind> =
|
||||
{
|
||||
@ -40,13 +41,21 @@ export const generateFields = <
|
||||
kind: T,
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
typeFactory: TypeFactory<T>,
|
||||
isNewRelationEnabled = false,
|
||||
): T extends InputTypeDefinitionKind
|
||||
? GraphQLInputFieldConfigMap
|
||||
: GraphQLFieldConfigMap<any, any> => {
|
||||
const fields = {};
|
||||
|
||||
for (const fieldMetadata of objectMetadata.fields) {
|
||||
if (isRelationFieldMetadataType(fieldMetadata.type)) {
|
||||
if (
|
||||
isFieldMetadataInterfaceOfType(
|
||||
fieldMetadata,
|
||||
FieldMetadataType.RELATION,
|
||||
) &&
|
||||
(fieldMetadata.settings?.relationType !== RelationType.MANY_TO_ONE ||
|
||||
!isNewRelationEnabled)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -80,6 +89,25 @@ export const generateFields = <
|
||||
typeFactoryOptions,
|
||||
);
|
||||
|
||||
if (
|
||||
isFieldMetadataInterfaceOfType(
|
||||
fieldMetadata,
|
||||
FieldMetadataType.RELATION,
|
||||
) &&
|
||||
fieldMetadata.settings?.relationType === RelationType.MANY_TO_ONE
|
||||
) {
|
||||
const joinColumnName = fieldMetadata.settings?.joinColumnName;
|
||||
|
||||
if (!joinColumnName) {
|
||||
throw new Error('Join column name is not defined');
|
||||
}
|
||||
|
||||
fields[joinColumnName] = {
|
||||
type,
|
||||
description: fieldMetadata.description,
|
||||
};
|
||||
}
|
||||
|
||||
fields[fieldMetadata.name] = {
|
||||
type,
|
||||
description: fieldMetadata.description,
|
||||
|
||||
@ -25,11 +25,13 @@ export class WorkspaceGraphQLSchemaFactory {
|
||||
objectMetadataCollection: ObjectMetadataInterface[],
|
||||
workspaceResolverBuilderMethods: WorkspaceResolverBuilderMethods,
|
||||
options: WorkspaceBuildSchemaOptions = {},
|
||||
isNewRelationEnabled = false,
|
||||
): Promise<GraphQLSchema> {
|
||||
// Generate type definitions
|
||||
await this.typeDefinitionsGenerator.generate(
|
||||
objectMetadataCollection,
|
||||
options,
|
||||
isNewRelationEnabled,
|
||||
);
|
||||
|
||||
// Generate schema
|
||||
|
||||
Reference in New Issue
Block a user