use crate::items::{CreatedItem, Item}; use async_graphql::{Context, EmptySubscription, Object, Schema}; use como_domain::item::queries::{GetItemQuery, GetItemsQuery}; use como_domain::item::requests::CreateItemDto; use como_domain::projects::mutation::CreateProjectMutation; use como_domain::projects::queries::GetProjectQuery; use como_domain::projects::ProjectDto; use como_domain::users::User; use como_infrastructure::register::ServiceRegister; pub type ComoSchema = Schema; pub struct MutationRoot; #[Object] impl MutationRoot { async fn create_item( &self, ctx: &Context<'_>, item: CreateItemDto, ) -> anyhow::Result { let services_register = ctx.data_unchecked::(); let created_item = services_register.item_service.add_item(item).await?; Ok(CreatedItem { id: created_item.id, }) } async fn create_project( &self, ctx: &Context<'_>, request: CreateProjectMutation, ) -> anyhow::Result { let user = ctx.data_unchecked::(); let services_register = ctx.data_unchecked::(); let project = services_register .project_service .create_project(request, user) .await?; Ok(project) } } pub struct QueryRoot; #[Object] impl QueryRoot { async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result { let item = ctx .data_unchecked::() .item_service .get_item(query) .await?; Ok(Item::from(item)) } async fn get_items( &self, ctx: &Context<'_>, query: GetItemsQuery, ) -> anyhow::Result> { let items = ctx .data_unchecked::() .item_service .get_items(query) .await?; Ok(items.iter().map(|i| Item::from(i.clone())).collect()) } // Projects async fn get_project( &self, ctx: &Context<'_>, query: GetProjectQuery, ) -> anyhow::Result { ctx.data_unchecked::() .project_service .get_project(query) .await } async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result> { let user = ctx.data_unchecked::(); ctx.data_unchecked::() .project_service .get_projects(user) .await } }