use async_graphql::{Context, EmptySubscription, Object, Schema}; use como_domain::{ item::{ queries::{GetItemQuery, GetItemsQuery}, requests::CreateItemDto, }, projects::{ queries::{GetProjectQuery, GetProjectsQuery}, ProjectDto, }, }; use como_infrastructure::register::ServiceRegister; use crate::items::{CreatedItem, Item}; 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, }) } } pub struct QueryRoot; #[Object] impl QueryRoot { // Items 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<'_>, query: GetProjectsQuery, ) -> anyhow::Result> { ctx.data_unchecked::() .project_service .get_projects(query) .await } }