11from __future__ import annotations
22
3+ from dataclasses import dataclass , field
34from typing import TYPE_CHECKING , Any , Mapping , Optional , Union
45
56from graphene import InputObjectType , Mutation
4950KINDS_CONCURRENT_MUTATIONS_NOT_ALLOWED = [InfrahubKind .GENERICGROUP ]
5051
5152
53+ @dataclass
54+ class DeleteResult :
55+ node : Node
56+ mutation : InfrahubMutationMixin
57+ deleted_nodes : list [Node ] = field (default_factory = list )
58+
59+
5260# ------------------------------------------
5361# Infrahub GraphQLType
5462# ------------------------------------------
@@ -64,7 +72,7 @@ async def mutate(cls, root: dict, info: GraphQLResolveInfo, data: InputObjectTyp
6472 obj = None
6573 mutation = None
6674 action = MutationAction .UNDEFINED
67- deleted : list [Node ] = []
75+ deleted_nodes : list [Node ] = []
6876
6977 if "Create" in cls .__name__ :
7078 obj , mutation = await cls .mutate_create (info = info , branch = graphql_context .branch , data = data , ** kwargs )
@@ -87,9 +95,11 @@ async def mutate(cls, root: dict, info: GraphQLResolveInfo, data: InputObjectTyp
8795 else :
8896 action = MutationAction .UPDATED
8997 elif "Delete" in cls .__name__ :
90- obj , mutation , deleted = await cls .mutate_delete (
91- info = info , branch = graphql_context .branch , data = data , ** kwargs
92- )
98+ delete_result = await cls .mutate_delete (info = info , branch = graphql_context .branch , data = data , ** kwargs )
99+ obj = delete_result .node
100+ mutation = delete_result .mutation
101+ deleted_nodes = delete_result .deleted_nodes
102+
93103 action = MutationAction .DELETED
94104 else :
95105 raise ValueError (
@@ -127,8 +137,8 @@ async def mutate(cls, root: dict, info: GraphQLResolveInfo, data: InputObjectTyp
127137
128138 events = [main_event ]
129139
130- deleted_changelogs = [node .node_changelog for node in deleted if node .id != obj .id ]
131- deleted_ids = [ node .node_id for node in deleted_changelogs ]
140+ deleted_changelogs = [node .node_changelog for node in deleted_nodes if node .id != obj .id ]
141+ deleted_ids = { node .node_id for node in deleted_changelogs }
132142
133143 for node_changelog in deleted_changelogs :
134144 meta = EventMeta .from_parent (parent = main_event )
@@ -448,9 +458,9 @@ async def mutate_update_object(
448458 await node_constraint_runner .check (node = obj , field_filters = fields_to_validate )
449459
450460 fields = list (data .keys ())
451- for field in ("id" , "hfid" ):
452- if field in fields :
453- fields .remove (field )
461+ for field_to_remove in ("id" , "hfid" ):
462+ if field_to_remove in fields :
463+ fields .remove (field_to_remove )
454464
455465 await obj .save (db = db , fields = fields )
456466
@@ -513,7 +523,7 @@ async def mutate_delete(
513523 info : GraphQLResolveInfo ,
514524 data : InputObjectType ,
515525 branch : Branch ,
516- ) -> tuple [ Node , Self , list [ Node ]] :
526+ ) -> DeleteResult :
517527 graphql_context : GraphqlContext = info .context
518528
519529 obj = await NodeManager .find_object (
@@ -531,7 +541,7 @@ async def mutate_delete(
531541
532542 ok = True
533543
534- return obj , cls (ok = ok ), deleted
544+ return DeleteResult ( node = obj , mutation = cls (ok = ok ), deleted_nodes = deleted )
535545
536546
537547class InfrahubMutation (InfrahubMutationMixin , Mutation ):
0 commit comments