Skip to content

Commit 84fac0a

Browse files
committed
change response format
1 parent a38cec2 commit 84fac0a

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

backend/infrahub/graphql/queries/branch.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from graphene import ID, Field, Int, List, NonNull, String
66

77
from infrahub.graphql.field_extractor import extract_graphql_fields
8-
from infrahub.graphql.types import BranchType, InfrahubBranchType
8+
from infrahub.graphql.types import BranchType, InfrahubBranch, InfrahubBranchType
99

1010
if TYPE_CHECKING:
1111
from graphql import GraphQLResolveInfo
@@ -39,12 +39,10 @@ async def infrahub_branch_resolver(
3939
fields = extract_graphql_fields(info)
4040
result: dict[str, Any] = {}
4141
if "edges" in fields:
42-
result["edges"] = [
43-
{"node": branch}
44-
for branch in await BranchType.get_list(
45-
graphql_context=info.context, fields=fields.get("edges", {}).get("node", {}), limit=limit, offset=offset
46-
)
47-
]
42+
branches = await InfrahubBranch.get_list(
43+
graphql_context=info.context, fields=fields.get("edges", {}).get("node", {}), limit=limit, offset=offset
44+
)
45+
result["edges"] = [{"node": branch} for branch in branches]
4846
if "count" in fields:
4947
result["count"] = await InfrahubBranchType.get_list_count(graphql_context=info.context)
5048
return result

backend/infrahub/graphql/types/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
StrAttributeType,
2222
TextAttributeType,
2323
)
24-
from .branch import BranchType, InfrahubBranchType
24+
from .branch import BranchType, InfrahubBranch, InfrahubBranchType
2525
from .interface import InfrahubInterface
2626
from .node import InfrahubObject
2727
from .permission import PaginatedObjectPermission
@@ -41,6 +41,7 @@
4141
"DropdownType",
4242
"IPHostType",
4343
"IPNetworkType",
44+
"InfrahubBranch",
4445
"InfrahubBranchType",
4546
"InfrahubInterface",
4647
"InfrahubObject",

backend/infrahub/graphql/types/branch.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class Meta:
3131
name = "Branch"
3232
model = Branch
3333

34+
@staticmethod
35+
async def _map_fields_to_graphql(objs: list[Branch], fields: dict) -> list[dict[str, Any]]:
36+
return [await obj.to_graphql(fields=fields) for obj in objs if obj.name != GLOBAL_BRANCH_NAME]
37+
3438
@classmethod
3539
async def get_list(
3640
cls,
@@ -44,11 +48,60 @@ async def get_list(
4448
if not objs:
4549
return []
4650

47-
return [await obj.to_graphql(fields=fields) for obj in objs if obj.name != GLOBAL_BRANCH_NAME]
51+
return await cls._map_fields_to_graphql(objs=objs, fields=fields)
52+
53+
54+
class RequiredStringValueField(InfrahubObjectType):
55+
value = String(required=True)
56+
57+
58+
class NonRequiredStringValueField(InfrahubObjectType):
59+
value = String(required=False)
60+
61+
62+
class NonRequiredBooleanValueField(InfrahubObjectType):
63+
value = Boolean(required=False)
64+
65+
66+
class InfrahubBranch(BranchType):
67+
id = String(required=True)
68+
created_at = String(required=False)
69+
70+
name = Field(RequiredStringValueField, required=True)
71+
description = Field(NonRequiredStringValueField, required=False)
72+
origin_branch = Field(NonRequiredStringValueField, required=False)
73+
branched_from = Field(NonRequiredStringValueField, required=False)
74+
sync_with_git = Field(NonRequiredBooleanValueField, required=False)
75+
is_default = Field(NonRequiredBooleanValueField, required=False)
76+
is_isolated = Field(
77+
NonRequiredBooleanValueField, required=False, deprecation_reason="non isolated mode is not supported anymore"
78+
)
79+
has_schema_changes = Field(NonRequiredBooleanValueField, required=False)
80+
81+
class Meta:
82+
description = "InfrahubBranch"
83+
name = "InfrahubBranch"
84+
85+
@staticmethod
86+
async def _map_fields_to_graphql(objs: list[Branch], fields: dict) -> list[dict[str, Any]]:
87+
field_keys = fields.keys()
88+
result: list[dict[str, Any]] = []
89+
for obj in objs:
90+
if obj.name == GLOBAL_BRANCH_NAME:
91+
continue
92+
data = {}
93+
for field in field_keys:
94+
value = getattr(obj, field, None)
95+
if isinstance(fields.get(field), dict):
96+
data[field] = {"value": value}
97+
else:
98+
data[field] = value
99+
result.append(data)
100+
return result
48101

49102

50103
class InfrahubBranchEdge(InfrahubObjectType):
51-
node = Field(BranchType, required=True)
104+
node = Field(InfrahubBranch, required=True)
52105

53106

54107
class InfrahubBranchType(InfrahubObjectType):

backend/tests/unit/graphql/queries/test_branch.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,12 @@ async def test_paginated_branch_query(
189189
count
190190
edges {
191191
node {
192-
name
193-
description
192+
name {
193+
value
194+
}
195+
description {
196+
value
197+
}
194198
}
195199
}
196200
}
@@ -210,22 +214,22 @@ async def test_paginated_branch_query(
210214

211215
expected_branches = [
212216
{
213-
"description": "Default Branch",
214-
"name": "main",
217+
"description": {"value": "Default Branch"},
218+
"name": {"value": "main"},
215219
},
216220
{
217-
"description": "my description",
218-
"name": "branch3",
221+
"description": {"value": "my description"},
222+
"name": {"value": "branch3"},
219223
},
220224
*[
221225
{
222-
"description": f"sample description {i}",
223-
"name": f"sample-branch-{i}",
226+
"description": {"value": f"sample description {i}"},
227+
"name": {"value": f"sample-branch-{i}"},
224228
}
225229
for i in range(10)
226230
],
227231
]
228232
all_branches_data_only = [branch.get("node") for branch in all_branches.data["InfrahubBranch"]["edges"]]
229-
assert all_branches_data_only.sort(key=operator.itemgetter("name")) == expected_branches.sort(
230-
key=operator.itemgetter("name")
233+
assert all_branches_data_only.sort(key=lambda x: x["name"]["value"]) == expected_branches.sort(
234+
key=lambda x: x["name"]["value"]
231235
)

0 commit comments

Comments
 (0)