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

Add DestroyMany to graphql query runner #7507

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

import { GraphqlQueryCreateManyResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-create-many-resolver.service';
import { GraphqlQueryDestroyManyResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-destroy-many-resolver.service';
import { GraphqlQueryDestroyOneResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-destroy-one-resolver.service';
import { GraphqlQueryFindDuplicatesResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-find-duplicates-resolver.service';
import { GraphqlQueryFindManyResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-find-many-resolver.service';
Expand Down Expand Up @@ -37,6 +38,8 @@ export class GraphqlQueryResolverFactory {
return this.moduleRef.get(GraphqlQueryCreateManyResolverService);
case 'destroyOne':
return this.moduleRef.get(GraphqlQueryDestroyOneResolverService);
case 'destroyMany':
return this.moduleRef.get(GraphqlQueryDestroyManyResolverService);
case 'updateOne':
case 'deleteOne':
return this.moduleRef.get(GraphqlQueryUpdateOneResolverService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Module } from '@nestjs/common';
import { GraphqlQueryResolverFactory } from 'src/engine/api/graphql/graphql-query-runner/factories/graphql-query-resolver.factory';
import { GraphqlQueryRunnerService } from 'src/engine/api/graphql/graphql-query-runner/graphql-query-runner.service';
import { GraphqlQueryCreateManyResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-create-many-resolver.service';
import { GraphqlQueryDestroyManyResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-destroy-many-resolver.service';
import { GraphqlQueryDestroyOneResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-destroy-one-resolver.service';
import { GraphqlQueryFindDuplicatesResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-find-duplicates-resolver.service';
import { GraphqlQueryFindManyResolverService } from 'src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-find-many-resolver.service';
Expand All @@ -16,14 +17,15 @@ import { WorkspaceQueryRunnerModule } from 'src/engine/api/graphql/workspace-que
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';

const graphqlQueryResolvers = [
GraphqlQueryFindOneResolverService,
GraphqlQueryFindManyResolverService,
GraphqlQueryFindDuplicatesResolverService,
GraphqlQueryCreateManyResolverService,
GraphqlQueryDestroyManyResolverService,
GraphqlQueryDestroyOneResolverService,
GraphqlQueryUpdateOneResolverService,
GraphqlQueryUpdateManyResolverService,
GraphqlQueryFindDuplicatesResolverService,
GraphqlQueryFindManyResolverService,
GraphqlQueryFindOneResolverService,
GraphqlQuerySearchResolverService,
GraphqlQueryUpdateManyResolverService,
GraphqlQueryUpdateOneResolverService,
];

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CreateOneResolverArgs,
DeleteManyResolverArgs,
DeleteOneResolverArgs,
DestroyManyResolverArgs,
DestroyOneResolverArgs,
FindDuplicatesResolverArgs,
FindManyResolverArgs,
Expand Down Expand Up @@ -285,6 +286,25 @@ export class GraphqlQueryRunnerService {
return result;
}

@LogExecutionTime()
async destroyMany<ObjectRecord extends IRecord>(
args: DestroyManyResolverArgs,
options: WorkspaceQueryRunnerOptions,
): Promise<ObjectRecord[]> {
const result = await this.executeQuery<
DestroyManyResolverArgs,
ObjectRecord[]
>('destroyMany', args, options);

this.apiEventEmitterService.emitDestroyEvents(
result,
options.authContext,
options.objectMetadataItem,
);

return result;
}

@LogExecutionTime()
public async restoreMany<ObjectRecord extends IRecord>(
args: RestoreManyResolverArgs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Injectable } from '@nestjs/common';

import graphqlFields from 'graphql-fields';

import { ResolverService } from 'src/engine/api/graphql/graphql-query-runner/interfaces/resolver-service.interface';
import { Record as IRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';
import { WorkspaceQueryRunnerOptions } from 'src/engine/api/graphql/workspace-query-runner/interfaces/query-runner-option.interface';
import { DestroyManyResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

import { QUERY_MAX_RECORDS } from 'src/engine/api/graphql/graphql-query-runner/constants/query-max-records.constant';
import { GraphqlQueryParser } from 'src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query.parser';
import { ObjectRecordsToGraphqlConnectionHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/object-records-to-graphql-connection.helper';
import { ProcessNestedRelationsHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/process-nested-relations.helper';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { formatResult } from 'src/engine/twenty-orm/utils/format-result.util';

@Injectable()
export class GraphqlQueryDestroyManyResolverService
implements ResolverService<DestroyManyResolverArgs, IRecord[]>
{
constructor(
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {}

async resolve<ObjectRecord extends IRecord = IRecord>(
args: DestroyManyResolverArgs,
options: WorkspaceQueryRunnerOptions,
): Promise<ObjectRecord[]> {
const { authContext, objectMetadataMapItem, objectMetadataMap, info } =
options;
const dataSource =
await this.twentyORMGlobalManager.getDataSourceForWorkspace(
authContext.workspace.id,
);

const repository = dataSource.getRepository(
objectMetadataMapItem.nameSingular,
);

const graphqlQueryParser = new GraphqlQueryParser(
objectMetadataMapItem.fields,
objectMetadataMap,
);

const selectedFields = graphqlFields(info);

const { relations } = graphqlQueryParser.parseSelectedFields(
objectMetadataMapItem,
selectedFields,
);

const queryBuilder = repository.createQueryBuilder(
objectMetadataMapItem.nameSingular,
);

const withFilterQueryBuilder = graphqlQueryParser.applyFilterToBuilder(
queryBuilder,
objectMetadataMapItem.nameSingular,
args.filter,
);

const nonFormattedDeletedObjectRecords = await withFilterQueryBuilder
.delete()
.returning('*')
.execute();

const deletedRecords = formatResult(
nonFormattedDeletedObjectRecords.raw,
objectMetadataMapItem,
objectMetadataMap,
);

const processNestedRelationsHelper = new ProcessNestedRelationsHelper();

if (relations) {
await processNestedRelationsHelper.processNestedRelations(
objectMetadataMap,
objectMetadataMapItem,
deletedRecords,
relations,
QUERY_MAX_RECORDS,
authContext,
dataSource,
);
}
Comment on lines +75 to +85
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Processing nested relations after deletion might lead to inconsistencies. Consider handling this before deletion or removing it if not necessary


const typeORMObjectRecordsParser =
new ObjectRecordsToGraphqlConnectionHelper(objectMetadataMap);

return deletedRecords.map((record: ObjectRecord) =>
typeORMObjectRecordsParser.processRecord(
record,
objectMetadataMapItem.nameSingular,
1,
1,
),
);
}

async validate(
args: DestroyManyResolverArgs,
_options: WorkspaceQueryRunnerOptions,
): Promise<void> {
if (!args.filter) {
throw new Error('Filter is required');
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Injectable } from '@nestjs/common';

import graphqlFields from 'graphql-fields';

import { ResolverService } from 'src/engine/api/graphql/graphql-query-runner/interfaces/resolver-service.interface';
import { Record as IRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';
import { WorkspaceQueryRunnerOptions } from 'src/engine/api/graphql/workspace-query-runner/interfaces/query-runner-option.interface';
import { DestroyOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

import { QUERY_MAX_RECORDS } from 'src/engine/api/graphql/graphql-query-runner/constants/query-max-records.constant';
import {
GraphqlQueryRunnerException,
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
import { GraphqlQueryParser } from 'src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query.parser';
import { ObjectRecordsToGraphqlConnectionHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/object-records-to-graphql-connection.helper';
import { ProcessNestedRelationsHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/process-nested-relations.helper';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { formatResult } from 'src/engine/twenty-orm/utils/format-result.util';

Expand All @@ -24,34 +30,78 @@ export class GraphqlQueryDestroyOneResolverService
args: DestroyOneResolverArgs,
options: WorkspaceQueryRunnerOptions,
): Promise<ObjectRecord> {
const { authContext, objectMetadataMapItem, objectMetadataMap } = options;
const repository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace(
const { authContext, objectMetadataMapItem, objectMetadataMap, info } =
options;
const dataSource =
await this.twentyORMGlobalManager.getDataSourceForWorkspace(
authContext.workspace.id,
objectMetadataMapItem.nameSingular,
);

const nonFormattedRecordBeforeDeletion = await repository.findOne({
where: { id: args.id },
withDeleted: true,
});
const repository = dataSource.getRepository(
objectMetadataMapItem.nameSingular,
);

const graphqlQueryParser = new GraphqlQueryParser(
objectMetadataMapItem.fields,
objectMetadataMap,
);

const selectedFields = graphqlFields(info);

const { relations } = graphqlQueryParser.parseSelectedFields(
objectMetadataMapItem,
selectedFields,
);

const queryBuilder = repository.createQueryBuilder(
objectMetadataMapItem.nameSingular,
);

const nonFormattedDeletedObjectRecords = await queryBuilder
.where({
id: args.id,
})
.take(1)
.delete()
.returning('*')
.execute();

if (!nonFormattedRecordBeforeDeletion) {
if (!nonFormattedDeletedObjectRecords.affected) {
throw new GraphqlQueryRunnerException(
'Record not found',
GraphqlQueryRunnerExceptionCode.RECORD_NOT_FOUND,
);
}

const recordBeforeDeletion = formatResult(
[nonFormattedRecordBeforeDeletion],
nonFormattedDeletedObjectRecords.raw,
objectMetadataMapItem,
objectMetadataMap,
)[0];

await repository.delete(args.id);
const processNestedRelationsHelper = new ProcessNestedRelationsHelper();

if (relations) {
await processNestedRelationsHelper.processNestedRelations(
objectMetadataMap,
objectMetadataMapItem,
[recordBeforeDeletion],
relations,
QUERY_MAX_RECORDS,
authContext,
dataSource,
);
}
Comment on lines +84 to +94
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Processing nested relations after deletion might lead to inconsistencies if related records were also deleted. Consider handling this scenario.


const typeORMObjectRecordsParser =
new ObjectRecordsToGraphqlConnectionHelper(objectMetadataMap);

return recordBeforeDeletion as ObjectRecord;
return typeORMObjectRecordsParser.processRecord(
recordBeforeDeletion,
objectMetadataMapItem.nameSingular,
1,
1,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Hardcoding '1' for totalCount and pageCount may be incorrect if used in pagination context. Consider using actual values or removing if unnecessary.

}

async validate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,13 @@ export class GraphqlQueryUpdateManyResolverService

const data = formatData(args.data, objectMetadataMapItem);

const result = await withFilterQueryBuilder
const nonFormattedUpdatedObjectRecords = await withFilterQueryBuilder
.update(data)
.returning('*')
.execute();

const nonFormattedUpdatedObjectRecords = result.raw;

const updatedRecords = formatResult(
nonFormattedUpdatedObjectRecords,
nonFormattedUpdatedObjectRecords.raw,
objectMetadataMapItem,
objectMetadataMap,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import {
} from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
import { WorkspaceSchemaBuilderContext } from 'src/engine/api/graphql/workspace-schema-builder/interfaces/workspace-schema-builder-context.interface';

import { GraphqlQueryRunnerService } from 'src/engine/api/graphql/graphql-query-runner/graphql-query-runner.service';
import { workspaceQueryRunnerGraphqlApiExceptionHandler } from 'src/engine/api/graphql/workspace-query-runner/utils/workspace-query-runner-graphql-api-exception-handler.util';
import { WorkspaceQueryRunnerService } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-runner.service';
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';

@Injectable()
export class DestroyManyResolverFactory
Expand All @@ -19,6 +22,8 @@ export class DestroyManyResolverFactory

constructor(
private readonly workspaceQueryRunnerService: WorkspaceQueryRunnerService,
private readonly featureFlagService: FeatureFlagService,
private readonly graphqlQueryRunnerService: GraphqlQueryRunnerService,
) {}

create(
Expand All @@ -38,6 +43,19 @@ export class DestroyManyResolverFactory
objectMetadataMapItem: internalContext.objectMetadataMapItem,
};

const isQueryRunnerTwentyORMEnabled =
await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IsQueryRunnerTwentyORMEnabled,
internalContext.authContext.workspace.id,
);

if (isQueryRunnerTwentyORMEnabled) {
return await this.graphqlQueryRunnerService.destroyMany(
args,
options,
);
}

return await this.workspaceQueryRunnerService.destroyMany(
args,
options,
Expand Down
Loading