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 all commits
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
Expand Up @@ -27,25 +27,34 @@ export class ObjectRecordsToGraphqlConnectionHelper {
this.objectMetadataMap = objectMetadataMap;
}

public createConnection<ObjectRecord extends IRecord = IRecord>(
objectRecords: ObjectRecord[],
objectName: string,
take: number,
totalCount: number,
order: RecordOrderBy | undefined,
hasNextPage: boolean,
hasPreviousPage: boolean,
public createConnection<ObjectRecord extends IRecord = IRecord>({
objectRecords,
objectName,
take,
totalCount,
order,
hasNextPage,
hasPreviousPage,
depth = 0,
): IConnection<ObjectRecord> {
}: {
objectRecords: ObjectRecord[];
objectName: string;
take: number;
totalCount: number;
order?: RecordOrderBy;
hasNextPage: boolean;
hasPreviousPage: boolean;
depth?: number;
}): IConnection<ObjectRecord> {
const edges = (objectRecords ?? []).map((objectRecord) => ({
node: this.processRecord(
node: this.processRecord({
objectRecord,
objectName,
take,
totalCount,
order,
depth,
),
}),
cursor: encodeCursor(objectRecord, order),
}));

Expand All @@ -61,14 +70,21 @@ export class ObjectRecordsToGraphqlConnectionHelper {
};
}

public processRecord<T extends Record<string, any>>(
objectRecord: T,
objectName: string,
take: number,
totalCount: number,
order?: RecordOrderBy,
public processRecord<T extends Record<string, any>>({
objectRecord,
objectName,
take,
totalCount,
order,
depth = 0,
): T {
}: {
objectRecord: T;
objectName: string;
take: number;
totalCount: number;
order?: RecordOrderBy;
depth?: number;
}): T {
if (depth >= CONNECTION_MAX_DEPTH) {
throw new GraphqlQueryRunnerException(
`Maximum depth of ${CONNECTION_MAX_DEPTH} reached`,
Expand Down Expand Up @@ -97,27 +113,31 @@ export class ObjectRecordsToGraphqlConnectionHelper {

if (isRelationFieldMetadataType(fieldMetadata.type)) {
if (Array.isArray(value)) {
processedObjectRecord[key] = this.createConnection(
value,
getRelationObjectMetadata(fieldMetadata, this.objectMetadataMap)
.nameSingular,
processedObjectRecord[key] = this.createConnection({
objectRecords: value,
objectName: getRelationObjectMetadata(
fieldMetadata,
this.objectMetadataMap,
).nameSingular,
take,
value.length,
totalCount: value.length,
order,
false,
false,
depth + 1,
);
hasNextPage: false,
hasPreviousPage: false,
depth: depth + 1,
});
} else if (isPlainObject(value)) {
processedObjectRecord[key] = this.processRecord(
value,
getRelationObjectMetadata(fieldMetadata, this.objectMetadataMap)
.nameSingular,
processedObjectRecord[key] = this.processRecord({
objectRecord: value,
objectName: getRelationObjectMetadata(
fieldMetadata,
this.objectMetadataMap,
).nameSingular,
take,
totalCount,
order,
depth + 1,
);
depth: depth + 1,
});
}
} else if (isCompositeFieldMetadataType(fieldMetadata.type)) {
processedObjectRecord[key] = this.processCompositeField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export class GraphqlQueryCreateManyResolverService
new ObjectRecordsToGraphqlConnectionHelper(objectMetadataMap);

return upsertedRecords.map((record: ObjectRecord) =>
typeORMObjectRecordsParser.processRecord(
record,
objectMetadataMapItem.nameSingular,
1,
1,
),
typeORMObjectRecordsParser.processRecord({
objectRecord: record,
objectName: objectMetadataMapItem.nameSingular,
take: 1,
totalCount: 1,
}),
);
}

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({
objectRecord: record,
objectName: objectMetadataMapItem.nameSingular,
take: 1,
totalCount: 1,
}),
);
}

async validate(
args: DestroyManyResolverArgs,
_options: WorkspaceQueryRunnerOptions,
): Promise<void> {
if (!args.filter) {
throw new Error('Filter is required');
}
}
}
Loading
Loading