como/como_gql/src/graphql.rs

104 lines
2.7 KiB
Rust
Raw Normal View History

use crate::items::{CreatedItem, Item};
2022-10-04 11:07:14 +02:00
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;
2022-10-04 11:06:48 +02:00
use como_infrastructure::register::ServiceRegister;
2022-10-04 11:07:14 +02:00
2022-10-04 12:06:00 +02:00
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
2022-10-02 14:15:45 +02:00
pub struct MutationRoot;
#[Object]
impl MutationRoot {
2022-10-03 23:00:31 +02:00
async fn create_item(
&self,
ctx: &Context<'_>,
item: CreateItemDto,
2022-10-04 22:39:57 +02:00
) -> anyhow::Result<CreatedItem> {
let user = ctx.data_unchecked::<User>();
2022-10-04 11:06:48 +02:00
let services_register = ctx.data_unchecked::<ServiceRegister>();
let created_item = services_register.item_service.add_item(item, user).await?;
2022-10-04 11:06:48 +02:00
2022-10-04 22:39:57 +02:00
Ok(CreatedItem {
id: created_item.id,
})
2022-10-03 23:00:31 +02:00
}
async fn create_project(
&self,
ctx: &Context<'_>,
request: CreateProjectMutation,
) -> anyhow::Result<ProjectDto> {
let user = ctx.data_unchecked::<User>();
let services_register = ctx.data_unchecked::<ServiceRegister>();
let project = services_register
.project_service
.create_project(request, user)
.await?;
Ok(project)
}
2022-10-02 14:15:45 +02:00
}
2022-10-02 12:12:08 +02:00
pub struct QueryRoot;
#[Object]
impl QueryRoot {
2022-10-04 22:39:57 +02:00
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
let user = ctx.data_unchecked::<User>();
2022-10-04 22:39:57 +02:00
let item = ctx
.data_unchecked::<ServiceRegister>()
.item_service
.get_item(query, user)
2022-10-04 22:39:57 +02:00
.await?;
Ok(Item::from(item))
}
async fn get_items(
&self,
ctx: &Context<'_>,
query: GetItemsQuery,
) -> anyhow::Result<Vec<Item>> {
let user = ctx.data_unchecked::<User>();
2022-10-04 22:39:57 +02:00
let items = ctx
.data_unchecked::<ServiceRegister>()
.item_service
.get_items(query, user)
2022-10-04 22:39:57 +02:00
.await?;
Ok(items.iter().map(|i| Item::from(i.clone())).collect())
}
// Projects
async fn get_project(
&self,
ctx: &Context<'_>,
query: GetProjectQuery,
) -> anyhow::Result<ProjectDto> {
ctx.data_unchecked::<ServiceRegister>()
.project_service
.get_project(query)
.await
}
async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<ProjectDto>> {
let user = ctx.data_unchecked::<User>();
2022-10-04 22:39:57 +02:00
ctx.data_unchecked::<ServiceRegister>()
.project_service
.get_projects(user)
2022-10-04 22:39:57 +02:00
.await
2022-10-04 11:06:48 +02:00
}
2022-10-02 12:12:08 +02:00
}