-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
); | ||
} | ||
|
||
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'; | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
There was a problem hiding this comment.
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