Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/+86c0992a.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ability to order nodes by metadata created_at or updated_at fields
6 changes: 6 additions & 0 deletions infrahub_sdk/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class OrderDirection(str, Enum):
ASC = "ASC"
DESC = "DESC"
21 changes: 21 additions & 0 deletions infrahub_sdk/graphql/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from pydantic import BaseModel

from infrahub_sdk.types import Order

from .constants import VARIABLE_TYPE_MAPPING


Expand Down Expand Up @@ -53,6 +55,16 @@ def convert_to_graphql_as_string(value: Any, convert_enum: bool = False) -> str:
if isinstance(value, list):
values_as_string = [convert_to_graphql_as_string(value=item, convert_enum=convert_enum) for item in value]
return "[" + ", ".join(values_as_string) + "]"
if isinstance(value, Order):
data = value.model_dump(exclude_none=True)
return (
"{ "
+ ", ".join(
f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
for key, val in data.items()
)
+ " }"
)
if isinstance(value, BaseModel):
data = value.model_dump()
return (
Expand All @@ -63,6 +75,15 @@ def convert_to_graphql_as_string(value: Any, convert_enum: bool = False) -> str:
)
+ " }"
)
if isinstance(value, dict):
return (
"{ "
+ ", ".join(
f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
for key, val in value.items()
)
+ " }"
)

return str(value)

Expand Down
20 changes: 18 additions & 2 deletions infrahub_sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from logging import Logger
from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable

from pydantic import BaseModel
from pydantic import BaseModel, Field, model_validator

from infrahub_sdk.enums import OrderDirection # noqa: TC001

if TYPE_CHECKING:
import httpx
Expand Down Expand Up @@ -68,5 +70,19 @@ def exception(self, event: str | None = None, *args: Any, **kw: Any) -> Any:
InfrahubLoggers = InfrahubLogger | Logger


class NodeMetaOrder(BaseModel):
created_at: OrderDirection | None = None
updated_at: OrderDirection | None = None

@model_validator(mode="after")
def validate_selection(self) -> NodeMetaOrder:
if self.created_at and self.updated_at:
raise ValueError("'created_at' and 'updated_at' are mutually exclusive")
return self


class Order(BaseModel):
disable: bool | None = None
disable: bool | None = Field(
default=None, description="Disable default ordering, can be used to improve performance"
)
node_metadata: NodeMetaOrder | None = Field(default=None, description="Order by node meta fields")