8000 Add `common_parent` relationship attribute by gmazoyer · Pull Request #6626 · opsmill/infrahub · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add common_parent relationship attribute #6626

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

Merged
merged 10 commits into from
Jun 5, 2025

Conversation

gmazoyer
Copy link
Contributor
@gmazoyer gmazoyer commented Jun 4, 2025

This relationship attribute is used to specify that peers of a given
relationship must share a common parent with the source object. While
the parent of the source object is infered automatically (as there is
only one parent per object), the common_parent value must be set to
the name of the parent relationship of the peer schema.

@github-actions github-actions bot added type/documentation Improvements or additions to documentation group/backend Issue related to the backend (API Server, Git Agent) labels Jun 4, 2025
Comment on lines 410 to 438
async def _validate_peer_parents(
info: GraphQLResolveInfo, data: RelationshipNodesInput, source_node: Node, peers: dict[str, Node]
) -> None:
relationship_name = str(data.name)
rel_schema = source_node.get_schema().get_relationship(name=relationship_name)
if not rel_schema.common_parent:
return

graphql_context: GraphqlContext = info.context

source_node_parent = await source_node.get_parent_relationship_peer(
db=graphql_context.db, name=rel_schema.common_parent
)
if not source_node_parent:
raise ValidationError(f"Node {source_node.id} ({source_node.get_kind()!r}) does not have a parent peer")

parents: set[str] = {source_node_parent.id}
for peer in peers.values():
peer_parent = await peer.get_parent_relationship_peer(db=graphql_context.db, name=rel_schema.common_parent)
if not peer_parent:
raise ValidationError(f"Peer {peer.id} ({peer.get_kind()!r}) does not have a parent peer")
parents.add(peer_parent.id)

if len(parents) > 1:
raise ValidationError(
f"Cannot relate {source_node.id!r} to '{relationship_name}' peers that do not have the same parent"
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like duplicating logic from another place, but the constraint checker cannot run here for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't the RelationshipPeerParentConstraint constraint run here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure yet, that the first thing I tried. I tried running it after during the transaction of the RelationshipAdd mutation but it failed like neo4j.exceptions.ClientError: Explicit Transaction must be handled explicitly. Running it before the transaction does not work because the checker isn't able to pick up the relationships are they are not set when it runs and therefore not picking up possible issues. Might need some more thoughts on this to figure it out.

@gmazoyer gmazoyer force-pushed the gma-20250604-common-parent branch from 75c1341 to d07df47 Compare June 4, 2025 14:00
@gmazoyer gmazoyer marked this pull request as ready for review June 4, 2025 14:01
@gmazoyer gmazoyer requested review from a team as code owners June 4, 2025 14:01
Copy link
codspeed-hq bot commented Jun 4, 2025

CodSpeed Performance Report

Merging #6626 will not alter performance

Comparing gma-20250604-common-parent (f7d9fe7) with release-1.3 (014ed76)

Summary

✅ 10 untouched benchmarks

@gmazoyer gmazoyer force-pushed the gma-20250604-common-parent branch from d07df47 to d7e93ea Compare June 4, 2025 14:38
Comment on lines 410 to 438
async def _validate_peer_parents(
info: GraphQLResolveInfo, data: RelationshipNodesInput, source_node: Node, peers: dict[str, Node]
) -> None:
relationship_name = str(data.name)
rel_schema = source_node.get_schema().get_relationship(name=relationship_name)
if not rel_schema.common_parent:
return

graphql_context: GraphqlContext = info.context

source_node_parent = await source_node.get_parent_relationship_peer(
db=graphql_context.db, name=rel_schema.common_parent
)
if not source_node_parent:
raise ValidationError(f"Node {source_node.id} ({source_node.get_kind()!r}) does not have a parent peer")

parents: set[str] = {source_node_parent.id}
for peer in peers.values():
peer_parent = await peer.get_parent_relationship_peer(db=graphql_context.db, name=rel_schema.common_parent)
if not peer_parent:
raise ValidationError(f"Peer {peer.id} ({peer.get_kind()!r}) does not have a parent peer")
parents.add(peer_parent.id)

if len(parents) > 1:
raise ValidationError(
f"Cannot relate {source_node.id!r} to '{relationship_name}' peers that do not have the same parent"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't the RelationshipPeerParentConstraint constraint run here?

@gmazoyer gmazoyer force-pushed the gma-20250604-common-parent branch from 5c37d03 to 040873f Compare June 5, 2025 08:44
@@ -38,4 +38,6 @@ async def check(
relationship_manager: RelationshipManager = getattr(node, relationship_name)
await relationship_manager.fetch_relationship_ids(db=db, force_refresh=True)
for relationship_constraint in self.relationship_manager_constraints:
await relationship_constraint.check(relm=relationship_manager, node_schema=node.get_schema())
await relationship_constraint.check(
relm=relationship_manager, node_schema=node.get_schema(), node=node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we're adding the node as an argument here and we can access the schema with node.get_schema() the node_schema parameter gets a bit redundant now. But we can perhaps consider removing that as a cleanup step.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do that in a dedicated PR as it needs to change some code in other constraint checkers.

gmazoyer added 10 commits June 5, 2025 11:11
This relationship attribute is used to specify that peers of a given
relationship must share a common parent with the source object. While
the parent of the source object is infered automatically (as there is
only one parent per object), the `common_parent` value must be set to
the name of the parent relationship of the peer schema.
They will be left like that until we have proper constraint validators
in place.
@gmazoyer gmazoyer force-pushed the gma-20250604-common-parent branch from cdb48cb to f7d9fe7 Compare June 5, 2025 09:11
@gmazoyer gmazoyer merged commit 97390e1 into release-1.3 Jun 5, 2025
58 of 60 checks passed
@gmazoyer gmazoyer deleted the gma-20250604-common-parent branch June 5, 2025 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
group/backend Issue related to the backend (API Server, Git Agent) type/documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0