from typing import AsyncGenerator import strawberry from channels.db import database_sync_to_async from strawberry.types import Info from core.graphql.pubsub import pubsub from core.graphql.types.service import ServiceType from core.graphql.utils import _extract_id from core.models.service import Service @strawberry.type class Subscription: @strawberry.subscription(description="Subscribe to service visit creation events") async def service_created(self, info: Info) -> AsyncGenerator[ServiceType, None]: user = info.context.user if not user or not user.is_authenticated: raise PermissionError("Authentication required") async with pubsub.subscribe("service_created") as subscriber: async for payload in subscriber: service_id = await _extract_id(payload) try: instance = await database_sync_to_async(Service.objects.get)(pk=service_id) except Service.DoesNotExist: continue yield instance @strawberry.subscription(description="Subscribe to service visit updates") async def service_updated(self, info: Info) -> AsyncGenerator[ServiceType, None]: user = info.context.user if not user or not user.is_authenticated: raise PermissionError("Authentication required") async with pubsub.subscribe("service_updated") as subscriber: async for payload in subscriber: service_id = await _extract_id(payload) try: instance = await database_sync_to_async(Service.objects.get)(pk=service_id) except Service.DoesNotExist: continue yield instance @strawberry.subscription(description="Subscribe to service visit deletion events") async def service_deleted(self, info: Info) -> AsyncGenerator[strawberry.ID, None]: user = info.context.user if not user or not user.is_authenticated: raise PermissionError("Authentication required") async with pubsub.subscribe("service_deleted") as subscriber: async for payload in subscriber: service_id = await _extract_id(payload) yield strawberry.ID(service_id)