-
Notifications
You must be signed in to change notification settings - Fork 6
Fix filters include parameter
#513
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -540,6 +540,7 @@ async def _process_nodes_and_relationships( | |
| schema_kind: str, | ||
| branch: str, | ||
| prefetch_relationships: bool, | ||
| include: list[str] | None, | ||
| timeout: int | None = None, | ||
| ) -> ProcessRelationsNode: | ||
| """Processes InfrahubNode and their Relationships from the GraphQL query response. | ||
|
|
@@ -557,16 +558,22 @@ async def _process_nodes_and_relationships( | |
| - 'related_nodes': A list of InfrahubNode objects representing the related nodes | ||
| """ | ||
|
|
||
| # Ideally, include and relationships wouldn't be parameters of this method, they should only | ||
| # be used to build the request for the server, and this method would build node according to the response. | ||
|
|
||
| nodes: list[InfrahubNode] = [] | ||
| related_nodes: list[InfrahubNode] = [] | ||
|
|
||
| for item in response.get(schema_kind, {}).get("edges", []): | ||
| node = await InfrahubNode.from_graphql(client=self, branch=branch, data=item, timeout=timeout) | ||
| nodes.append(node) | ||
|
|
||
| if prefetch_relationships: | ||
| if prefetch_relationships or include is not None: | ||
|
||
| await node._process_relationships( | ||
| node_data=item, branch=branch, related_nodes=related_nodes, timeout=timeout | ||
| node_data=item, | ||
| branch=branch, | ||
| related_nodes=related_nodes, | ||
| timeout=timeout, | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return ProcessRelationsNode(nodes=nodes, related_nodes=related_nodes) | ||
|
|
@@ -816,6 +823,7 @@ async def process_page(page_offset: int, page_number: int) -> tuple[dict, Proces | |
| branch=branch, | ||
| prefetch_relationships=prefetch_relationships, | ||
| timeout=timeout, | ||
| include=include, | ||
| ) | ||
| return response, process_result | ||
|
|
||
|
|
@@ -1831,6 +1839,7 @@ def _process_nodes_and_relationships( | |
| schema_kind: str, | ||
| branch: str, | ||
| prefetch_relationships: bool, | ||
| include: list[str] | None, | ||
| timeout: int | None = None, | ||
| ) -> ProcessRelationsNodeSync: | ||
| """Processes InfrahubNodeSync and their Relationships from the GraphQL query response. | ||
|
|
@@ -1855,7 +1864,7 @@ def _process_nodes_and_relationships( | |
| node = InfrahubNodeSync.from_graphql(client=self, branch=branch, data=item, timeout=timeout) | ||
| nodes.append(node) | ||
|
|
||
| if prefetch_relationships: | ||
| if prefetch_relationships or include is not None: | ||
|
||
| node._process_relationships(node_data=item, branch=branch, related_nodes=related_nodes, timeout=timeout) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return ProcessRelationsNodeSync(nodes=nodes, related_nodes=related_nodes) | ||
|
|
@@ -1980,6 +1989,7 @@ def process_page(page_offset: int, page_number: int) -> tuple[dict, ProcessRelat | |
| branch=branch, | ||
| prefetch_relationships=prefetch_relationships, | ||
| timeout=timeout, | ||
| include=include, | ||
| ) | ||
| return response, process_result | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -742,12 +742,11 @@ async def generate_query_data_node( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_data: dict[str, Any] = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if rel_schema and prefetch_relationships: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| should_fetch_relationship = prefetch_relationships or (include is not None and rel_name in include) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was something like this I was thinking about in the comments above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had actually addressed these changes following Aaron's comment but didn't push them |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if rel_schema and should_fetch_relationship: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_schema = await self._client.schema.get(kind=rel_schema.peer, branch=self._branch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_node = InfrahubNode(client=self._client, schema=peer_schema, branch=self._branch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_data = await peer_node.generate_query_data_node( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| include=include, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude=exclude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| property=property, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
744
to
752
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Async nested prefetch should propagate include/exclude (filtered) to match sync semantics and avoid over/under-fetch. Currently only Apply: - peer_data = await peer_node.generate_query_data_node(
- property=property,
- )
+ # Propagate only relationship names relevant to the peer schema
+ peer_rel_names = getattr(peer_schema, "relationship_names", [r.name for r in peer_schema.relationships])
+ child_include = [n for n in (include or []) if n in peer_rel_names] or None
+ child_exclude = [n for n in (exclude or []) if n in peer_rel_names] or None
+ peer_data = await peer_node.generate_query_data_node(
+ include=child_include,
+ exclude=child_exclude,
+ property=property,
+ )📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -886,7 +885,11 @@ async def update( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| await self._process_mutation_result(mutation_name=mutation_name, response=response, timeout=timeout) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def _process_relationships( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, node_data: dict[str, Any], branch: str, related_nodes: list[InfrahubNode], timeout: int | None = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| node_data: dict[str, Any], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| branch: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| related_nodes: list[InfrahubNode], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Processes the Relationships of a InfrahubNode and add Related Nodes to a list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1363,7 +1366,8 @@ def generate_query_data_node( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_data: dict[str, Any] = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if rel_schema and prefetch_relationships: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| should_fetch_relationship = prefetch_relationships or (include is not None and rel_name in include) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if rel_schema and should_fetch_relationship: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_schema = self._client.schema.get(kind=rel_schema.peer, branch=self._branch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_node = InfrahubNodeSync(client=self._client, schema=peer_schema, branch=self._branch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peer_data = peer_node.generate_query_data_node(include=include, exclude=exclude, property=property) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this comment and I think you can do this differently and then remove the comment
what about instead of passing
includein here, you update theprefetch_relationshipsparameter to be ado_process_relationshipsBoolean flag and then update the two places where_process_nodes_and_relationshipsis called to setdo_process_relationshipsbased onprefetch_relationshipsand ifincludeis not empty (or maybe ifincludeactually includes the name of a relationship)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is about the fact that we should rely on these parameters only to fetch the data from the server and not while building the nodes client side.
The current logic is:
The comment means that we should get rid of the second if condition, ie, while building the nodes we should just build it ouf of the payload that we previously fetched. Does this make sense? I tried to do so but it led to some complicated bugs that should be treated out of this PR if we want to clean that.
I still made a minor change following up your comment so that include checks on whether it contains a relationship name.
Using a
do_prefetch_relationshipsas you suggested might not fit well here because we have access to node relationships once it is built inside the_process_nodes_and_relationshipsmethod, and we need relationships names to check whetherincludecontains some.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just suggesting making the below small changes. I didn't realize that this comment was related to existing functionality. This sounds like something that deserves its own issue in GH/Jira instead of a comment that will be ignored / forgotten
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understood that this was the change you suggested, and I think we cannot do that because
do_process_relationships=prefetch_relationships or include is not Noneis missing checking relationships names as you nicely pointed out, and we need the node being built to do that, cf my previous commentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created IHS-161