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_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 context = ctx.data_unchecked::(); let services_register = ctx.data_unchecked::(); let created_item = services_register .item_service .add_item(context, item) .await?; Ok(CreatedItem { id: created_item.id, }) } async fn create_project( &self, ctx: &Context<'_>, request: CreateProjectMutation, ) -> anyhow::Result { let context = ctx.data_unchecked::(); let services_register = ctx.data_unchecked::(); let project = services_register .project_service .create_project(context, request) .await?; Ok(project) } } pub struct QueryRoot; #[Object] impl QueryRoot { async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result { let context = ctx.data_unchecked::(); let item = ctx .data_unchecked::() .item_service .get_item(context, query) .await?; Ok(Item::from(item)) } async fn get_items( &self, ctx: &Context<'_>, query: GetItemsQuery, ) -> anyhow::Result> { let context = ctx.data_unchecked::(); let items = ctx .data_unchecked::() .item_service .get_items(context, query) .await?; Ok(items.iter().map(|i| Item::from(i.clone())).collect()) } // Projects async fn get_project( &self, ctx: &Context<'_>, query: GetProjectQuery, ) -> anyhow::Result { let context = ctx.data_unchecked::(); ctx.data_unchecked::() .project_service .get_project(context, query) .await } async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result> { let context = ctx.data_unchecked::(); ctx.data_unchecked::() .project_service .get_projects(context) .await } }