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

feature: add count method to InfrahubClient #158

Closed
wvandeun opened this issue Nov 29, 2024 · 1 comment · Fixed by #225
Closed

feature: add count method to InfrahubClient #158

wvandeun opened this issue Nov 29, 2024 · 1 comment · Fixed by #225
Assignees
Labels
type/feature New feature or request

Comments

@wvandeun
Copy link
Contributor

Component

Python SDK

Describe the Feature Request

Add a count method that allows you to count the kind of objects that match your query:

from infrahub_sdk import InfrahubClient
client = InfrahubClient()
num_devices = await client.count(kind="InfraDevice", site__name__value="alt1")

Describe the Use Case

In scenarios where I would need to count the amount of objects that match a query, I have 2 options today:

  • retrieve the objects and take the length of the resulting list, which could be expensive, because we would automatically fetch attributes and relationships of cardinality one
  • construct a raw GraphQL query

The above approaches are not ideal and we could make this more simple and less expensive.

Additional Information

No response

@wvandeun wvandeun added the type/feature New feature or request label Nov 29, 2024
@wvandeun
Copy link
Contributor Author

wvandeun commented Dec 2, 2024

Today, we can achieve the same using a GraphQL query:

from infrahub_sdk import InfrahubClientSync
client = InfrahubClientSync()
query = "query { BuiltinTag { count }}"
resp = client.execute_graphql(query=query)
print(resp["BuiltinTag"]["count"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants