Skip to content

Commit 74bad3b

Browse files
committed
Change behavior of include to only include the requested fields
1 parent c3315c7 commit 74bad3b

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
@@ -991,10 +991,10 @@ async def test_query_data_generic_fragment(clients, mock_schema_query_02, client
991991
async def test_query_data_include_property(client, location_schema: NodeSchemaAPI, client_type):
992992
if client_type == "standard":
993993
node = InfrahubNode(client=client, schema=location_schema)
994-
data = await node.generate_query_data(include=["tags"], property=True)
994+
data = await node.generate_query_data(include=["name", "type", "tags"], property=True)
995995
else:
996996
node = InfrahubNodeSync(client=client, schema=location_schema)
997-
data = node.generate_query_data(include=["tags"], property=True)
997+
data = node.generate_query_data(include=["name", "type", "tags"], property=True)
998998

999999
assert data == {
10001000
"BuiltinLocation": {
@@ -1023,23 +1023,6 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
10231023
},
10241024
"value": None,
10251025
},
1026-
"description": {
1027-
"is_default": None,
1028-
"is_from_profile": None,
1029-
"is_protected": None,
1030-
"is_visible": None,
1031-
"owner": {
1032-
"__typename": None,
1033-
"display_label": None,
1034-
"id": None,
1035-
},
1036-
"source": {
1037-
"__typename": None,
1038-
"display_label": None,
1039-
"id": None,
1040-
},
1041-
"value": None,
1042-
},
10431026
"type": {
10441027
"is_default": None,
10451028
"is_from_profile": None,
@@ -1057,28 +1040,6 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
10571040
},
10581041
"value": None,
10591042
},
1060-
"primary_tag": {
1061-
"properties": {
1062-
"is_protected": None,
1063-
"is_visible": None,
1064-
"owner": {
1065-
"__typename": None,
1066-
"display_label": None,
1067-
"id": None,
1068-
},
1069-
"source": {
1070-
"__typename": None,
1071-
"display_label": None,
1072-
"id": None,
1073-
},
1074-
},
1075-
"node": {
1076-
"id": None,
1077-
"hfid": None,
1078-
"display_label": None,
1079-
"__typename": None,
1080-
},
1081-
},
10821043
"tags": {
10831044
"count": None,
10841045
"edges": {
@@ -1113,10 +1074,10 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
11131074
async def test_query_data_include(client, location_schema: NodeSchemaAPI, client_type):
11141075
if client_type == "standard":
11151076
node = InfrahubNode(client=client, schema=location_schema)
1116-
data = await node.generate_query_data(include=["tags"])
1077+
data = await node.generate_query_data(include=["name", "type", "tags"])
11171078
else:
11181079
node = InfrahubNodeSync(client=client, schema=location_schema)
1119-
data = node.generate_query_data(include=["tags"])
1080+
data = node.generate_query_data(include=["name", "type", "tags"])
11201081

11211082
assert data == {
11221083
"BuiltinLocation": {
@@ -1131,20 +1092,9 @@ async def test_query_data_include(client, location_schema: NodeSchemaAPI, client
11311092
"name": {
11321093
"value": None,
11331094
},
1134-
"description": {
1135-
"value": None,
1136-
},
11371095
"type": {
11381096
"value": None,
11391097
},
1140-
"primary_tag": {
1141-
"node": {
1142-
"id": None,
1143-
"hfid": None,
1144-
"display_label": None,
1145-
"__typename": None,
1146-
},
1147-
},
11481098
"tags": {
11491099
"count": None,
11501100
"edges": {
@@ -1251,6 +1201,21 @@ async def test_query_data_exclude(client, location_schema: NodeSchemaAPI, client
12511201
}
12521202

12531203

1204+
@pytest.mark.parametrize("client_type", client_types)
1205+
async def test_query_data_include_exclude(client, location_schema: NodeSchemaAPI, client_type):
1206+
if client_type == "standard":
1207+
node = InfrahubNode(client=client, schema=location_schema)
1208+
1209+
with pytest.raises(ValueError) as exc:
1210+
await node.generate_query_data(include=["name", "type"], exclude=["description"], property=True)
1211+
assert "include and exclude are exclusive" in str(exc.value)
1212+
else:
1213+
node = InfrahubNodeSync(client=client, schema=location_schema)
1214+
with pytest.raises(ValueError) as exc:
1215+
node.generate_query_data(include=["name", "type", "tags"], exclude=["description"], property=True)
1216+
assert "include and exclude are exclusive" in str(exc.value)
1217+
1218+
12541219
@pytest.mark.parametrize("client_type", client_types)
12551220
async def test_create_input_data(client, location_schema: NodeSchemaAPI, client_type):
12561221
data = {"name": {"value": "JFK1"}, "description": {"value": "JFK Airport"}, "type": {"value": "SITE"}}

0 commit comments

Comments
 (0)