|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from collections.abc import Iterable |
| 6 | +from datetime import datetime |
| 7 | +from enum import StrEnum |
| 8 | +from typing import Self, override |
| 9 | + |
5 | 10 | import strawberry |
| 11 | +from strawberry import ID, Info |
| 12 | +from strawberry.relay import Node, NodeID |
6 | 13 |
|
| 14 | +from ai.backend.manager.api.gql.base import OrderDirection, StringFilter |
7 | 15 | from ai.backend.manager.api.gql.rbac.types.entity_node import EntityNode |
| 16 | +from ai.backend.manager.api.gql.rbac.types.permission import EntityTypeGQL |
| 17 | +from ai.backend.manager.api.gql.types import GQLFilter, GQLOrderBy, StrawberryGQLContext |
| 18 | +from ai.backend.manager.data.permission.association_scopes_entities import ( |
| 19 | + AssociationScopesEntitiesData, |
| 20 | +) |
| 21 | +from ai.backend.manager.repositories.base import QueryCondition, QueryOrder |
| 22 | +from ai.backend.manager.repositories.permission_controller.options import ( |
| 23 | + EntityScopeConditions, |
| 24 | + EntityScopeOrders, |
| 25 | +) |
| 26 | + |
| 27 | +# ==================== Enums ==================== |
| 28 | + |
| 29 | + |
| 30 | +@strawberry.enum(description="Added in 26.3.0. Entity ordering field") |
| 31 | +class EntityOrderField(StrEnum): |
| 32 | + ENTITY_TYPE = "entity_type" |
| 33 | + REGISTERED_AT = "registered_at" |
| 34 | + |
| 35 | + |
| 36 | +# ==================== Node Types ==================== |
| 37 | + |
| 38 | + |
| 39 | +@strawberry.type( |
| 40 | + name="EntityRef", |
| 41 | + description="Added in 26.3.0. Entity reference from the association_scopes_entities table", |
| 42 | +) |
| 43 | +class EntityRefGQL(Node): |
| 44 | + id: NodeID[str] |
| 45 | + scope_type: EntityTypeGQL |
| 46 | + scope_id: str |
| 47 | + entity_type: EntityTypeGQL |
| 48 | + entity_id: str |
| 49 | + registered_at: datetime |
| 50 | + |
| 51 | + @strawberry.field( # type: ignore[misc] |
| 52 | + description="The resolved entity object." |
| 53 | + ) |
| 54 | + async def entity( |
| 55 | + self, |
| 56 | + *, |
| 57 | + info: Info[StrawberryGQLContext], |
| 58 | + ) -> EntityNode | None: |
| 59 | + raise NotImplementedError |
| 60 | + |
| 61 | + @classmethod |
| 62 | + async def resolve_nodes( # type: ignore[override] |
| 63 | + cls, |
| 64 | + *, |
| 65 | + info: Info[StrawberryGQLContext], |
| 66 | + node_ids: Iterable[str], |
| 67 | + required: bool = False, |
| 68 | + ) -> Iterable[Self | None]: |
| 69 | + raise NotImplementedError |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def from_dataclass(cls, data: AssociationScopesEntitiesData) -> Self: |
| 73 | + return cls( |
| 74 | + id=ID(str(data.id)), |
| 75 | + scope_type=EntityTypeGQL.from_scope_type(data.scope_id.scope_type), |
| 76 | + scope_id=data.scope_id.scope_id, |
| 77 | + entity_type=EntityTypeGQL.from_internal(data.object_id.entity_type), |
| 78 | + entity_id=data.object_id.entity_id, |
| 79 | + registered_at=data.registered_at, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +# ==================== Filter Types ==================== |
| 84 | + |
| 85 | + |
| 86 | +@strawberry.input(description="Added in 26.3.0. Filter for entity associations") |
| 87 | +class EntityFilter(GQLFilter): |
| 88 | + entity_type: EntityTypeGQL | None = None |
| 89 | + entity_id: StringFilter | None = None |
| 90 | + |
| 91 | + @override |
| 92 | + def build_conditions(self) -> list[QueryCondition]: |
| 93 | + conditions: list[QueryCondition] = [] |
| 94 | + |
| 95 | + if self.entity_type is not None: |
| 96 | + conditions.append(EntityScopeConditions.by_entity_type(self.entity_type.to_internal())) |
| 97 | + |
| 98 | + if self.entity_id is not None: |
| 99 | + condition = self.entity_id.build_query_condition( |
| 100 | + contains_factory=EntityScopeConditions.by_entity_id_contains, |
| 101 | + equals_factory=EntityScopeConditions.by_entity_id_equals, |
| 102 | + starts_with_factory=EntityScopeConditions.by_entity_id_starts_with, |
| 103 | + ends_with_factory=EntityScopeConditions.by_entity_id_ends_with, |
| 104 | + ) |
| 105 | + if condition: |
| 106 | + conditions.append(condition) |
| 107 | + |
| 108 | + return conditions |
| 109 | + |
| 110 | + |
| 111 | +# ==================== OrderBy Types ==================== |
| 112 | + |
| 113 | + |
| 114 | +@strawberry.input(description="Added in 26.3.0. Order by specification for entity associations") |
| 115 | +class EntityOrderBy(GQLOrderBy): |
| 116 | + field: EntityOrderField |
| 117 | + direction: OrderDirection = OrderDirection.DESC |
| 118 | + |
| 119 | + @override |
| 120 | + def to_query_order(self) -> QueryOrder: |
| 121 | + ascending = self.direction == OrderDirection.ASC |
| 122 | + match self.field: |
| 123 | + case EntityOrderField.ENTITY_TYPE: |
| 124 | + return EntityScopeOrders.entity_type(ascending) |
| 125 | + case EntityOrderField.REGISTERED_AT: |
| 126 | + return EntityScopeOrders.registered_at(ascending) |
| 127 | + |
| 128 | + |
| 129 | +# ==================== Connection Types ==================== |
8 | 130 |
|
9 | 131 |
|
10 | 132 | @strawberry.type(description="Added in 26.3.0. Entity edge") |
11 | 133 | class EntityEdge: |
12 | | - node: EntityNode |
| 134 | + node: EntityRefGQL |
13 | 135 | cursor: str |
14 | 136 |
|
15 | 137 |
|
|
0 commit comments