Skip to content

Commit fb52237

Browse files
committed
Add ability to order results by metadata created_at or updated_at
1 parent aa0164a commit fb52237

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

changelog/+86c0992a.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ability to order nodes by metadata created_at or updated_at fields

infrahub_sdk/enums.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from enum import Enum
2+
3+
4+
class OrderDirection(str, Enum):
5+
ASC = "ASC"
6+
DESC = "DESC"

infrahub_sdk/graphql/renderers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from pydantic import BaseModel
99

10+
from infrahub_sdk.types import Order
11+
1012
from .constants import VARIABLE_TYPE_MAPPING
1113

1214

@@ -53,6 +55,16 @@ def convert_to_graphql_as_string(value: Any, convert_enum: bool = False) -> str:
5355
if isinstance(value, list):
5456
values_as_string = [convert_to_graphql_as_string(value=item, convert_enum=convert_enum) for item in value]
5557
return "[" + ", ".join(values_as_string) + "]"
58+
if isinstance(value, Order):
59+
data = value.model_dump(exclude_none=True)
60+
return (
61+
"{ "
62+
+ ", ".join(
63+
f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
64+
for key, val in data.items()
65+
)
66+
+ " }"
67+
)
5668
if isinstance(value, BaseModel):
5769
data = value.model_dump()
5870
return (
@@ -63,6 +75,15 @@ def convert_to_graphql_as_string(value: Any, convert_enum: bool = False) -> str:
6375
)
6476
+ " }"
6577
)
78+
if isinstance(value, dict):
79+
return (
80+
"{ "
81+
+ ", ".join(
82+
f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
83+
for key, val in value.items()
84+
)
85+
+ " }"
86+
)
6687

6788
return str(value)
6889

infrahub_sdk/types.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from logging import Logger
55
from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
66

7-
from pydantic import BaseModel
7+
from pydantic import BaseModel, Field, model_validator
8+
9+
from infrahub_sdk.enums import OrderDirection # noqa: TC001
810

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

7072

73+
class NodeMetaOrder(BaseModel):
74+
created_at: OrderDirection | None = None
75+
updated_at: OrderDirection | None = None
76+
77+
@model_validator(mode="after")
78+
def validate_selection(self) -> NodeMetaOrder:
79+
if self.created_at and self.updated_at:
80+
raise ValueError("'created_at' and 'updated_at' are mutually exclusive")
81+
return self
82+
83+
7184
class Order(BaseModel):
72-
disable: bool | None = None
85+
disable: bool | None = Field(
86+
default=None, description="Disable default ordering, can be used to improve performance"
87+
)
88+
node_metadata: NodeMetaOrder | None = Field(default=None, description="Order by node meta fields")

0 commit comments

Comments
 (0)