como/como_gql/src/graphql.rs
kjuulh 3e5309e1e6
feat: update scripts to use new cuddle
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-06-10 14:35:03 +02:00

95 lines
2.6 KiB
Rust

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