Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 24, 2025

GraphQL supports querying ancestors and descendants on hierarchical nodes, but this functionality was missing from the Python SDK.

Changes

Node detection and initialization (infrahub_sdk/node/node.py):

  • Detect hierarchical nodes via schema.hierarchy field
  • Initialize ancestors and descendants as RelationshipManager instances in _init_relationships()
  • Expose hierarchical fields through __getattr__()
  • Implemented for both async and sync node classes

Query generation:

  • Include ancestors/descendants in GraphQL queries when requested via include parameter or prefetch_relationships=True
  • Apply GraphQL fragments (...on NodeKind) to handle polymorphic hierarchy queries correctly

Testing (tests/unit/sdk/test_hierarchical_nodes.py):

  • Added 16 tests covering initialization, data handling, and query generation for both async/sync implementations

Usage

# Fetch a hierarchical node and include ancestors
location = await client.get(kind="InfraLocation", name__value="United States")
query_data = await location.generate_query_data_node(include=["ancestors"])

# Or include both ancestors and descendants
query_data = await location.generate_query_data_node(
    include=["ancestors", "descendants"]
)

# Access the relationship managers directly
ancestors_mgr = location.ancestors
descendants_mgr = location.descendants

The implementation mirrors the existing relationship pattern, treating ancestors/descendants as read-only many-cardinality relationships.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: /usr/bin/curl curl -LsSf REDACTED (dns block)
  • mock
    • Triggering command: /usr/bin/python3 python3 -m pytest tests/unit/sdk/test_hierarchical_nodes.py -v (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>task: add support for hierarchical nodes ancestors and descendants in the Python SDK</issue_title>
<issue_description>### Component

Python SDK

Task Description

We should add support for ancestors and descendants in the Python SDK.

In GraphQL we can execute the following GraphQL query to get the ancestors or descendants of a Node that is part of a hierarchy:

query {
  InfraCountry(name__value: "United States of America") {
    edges {
      node {
        ancestors {
          edges {
            node {
              __typename
              name {
                value
              }
            }
          }
        }
        descendants {
          edges {
            node {
              __typename
              name {
                value
              }
            }
          }
        }
      }
    }
  }
}

This functionality is currently not available in the Python SDK.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@coderabbitai
Copy link

coderabbitai bot commented Nov 24, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

- Added _hierarchy_support flag to detect hierarchical nodes
- Initialized _hierarchical_data dict for ancestors/descendants
- Created RelationshipManager instances for ancestors/descendants
- Updated __getattr__ to return hierarchical data
- Added query generation for ancestors/descendants in both async and sync versions

Co-authored-by: BeArchiTek <[email protected]>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 24, 2025

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2191f4e
Status: ✅  Deploy successful!
Preview URL: https://83ca0031.infrahub-sdk-python.pages.dev
Branch Preview URL: https://copilot-add-hierarchical-nod.infrahub-sdk-python.pages.dev

View logs

Copilot AI and others added 2 commits November 24, 2025 14:27
- Created 16 tests covering initialization, data handling, and query generation
- Tests verify ancestors and descendants are properly initialized
- Tests confirm query generation includes hierarchical fields
- Tests validate both async and sync node implementations
- All tests pass successfully

Co-authored-by: BeArchiTek <[email protected]>
- Removed redundant 'if should_fetch' checks in both async and sync implementations
- The checks were unnecessary since we already continue if should_fetch is False
- All tests continue to pass

Co-authored-by: BeArchiTek <[email protected]>
Copilot AI changed the title [WIP] Add support for hierarchical nodes ancestors and descendants Add ancestors and descendants support for hierarchical nodes Nov 24, 2025
Copilot AI requested a review from BeArchiTek November 24, 2025 14:35
Copilot finished work on behalf of BeArchiTek November 24, 2025 14:35
@BeArchiTek BeArchiTek marked this pull request as ready for review November 27, 2025 16:42
@BeArchiTek BeArchiTek requested review from a team November 27, 2025 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants