16
crates/como_gql/Cargo.toml
Normal file
16
crates/como_gql/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "como_gql"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
como_core.workspace = true
|
||||
como_domain.workspace = true
|
||||
como_infrastructure.workspace = true
|
||||
|
||||
anyhow.workspace = true
|
||||
async-trait.workspace = true
|
||||
async-graphql.workspace = true
|
||||
uuid.workspace = true
|
94
crates/como_gql/src/graphql.rs
Normal file
94
crates/como_gql/src/graphql.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
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())
|
||||
}
|
||||
}
|
80
crates/como_gql/src/items.rs
Normal file
80
crates/como_gql/src/items.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
use crate::common::*;
|
||||
use async_graphql::{Context, Object};
|
||||
use como_domain::{
|
||||
item::{queries::GetItemQuery, ItemDto, ItemState},
|
||||
projects::queries::GetProjectQuery,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::projects::Project;
|
||||
|
||||
pub struct CreatedItem {
|
||||
pub id: Uuid,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl CreatedItem {
|
||||
pub async fn item(&self, ctx: &Context<'_>) -> anyhow::Result<Item> {
|
||||
let item = item_service(ctx)
|
||||
.get_item(get_domain_context(ctx), GetItemQuery { item_id: self.id })
|
||||
.await?;
|
||||
|
||||
Ok(item.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Item {
|
||||
pub id: Uuid,
|
||||
pub title: String,
|
||||
pub description: Option<String>,
|
||||
pub state: ItemState,
|
||||
pub project_id: Uuid,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl Item {
|
||||
pub async fn id(&self, _ctx: &Context<'_>) -> anyhow::Result<Uuid> {
|
||||
return Ok(self.id);
|
||||
}
|
||||
|
||||
pub async fn title(&self, _ctx: &Context<'_>) -> anyhow::Result<String> {
|
||||
return Ok(self.title.clone());
|
||||
}
|
||||
|
||||
pub async fn description(&self, _ctx: &Context<'_>) -> anyhow::Result<Option<String>> {
|
||||
return Ok(self.description.clone());
|
||||
}
|
||||
|
||||
pub async fn state(&self, _ctx: &Context<'_>) -> anyhow::Result<ItemState> {
|
||||
return Ok(self.state);
|
||||
}
|
||||
|
||||
pub async fn project(&self, ctx: &Context<'_>) -> anyhow::Result<Project> {
|
||||
let project = project_service(ctx)
|
||||
.get_project(
|
||||
get_domain_context(ctx),
|
||||
GetProjectQuery {
|
||||
project_id: self.project_id,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(project.into())
|
||||
}
|
||||
|
||||
pub async fn project_id(&self, _ctx: &Context<'_>) -> anyhow::Result<Uuid> {
|
||||
return Ok(self.project_id);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ItemDto> for Item {
|
||||
fn from(dto: ItemDto) -> Self {
|
||||
Self {
|
||||
id: dto.id,
|
||||
title: dto.title,
|
||||
description: dto.description,
|
||||
state: dto.state,
|
||||
project_id: dto.project_id,
|
||||
}
|
||||
}
|
||||
}
|
33
crates/como_gql/src/lib.rs
Normal file
33
crates/como_gql/src/lib.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
pub mod graphql;
|
||||
mod items;
|
||||
mod projects;
|
||||
|
||||
pub mod common {
|
||||
use async_graphql::Context;
|
||||
use como_core::items::DynItemService;
|
||||
use como_core::projects::DynProjectService;
|
||||
use como_infrastructure::register::ServiceRegister;
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn get_domain_context<'a>(ctx: &Context<'a>) -> &'a como_domain::Context {
|
||||
ctx.data_unchecked::<como_domain::Context>()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[inline(always)]
|
||||
pub(crate) fn get_service_register<'a>(ctx: &Context<'a>) -> &'a ServiceRegister {
|
||||
ctx.data_unchecked::<ServiceRegister>()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn project_service<'a>(ctx: &Context<'a>) -> DynProjectService {
|
||||
ctx.data_unchecked::<ServiceRegister>()
|
||||
.project_service
|
||||
.clone()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn item_service<'a>(ctx: &Context<'a>) -> DynItemService {
|
||||
ctx.data_unchecked::<ServiceRegister>().item_service.clone()
|
||||
}
|
||||
}
|
48
crates/como_gql/src/projects.rs
Normal file
48
crates/como_gql/src/projects.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::common::*;
|
||||
use async_graphql::{Context, Object};
|
||||
use como_domain::projects::ProjectDto;
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::items::Item;
|
||||
|
||||
pub struct Project {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl Project {
|
||||
async fn id(&self) -> &Uuid {
|
||||
&self.id
|
||||
}
|
||||
|
||||
async fn name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
|
||||
async fn items(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Item>> {
|
||||
let items = item_service(ctx)
|
||||
.get_items(
|
||||
get_domain_context(ctx),
|
||||
como_domain::item::queries::GetItemsQuery {
|
||||
project_id: self.id,
|
||||
},
|
||||
)
|
||||
.await?
|
||||
.iter()
|
||||
.map(|i| Item::from(i.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(items)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ProjectDto> for Project {
|
||||
fn from(dto: ProjectDto) -> Self {
|
||||
Self {
|
||||
id: dto.id,
|
||||
name: dto.name,
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user