22
33from typing import TYPE_CHECKING , Any
44
5- from graphene import Boolean , Field , Int , String
5+ from graphene import Boolean , Field , Int , List , NonNull , String
66
77from infrahub .core .branch import Branch
88from infrahub .core .constants import GLOBAL_BRANCH_NAME
99
10+ from ...exceptions import BranchNotFoundError
1011from .enums import InfrahubBranchStatus
1112from .standard_node import InfrahubObjectType
1213
@@ -33,6 +34,10 @@ class Meta:
3334 name = "Branch"
3435 model = Branch
3536
37+ @staticmethod
38+ async def _map_fields_to_graphql (objs : list [Branch ], fields : dict ) -> list [dict [str , Any ]]:
39+ return [await obj .to_graphql (fields = fields ) for obj in objs if obj .name != GLOBAL_BRANCH_NAME ]
40+
3641 @classmethod
3742 async def get_list (
3843 cls ,
@@ -46,4 +51,82 @@ async def get_list(
4651 if not objs :
4752 return []
4853
49- return [await obj .to_graphql (fields = fields ) for obj in objs if obj .name != GLOBAL_BRANCH_NAME ]
54+ return await cls ._map_fields_to_graphql (objs = objs , fields = fields )
55+
56+
57+ class RequiredStringValueField (InfrahubObjectType ):
58+ value = String (required = True )
59+
60+
61+ class NonRequiredStringValueField (InfrahubObjectType ):
62+ value = String (required = False )
63+
64+
65+ class NonRequiredIntValueField (InfrahubObjectType ):
66+ value = Int (required = False )
67+
68+
69+ class NonRequiredBooleanValueField (InfrahubObjectType ):
70+ value = Boolean (required = False )
71+
72+
73+ class StatusField (InfrahubObjectType ):
74+ value = InfrahubBranchStatus (required = True )
75+
76+
77+ class InfrahubBranch (BranchType ):
78+ name = Field (RequiredStringValueField , required = True )
79+ description = Field (NonRequiredStringValueField , required = False )
80+ origin_branch = Field (NonRequiredStringValueField , required = False )
81+ branched_from = Field (NonRequiredStringValueField , required = False )
82+ graph_version = Field (NonRequiredIntValueField , required = False )
83+ status = Field (StatusField , required = True )
84+ sync_with_git = Field (NonRequiredBooleanValueField , required = False )
85+ is_default = Field (NonRequiredBooleanValueField , required = False )
86+ is_isolated = Field (
87+ NonRequiredBooleanValueField , required = False , deprecation_reason = "non isolated mode is not supported anymore"
88+ )
89+ has_schema_changes = Field (NonRequiredBooleanValueField , required = False )
90+
91+ class Meta :
92+ description = "InfrahubBranch"
93+ name = "InfrahubBranch"
94+
95+ @staticmethod
96+ async def _map_fields_to_graphql (objs : list [Branch ], fields : dict ) -> list [dict [str , Any ]]:
97+ field_keys = fields .keys ()
98+ result : list [dict [str , Any ]] = []
99+ for obj in objs :
100+ if obj .name == GLOBAL_BRANCH_NAME :
101+ continue
102+ data : dict [str , Any ] = {}
103+ for field in field_keys :
104+ if field == "id" :
105+ data ["id" ] = obj .uuid
106+ continue
107+ value = getattr (obj , field , None )
108+ if isinstance (fields .get (field ), dict ):
109+ data [field ] = {"value" : value }
110+ else :
111+ data [field ] = value
112+ result .append (data )
113+ return result
114+
115+
116+ class InfrahubBranchEdge (InfrahubObjectType ):
117+ node = Field (InfrahubBranch , required = True )
118+
119+
120+ class InfrahubBranchType (InfrahubObjectType ):
121+ count = Field (Int , description = "Total number of items" )
122+ edges = Field (NonNull (List (of_type = NonNull (InfrahubBranchEdge ))))
123+
124+ @classmethod
125+ async def get_list_count (cls , graphql_context : GraphqlContext , ** kwargs : Any ) -> int :
126+ async with graphql_context .db .start_session (read_only = True ) as db :
127+ count = await Branch .get_list_count (db = db , ** kwargs )
128+ try :
129+ await Branch .get_by_name (name = GLOBAL_BRANCH_NAME , db = db )
130+ return count - 1
131+ except BranchNotFoundError :
132+ return count
0 commit comments