# Python from typing import Optional, List, cast from uuid import UUID import strawberry import strawberry_django as sd from strawberry import Info from asgiref.sync import sync_to_async from core.graphql.types.session import ServiceSessionType, ProjectSessionType from core.graphql.filters.session import ServiceSessionFilter, ProjectSessionFilter from core.models.session import ServiceSession, ProjectSession @strawberry.type class Query: service_session: Optional[ServiceSessionType] = sd.node() service_sessions: List[ServiceSessionType] = sd.field(filters=ServiceSessionFilter) @strawberry.field(description="Get the active service session for a given service") async def active_service_session(self, service_id: UUID, info: Info) -> Optional[ServiceSessionType]: def fetch(): qs = ( ServiceSession.objects .select_related("service", "account", "account_address", "customer", "scope") .prefetch_related("completed_tasks") ) return qs.filter(service_id=service_id, end__isnull=True).first() obj = await sync_to_async(fetch, thread_sensitive=True)() return cast(Optional[ServiceSessionType], obj) project_session: Optional[ProjectSessionType] = sd.node() project_sessions: List[ProjectSessionType] = sd.field(filters=ProjectSessionFilter) @strawberry.field(description="Get the active project session for a given project") async def active_project_session(self, project_id: UUID, info: Info) -> Optional[ProjectSessionType]: def fetch(): qs = ( ProjectSession.objects .select_related("project", "account", "account_address", "customer", "scope") .prefetch_related("completed_tasks") ) return qs.filter(project_id=project_id, end__isnull=True).first() obj = await sync_to_async(fetch, thread_sensitive=True)() return cast(Optional[ProjectSessionType], obj)