-
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
Merged
charlesBochet
merged 4 commits into
main
from
c--add-destroy-many-to-graphql-query-runner
Oct 8, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...api/graphql/graphql-query-runner/resolvers/graphql-query-destroy-many-resolver.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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'); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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