Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(gql): Allow getting group information
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Aug 25, 2024
1 parent 2aeb22d commit db81824
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/gql/user.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use async_graphql::{Context, Object, Result, SimpleObject};
use async_graphql::{ComplexObject, Context, Object, Result, SimpleObject};
use ecow::EcoString;

use super::error::Error;
Expand Down Expand Up @@ -31,6 +31,7 @@ impl UserQuery {
}

#[derive(Debug, Clone, SimpleObject)]
#[graphql(complex)]
pub struct User {
pub user_id: String,
pub group_id: Option<i64>,
Expand All @@ -48,3 +49,40 @@ impl From<db::User> for User {
}
}
}

#[ComplexObject]
impl User {
async fn group<'ctx>(&self, ctx: &Context<'ctx>) -> Result<Option<Group>> {
tracing::debug!("Running GraphQL query 'group'");

let Some(group_id) = self.group_id else {
return Ok(None);
};

let pool = ctx.data::<db::Pool>()?;
let group = db::get_group(pool, group_id).await?;

Ok(Some(group.into()))
}
}

#[derive(SimpleObject)]
pub struct Group {
pub group_id: i64,
pub name: String,
pub description: String,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl From<db::Group> for Group {
fn from(group: db::Group) -> Self {
Self {
group_id: group.group_id,
name: group.name,
description: group.description,
created_at: group.created_at,
updated_at: group.updated_at,
}
}
}

0 comments on commit db81824

Please sign in to comment.