with memory db
This commit is contained in:
@@ -1,8 +1,21 @@
|
||||
use async_graphql::{Context, EmptySubscription, Object, Schema};
|
||||
|
||||
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto};
|
||||
use como_domain::{
|
||||
item::{
|
||||
queries::{GetItemQuery, GetItemsQuery},
|
||||
requests::CreateItemDto,
|
||||
responses::CreatedItemDto,
|
||||
ItemDto,
|
||||
},
|
||||
projects::{
|
||||
queries::{GetProjectQuery, GetProjectsQuery},
|
||||
ProjectDto,
|
||||
},
|
||||
};
|
||||
use como_infrastructure::register::ServiceRegister;
|
||||
|
||||
use crate::items::{CreatedItem, Item};
|
||||
|
||||
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
|
||||
|
||||
pub struct MutationRoot;
|
||||
@@ -49,12 +62,14 @@ impl MutationRoot {
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
item: CreateItemDto,
|
||||
) -> anyhow::Result<CreatedItemDto> {
|
||||
) -> anyhow::Result<CreatedItem> {
|
||||
let services_register = ctx.data_unchecked::<ServiceRegister>();
|
||||
|
||||
let created_item = services_register.item_service.add_item(item).await?;
|
||||
|
||||
Ok(created_item)
|
||||
Ok(CreatedItem {
|
||||
id: created_item.id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +77,51 @@ pub struct QueryRoot;
|
||||
|
||||
#[Object]
|
||||
impl QueryRoot {
|
||||
async fn hello(&self, _ctx: &Context<'_>) -> String {
|
||||
"hello".into()
|
||||
// Items
|
||||
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
|
||||
let item = ctx
|
||||
.data_unchecked::<ServiceRegister>()
|
||||
.item_service
|
||||
.get_item(query)
|
||||
.await?;
|
||||
|
||||
Ok(Item::from(item))
|
||||
}
|
||||
|
||||
async fn get_items(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
query: GetItemsQuery,
|
||||
) -> anyhow::Result<Vec<Item>> {
|
||||
let items = ctx
|
||||
.data_unchecked::<ServiceRegister>()
|
||||
.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<ProjectDto> {
|
||||
ctx.data_unchecked::<ServiceRegister>()
|
||||
.project_service
|
||||
.get_project(query)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn get_projects(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
query: GetProjectsQuery,
|
||||
) -> anyhow::Result<Vec<ProjectDto>> {
|
||||
ctx.data_unchecked::<ServiceRegister>()
|
||||
.project_service
|
||||
.get_projects(query)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
76
como_gql/src/items.rs
Normal file
76
como_gql/src/items.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
use async_graphql::{Context, Object};
|
||||
use como_domain::{
|
||||
item::{queries::GetItemQuery, ItemDto, ItemState},
|
||||
projects::queries::GetProjectQuery,
|
||||
};
|
||||
use como_infrastructure::register::ServiceRegister;
|
||||
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 = ctx
|
||||
.data_unchecked::<ServiceRegister>()
|
||||
.item_service
|
||||
.get_item(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,
|
||||
}
|
||||
|
||||
#[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 = ctx
|
||||
.data_unchecked::<ServiceRegister>()
|
||||
.project_service
|
||||
.get_project(GetProjectQuery {
|
||||
item_id: Some(self.id),
|
||||
project_id: None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(project.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ItemDto> for Item {
|
||||
fn from(dto: ItemDto) -> Self {
|
||||
Self {
|
||||
id: dto.id,
|
||||
title: dto.title,
|
||||
description: dto.description,
|
||||
state: dto.state,
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,6 +9,8 @@ use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
|
||||
use graphql::ComoSchema;
|
||||
|
||||
pub mod graphql;
|
||||
mod items;
|
||||
mod projects;
|
||||
|
||||
pub async fn graphql_handler(
|
||||
schema: Extension<ComoSchema>,
|
||||
|
18
como_gql/src/projects.rs
Normal file
18
como_gql/src/projects.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use async_graphql::SimpleObject;
|
||||
use como_domain::projects::ProjectDto;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(SimpleObject)]
|
||||
pub struct Project {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl From<ProjectDto> for Project {
|
||||
fn from(dto: ProjectDto) -> Self {
|
||||
Self {
|
||||
id: dto.id,
|
||||
name: dto.name,
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user