Skip to content

Commit 53fa7a3

Browse files
committed
Change behavior of include to only include the requested fields
1 parent 322a0d1 commit 53fa7a3

File tree

3 files changed

+26
-61
lines changed

3 files changed

+26
-61
lines changed

changelog/192.changed.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When using include as part of a query, the default behavior has changed.
2+
Only the fields defined in `include` will be returned now, where previously the Python SDK would add the fields provided to the default one.

infrahub_sdk/node.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,7 @@ def generate_query_data_init(
992992
data["@filters"]["limit"] = limit
993993

994994
if include and exclude:
995-
in_both, _, _ = compare_lists(include, exclude)
996-
if in_both:
997-
raise ValueError(f"{in_both} are part of both include and exclude")
995+
raise ValueError("include and exclude are exclusive, they shouldn't be used together")
998996

999997
if partial_match:
1000998
data["@filters"]["partial_match"] = True
@@ -1244,7 +1242,7 @@ async def generate_query_data_node(
12441242
data: dict[str, Any] = {}
12451243

12461244
for attr_name in self._attributes:
1247-
if exclude and attr_name in exclude:
1245+
if (exclude and attr_name in exclude) or (include and attr_name not in include):
12481246
continue
12491247

12501248
attr: Attribute = getattr(self, attr_name)
@@ -1262,7 +1260,7 @@ async def generate_query_data_node(
12621260
data[attr_name] = {"@alias": f"__alias__{self._schema.kind}__{attr_name}"}
12631261

12641262
for rel_name in self._relationships:
1265-
if exclude and rel_name in exclude:
1263+
if (exclude and rel_name in exclude) or (include and rel_name not in include):
12661264
continue
12671265

12681266
rel_schema = self._schema.get_relationship(name=rel_name)
@@ -1749,7 +1747,7 @@ def generate_query_data_node(
17491747
data: dict[str, Any] = {}
17501748

17511749
for attr_name in self._attributes:
1752-
if exclude and attr_name in exclude:
1750+
if (exclude and attr_name in exclude) or (include and attr_name not in include):
17531751
continue
17541752

17551753
attr: Attribute = getattr(self, attr_name)
@@ -1767,7 +1765,7 @@ def generate_query_data_node(
17671765
data[attr_name] = {"@alias": f"__alias__{self._schema.kind}__{attr_name}"}
17681766

17691767
for rel_name in self._relationships:
1770-
if exclude and rel_name in exclude:
1768+
if (exclude and rel_name in exclude) or (include and rel_name not in include):
17711769
continue
17721770

17731771
rel_schema = self._schema.get_relationship(name=rel_name)

tests/unit/sdk/test_node.py

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -981,10 +981,10 @@ async def test_query_data_generic_fragment(clients, mock_schema_query_02, client
981981
async def test_query_data_include_property(client, location_schema: NodeSchemaAPI, client_type):
982982
if client_type == "standard":
983983
node = InfrahubNode(client=client, schema=location_schema)
984-
data = await node.generate_query_data(include=["tags"], property=True)
984+
data = await node.generate_query_data(include=["name", "type", "tags"], property=True)
985985
else:
986986
node = InfrahubNodeSync(client=client, schema=location_schema)
987-
data = node.generate_query_data(include=["tags"], property=True)
987+
data = node.generate_query_data(include=["name", "type", "tags"], property=True)
988988

989989
assert data == {
990990
"BuiltinLocation": {
@@ -1013,23 +1013,6 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
10131013
},
10141014
"value": None,
10151015
},
1016-
"description": {
1017-
"is_default": None,
1018-
"is_from_profile": None,
1019-
"is_protected": None,
1020-
"is_visible": None,
1021-
"owner": {
1022-
"__typename": None,
1023-
"display_label": None,
1024-
"id": None,
1025-
},
1026-
"source": {
1027-
"__typename": None,
1028-
"display_label": None,
1029-
"id": None,
1030-
},
1031-
"value": None,
1032-
},
10331016
"type": {
10341017
"is_default": None,
10351018
"is_from_profile": None,
@@ -1047,28 +1030,6 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
10471030
},
10481031
"value": None,
10491032
},
1050-
"primary_tag": {
1051-
"properties": {
1052-
"is_protected": None,
1053-
"is_visible": None,
1054-
"owner": {
1055-
"__typename": None,
1056-
"display_label": None,
1057-
"id": None,
1058-
},
1059-
"source": {
1060-
"__typename": None,
1061-
"display_label": None,
1062-
"id": None,
1063-
},
1064-
},
1065-
"node": {
1066-
"id": None,
1067-
"hfid": None,
1068-
"display_label": None,
1069-
"__typename": None,
1070-
},
1071-
},
10721033
"tags": {
10731034
"count": None,
10741035
"edges": {
@@ -1103,10 +1064,10 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
11031064
async def test_query_data_include(client, location_schema: NodeSchemaAPI, client_type):
11041065
if client_type == "standard":
11051066
node = InfrahubNode(client=client, schema=location_schema)
1106-
data = await node.generate_query_data(include=["tags"])
1067+
data = await node.generate_query_data(include=["name", "type", "tags"])
11071068
else:
11081069
node = InfrahubNodeSync(client=client, schema=location_schema)
1109-
data = node.generate_query_data(include=["tags"])
1070+
data = node.generate_query_data(include=["name", "type", "tags"])
11101071

11111072
assert data == {
11121073
"BuiltinLocation": {
@@ -1121,20 +1082,9 @@ async def test_query_data_include(client, location_schema: NodeSchemaAPI, client
11211082
"name": {
11221083
"value": None,
11231084
},
1124-
"description": {
1125-
"value": None,
1126-
},
11271085
"type": {
11281086
"value": None,
11291087
},
1130-
"primary_tag": {
1131-
"node": {
1132-
"id": None,
1133-
"hfid": None,
1134-
"display_label": None,
1135-
"__typename": None,
1136-
},
1137-
},
11381088
"tags": {
11391089
"count": None,
11401090
"edges": {
@@ -1241,6 +1191,21 @@ async def test_query_data_exclude(client, location_schema: NodeSchemaAPI, client
12411191
}
12421192

12431193

1194+
@pytest.mark.parametrize("client_type", client_types)
1195+
async def test_query_data_include_exclude(client, location_schema: NodeSchemaAPI, client_type):
1196+
if client_type == "standard":
1197+
node = InfrahubNode(client=client, schema=location_schema)
1198+
1199+
with pytest.raises(ValueError) as exc:
1200+
await node.generate_query_data(include=["name", "type"], exclude=["description"], property=True)
1201+
assert "include and exclude are exclusive" in str(exc.value)
1202+
else:
1203+
node = InfrahubNodeSync(client=client, schema=location_schema)
1204+
with pytest.raises(ValueError) as exc:
1205+
node.generate_query_data(include=["name", "type", "tags"], exclude=["description"], property=True)
1206+
assert "include and exclude are exclusive" in str(exc.value)
1207+
1208+
12441209
@pytest.mark.parametrize("client_type", client_types)
12451210
async def test_create_input_data(client, location_schema: NodeSchemaAPI, client_type):
12461211
data = {"name": {"value": "JFK1"}, "description": {"value": "JFK Airport"}, "type": {"value": "SITE"}}

0 commit comments

Comments
 (0)