Skip to content

Commit 99d3266

Browse files
authored
Merge pull request #4878 from opsmill/lgu-search-groups
Search anywhere looks at Groups
2 parents d8abe02 + 508ab84 commit 99d3266

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

backend/infrahub/graphql/queries/search.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async def search_resolver(
105105
) -> dict[str, Any]:
106106
context: GraphqlContext = info.context
107107
response: dict[str, Any] = {}
108-
result: list[CoreNode] = []
108+
results: list[CoreNode] = []
109109

110110
fields = await extract_fields_first_node(info)
111111

@@ -114,30 +114,30 @@ async def search_resolver(
114114
db=context.db, branch=context.branch, at=context.at, id=q
115115
)
116116
if matching:
117-
result.append(matching)
117+
results.append(matching)
118118
else:
119119
try:
120120
# Convert any IPv6 address, network or partial address to collapsed format as it might be stored in db.
121121
q = _collapse_ipv6(q)
122122
except (ValueError, ipaddress.AddressValueError):
123123
pass
124124

125-
result.extend(
126-
await NodeManager.query(
125+
for kind in [InfrahubKind.NODE, InfrahubKind.GENERICGROUP]:
126+
objs = await NodeManager.query(
127127
db=context.db,
128128
branch=context.branch,
129-
schema=InfrahubKind.NODE,
129+
schema=kind,
130130
filters={"any__value": q},
131131
limit=limit,
132132
partial_match=partial_match,
133133
)
134-
)
134+
results.extend(objs)
135135

136-
if "edges" in fields and result:
137-
response["edges"] = [{"node": {"id": obj.id, "kind": obj.get_kind()}} for obj in result]
136+
if "edges" in fields and len(results) > 0:
137+
response["edges"] = [{"node": {"id": obj.id, "kind": obj.get_kind()}} for obj in results]
138138

139139
if "count" in fields:
140-
response["count"] = len(result)
140+
response["count"] = len(results)
141141

142142
return response
143143

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from graphql import graphql
33

44
from infrahub.core.branch import Branch
5+
from infrahub.core.constants import InfrahubKind
56
from infrahub.core.node import Node
67
from infrahub.database import InfrahubDatabase
78
from infrahub.graphql.initialization import prepare_graphql_params
@@ -282,3 +283,28 @@ def test_collapse_ipv6_address_or_network(query, expected):
282283
def test_collapse_ipv6_address_or_network_invalid_cases(query):
283284
with pytest.raises(ValueError):
284285
_collapse_ipv6(query)
286+
287+
288+
async def test_search_groups(
289+
db: InfrahubDatabase,
290+
default_branch: Branch,
291+
register_core_models_schema,
292+
register_builtin_models_schema,
293+
car_person_data_generic,
294+
):
295+
group1 = await Node.init(db=db, schema=InfrahubKind.STANDARDGROUP)
296+
await group1.new(db=db, name="group1", members=[car_person_data_generic["c1"], car_person_data_generic["c2"]])
297+
await group1.save(db=db)
298+
299+
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
300+
301+
result = await graphql(
302+
schema=gql_params.schema,
303+
source=SEARCH_QUERY,
304+
context_value=gql_params.context,
305+
root_value=None,
306+
variable_values={"search": "group1"},
307+
)
308+
assert result.data["InfrahubSearchAnywhere"]["count"] == 1
309+
310+
assert result.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] == group1.id

changelog/3173.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix search anywhere so it looks at Groups

0 commit comments

Comments
 (0)