Skip to content

Commit

Permalink
feat: avoid boilerplate with frunk
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Jul 19, 2023
1 parent fa1fb47 commit 05a920b
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 102 deletions.
54 changes: 54 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter", "time", "lo
# Configurations
dotenv = "0.15.0"

# Misc
base64 = "0.21.2"
chrono = "0.4.26"
frunk = "0.4.2"
frunk_core = { version = "0.4.2" }
serde = "1.0.171"
serde_json = "1.0.103"
thiserror = "1.0.43"
Expand Down
4 changes: 3 additions & 1 deletion src/domain/health/entities.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Debug)]
use frunk::LabelledGeneric;

#[derive(Debug, LabelledGeneric)]
pub struct Health {
pub status: String,
}
Expand Down
13 changes: 2 additions & 11 deletions src/domain/health/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
use async_graphql::SimpleObject;
use frunk::LabelledGeneric;
use serde::Serialize;
use utoipa::ToSchema;

use crate::domain::health::entities;

#[derive(Debug, SimpleObject, Serialize, ToSchema)]
#[derive(Debug, SimpleObject, Serialize, ToSchema, LabelledGeneric)]
pub struct Health {
pub status: String,
}

impl From<entities::Health> for Health {
fn from(health: entities::Health) -> Self {
Self {
status: health.status,
}
}
}

