From b637a7ab8afe95038c1c2ba3d0a6cab3fccb90ad Mon Sep 17 00:00:00 2001 From: Aaron McCarty Date: Fri, 3 Jan 2025 15:22:17 -0800 Subject: [PATCH 1/2] check if this fix breaks other things --- backend/infrahub/core/relationship/model.py | 10 ++++-- .../unit/graphql/test_mutation_create.py | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/backend/infrahub/core/relationship/model.py b/backend/infrahub/core/relationship/model.py index d1c19d8d41..aeb59015a1 100644 --- a/backend/infrahub/core/relationship/model.py +++ b/backend/infrahub/core/relationship/model.py @@ -432,10 +432,14 @@ async def resolve(self, db: InfrahubDatabase) -> None: else self.schema.peer ) peer = await registry.manager.get_one_by_hfid( - db=db, hfid=self.peer_hfid, branch=self.branch, kind=kind, fields={"display_label": None} + db=db, + hfid=self.peer_hfid, + branch=self.branch, + kind=kind, + fields={"display_label": None}, + raise_on_error=True, ) - if peer: - await self.set_peer(value=peer) + await self.set_peer(value=peer) if not self.peer_id and self.from_pool and "id" in self.from_pool: pool_id = str(self.from_pool.get("id")) diff --git a/backend/tests/unit/graphql/test_mutation_create.py b/backend/tests/unit/graphql/test_mutation_create.py index c5505678b5..152735ea29 100644 --- a/backend/tests/unit/graphql/test_mutation_create.py +++ b/backend/tests/unit/graphql/test_mutation_create.py @@ -499,6 +499,37 @@ async def test_create_object_with_single_relationship(db: InfrahubDatabase, defa assert len(result.data["TestCarCreate"]["object"]["id"]) == 36 # length of an UUID +async def test_create_object_with_invalid_single_relationship_fails( + db: InfrahubDatabase, default_branch, hierarchical_location_schema +): + query = """ + mutation { + LocationSiteCreate( + data: { + name: { value: "NewSite" }, + parent: { hfid: ["pretend region"] } + } + ) { + ok + object { + id + } + } + } + """ + gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch) + result = await graphql( + schema=gql_params.schema, + source=query, + context_value=gql_params.context, + root_value=None, + variable_values={}, + ) + assert len(result.errors) == 1 + gql_error = result.errors[0] + assert "Unable to find the node pretend region / LocationRegion in the database." in gql_error.message + + async def test_create_object_with_single_relationship_flag_property( db: InfrahubDatabase, default_branch, car_person_schema ): From de91d3a119e92fc6e2cc1ffe3f601274d0be4454 Mon Sep 17 00:00:00 2001 From: Aaron McCarty Date: Fri, 3 Jan 2025 15:52:06 -0800 Subject: [PATCH 2/2] add changelog --- changelog/5360.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/5360.fixed.md diff --git a/changelog/5360.fixed.md b/changelog/5360.fixed.md new file mode 100644 index 0000000000..f8e522813d --- /dev/null +++ b/changelog/5360.fixed.md @@ -0,0 +1 @@ +Raise a better error when trying to resolve an invalid HFID for a relationship \ No newline at end of file