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

bug: GraphQLQueryAnalyzer complexity for depth and height doesn't consider fragments #5562

Open
ogenstad opened this issue Jan 24, 2025 · 0 comments
Labels
group/backend Issue related to the backend (API Server, Git Agent) state/backlog This issue is part of the backlog type/bug Something isn't working as expected

Comments

@ogenstad
Copy link
Contributor

Component

API Server / GraphQL, Python SDK

Infrahub version

v1.2.0a0

Current Behavior

When adding a GraphQL Query to Infrahub we currently calculate the complexity and indicate the queries height and depth. As an example the following query will have depth: 8 and height: 11 where height indicates the number of returned elements and depth indicates how deeply the query is nested (often indicating relationship lookups)

query {
  InfraDevice {
    edges {
      node {
        name {
          value
        }
        description {
          value
        }
        interfaces {
          edges {
            node {
              display_label
            }
          }
        }
      }
    }
  }
}

If you instead use fragments this calculation doesn't work, for instance:

fragment interface on InfraInterfaceL3 {
  name {
    value
  }
  member_of_groups {
    edges {
      node {
        name {
          value
        }
        members {
          edges {
            node {
              display_label
              member_of_groups {
                edges {
                  node {
                    name { value}
                    members {
                      edges { node {
                        display_label
                      }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

query MyQuery {
  InfraDevice {
    edges {
      node {
        display_label
        interfaces {
          edges {
            node {
              ...interface
            }
          }
        }
      }
    }
  }
}

This query gets reported as having depth: 7 and height: 7 indicating that we only consider the graphql query itself (MyQuery), where we should in fact also include the fragment within this evaluation.

Expected Behavior

The entire query should be included in the evaluation.

Steps to Reproduce

  1. Load the base infrastructure schema
  2. Create a CoreGraphQLQuery with the query above and observe the values of the depth and height read_only attributes

Additional Information

Something to consider is that the methods for evaluating height and depth comes from the SDK today, where we could consider moving this logic to Infrahub server to use the information already processed by the improved InfrahubGraphQLQueryAnalyzer.

Also note, currently we don't use these values for anything so it might not be something that's urgent for the moment but it's something to consider. Another aspect is that all of this would currently be queried without considering the @expand directive we added. If we keep that directive we should probably rewrite it and include it as part of the Query Analyzer and manage the update of the GraphQL Query document inline instead of doing it later where we aren't able include the added fields within the complexity calculation.

Finally the above query is also problematic today due to #5322, probably for the same reason (we extract the fields of the base query and don't include the names fragment when processing the query)

@ogenstad ogenstad added group/backend Issue related to the backend (API Server, Git Agent) type/bug Something isn't working as expected labels Jan 24, 2025
@exalate-issue-sync exalate-issue-sync bot added state/need-triage This issue needs to be triaged state/backlog This issue is part of the backlog and removed state/need-triage This issue needs to be triaged labels Jan 27, 2025
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) state/backlog This issue is part of the backlog type/bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

1 participant