#[derive(Debug, Serialize, ToSchema)]
pub struct HealthResponse {
pub data: Health,
Expand Down
3 changes: 2 additions & 1 deletion src/domain/health/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use async_graphql::{Context, Error, FieldResult, Object};
use axum::{response::IntoResponse, Json};
use frunk_core::labelled::Transmogrifier;

use super::model;
use crate::context::ServerContext;
Expand All @@ -16,7 +17,7 @@ impl HealthQuery {

let result = server_ctx.health_service.get_health().await;
match result {
Ok(res) => Ok(res.into()),
Ok(res) => Ok(res.transmogrify()),
Err(err) => Err(Error::new(err.to_string())),
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/domain/meta/entities.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Debug)]
use frunk::LabelledGeneric;

#[derive(Debug, LabelledGeneric)]
pub struct Meta {
pub build: String,
pub version: String,
Expand Down
14 changes: 2 additions & 12 deletions src/domain/meta/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
use async_graphql::SimpleObject;
use frunk::LabelledGeneric;

use crate::domain::meta::entities;

#[derive(Debug, SimpleObject)]
#[derive(Debug, SimpleObject, LabelledGeneric)]
pub struct Meta {
pub build: String,
pub version: String,
}

impl From<entities::Meta> for Meta {
fn from(meta: entities::Meta) -> Self {
Self {
build: meta.build,
version: meta.version,
}
}
}
3 changes: 2 additions & 1 deletion src/domain/meta/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use async_graphql::{Context, Error, FieldResult, Object};
use frunk_core::labelled::Transmogrifier;

use super::model;
use crate::context::ServerContext;
Expand All @@ -15,7 +16,7 @@ impl MetaQuery {

let result = server_ctx.meta_service.get_meta().await;
match result {
Ok(res) => Ok(res.into()),
Ok(res) => Ok(res.transmogrify()),
Err(err) => Err(Error::new(err.to_string())),
}
}
Expand Down
35 changes: 19 additions & 16 deletions src/domain/user/entities.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
use chrono;
use sqlx;

use crate::relay::Base64Cursor;
use frunk::LabelledGeneric;

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct User {
pub id: uuid::Uuid,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
use crate::{
relay::Base64Cursor,
scalar::{Id, Time},
};

#[derive(Debug, Clone, sqlx::FromRow, LabelledGeneric)]
pub struct User {
pub id: Id,
pub created_at: Time,
pub updated_at: Time,
pub name: String,
pub full_name: Option<String>,
}

#[derive(Debug)]
pub struct PageInfo {
pub end_cursor: Option<String>,
pub has_next_page: bool,
pub start_cursor: Option<String>,
pub has_previous_page: bool,
}

#[derive(Debug)]
#[derive(Debug, LabelledGeneric)]
pub struct UserEdge {
pub node: User,
pub cursor: String,
Expand All @@ -33,3 +28,11 @@ impl From<User> for UserEdge {
Self { node: user, cursor }
}
}

#[derive(Debug, LabelledGeneric)]
pub struct PageInfo {
pub end_cursor: Option<String>,
pub has_next_page: bool,
pub start_cursor: Option<String>,
pub has_previous_page: bool,
}
7 changes: 4 additions & 3 deletions src/domain/user/model/input.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use async_graphql::InputObject;
use frunk::LabelledGeneric;

use crate::scalar::Id;

#[derive(InputObject)]
#[derive(InputObject, LabelledGeneric)]
pub struct CreateUserInput {
/// The name for the User.
pub name: String,
/// The full name for the User.
pub full_name: Option<String>,
}

#[derive(InputObject)]
#[derive(InputObject, LabelledGeneric)]
pub struct UpdateUserInput {
/// The ID of the User to modify.
pub id: Id,
Expand All @@ -20,7 +21,7 @@ pub struct UpdateUserInput {
pub full_name: Option<String>,
}

#[derive(InputObject)]
#[derive(InputObject, LabelledGeneric)]
pub struct DeleteUserInput {
pub user_id: Id,
}
44 changes: 7 additions & 37 deletions src/domain/user/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod input;
use std::sync::Arc;

use async_graphql::{ComplexObject, Context, Result, SimpleObject};
use frunk::LabelledGeneric;
use frunk_core::labelled::Transmogrifier;
use sqlx::Row;

use crate::{
Expand All @@ -11,7 +13,7 @@ use crate::{
scalar::{Id, Time},
};

#[derive(Debug, SimpleObject)]
#[derive(Debug, SimpleObject, LabelledGeneric)]
pub struct User {
/// The ID of the User.
pub id: Id,
Expand All @@ -23,39 +25,18 @@ pub struct User {
pub full_name: Option<String>,
}

impl From<entities::User> for User {
fn from(user: entities::User) -> Self {
Self {
id: user.id,
created_at: user.created_at,

name: user.name,
full_name: user.full_name,
}
}
}

#[derive(Debug, SimpleObject)]
#[derive(Debug, SimpleObject, LabelledGeneric)]
pub struct UserEdge {
// The item at the end of the edge.
pub node: User,
// A cursor for use in pagination.
pub cursor: String,
}

impl From<entities::UserEdge> for UserEdge {
fn from(user: entities::UserEdge) -> Self {
Self {
node: user.node.into(),
cursor: user.cursor,
}
}
}

impl From<entities::User> for UserEdge {
fn from(user: entities::User) -> Self {
let cursor = Base64Cursor::new(user.id).encode();
let user_model = user.into();
let user_model = user.transmogrify();
Self {
node: user_model,
cursor,
Expand All @@ -81,7 +62,7 @@ pub struct UserConnection {
pub last: Option<i32>,
}

#[derive(Debug, SimpleObject)]
#[derive(Debug, SimpleObject, LabelledGeneric)]
pub struct PageInfo {
// When paginating forwards, the cursor to continue.
pub end_cursor: Option<String>,
Expand All @@ -93,17 +74,6 @@ pub struct PageInfo {
pub has_previous_page: bool,
}

impl From<entities::PageInfo> for PageInfo {
fn from(page_info: entities::PageInfo) -> Self {
Self {
has_next_page: page_info.has_next_page,
has_previous_page: page_info.has_previous_page,
start_cursor: page_info.start_cursor,
end_cursor: page_info.end_cursor,
}
}
}

#[ComplexObject]
impl UserConnection {
// Information to aid in pagination.
Expand All @@ -118,7 +88,7 @@ impl UserConnection {
self.before.clone(),
)
.await?;
Ok(page_info.into())
Ok(page_info.transmogrify())
}
// Identifies the total count of items in the connection.
async fn total_count(&self, ctx: &Context<'_>) -> Result<i64> {
Expand Down
Loading

0 comments on commit 05a920b

Please sign in to comment.