Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise better error for invalid HFID relationship #5370

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions backend/infrahub/core/relationship/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
31 changes: 31 additions & 0 deletions backend/tests/unit/graphql/test_mutation_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down
1 change: 1 addition & 0 deletions changelog/5360.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise a better error when trying to resolve an invalid HFID for a relationship
Loading