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

Commit 2aeb22d

Browse files
committedAug 25, 2024
feat(gql): Get user information
1 parent 65336f6 commit 2aeb22d

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
 

‎src/gql.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ pub mod poem;
66
pub mod questions;
77
pub mod schema;
88
pub mod sql_executor;
9+
pub mod user;
910

1011
use async_graphql::MergedObject;
1112

1213
#[derive(MergedObject, Default)]
13-
pub struct Query(pub schema::SchemaQuery, pub questions::QuestionQuery);
14+
pub struct Query(
15+
pub schema::SchemaQuery,
16+
pub questions::QuestionQuery,
17+
pub user::UserQuery,
18+
);
1419

1520
#[derive(MergedObject, Default)]
1621
pub struct Mutation(pub sql_executor::SqlExecutorMutation);

‎src/gql/user.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use async_graphql::{Context, Object, Result, SimpleObject};
2+
use ecow::EcoString;
3+
4+
use super::error::Error;
5+
use crate::{db, gql::error::ErrorCode};
6+
7+
use super::auth::ContextAuthExt;
8+
9+
#[derive(Default)]
10+
pub struct UserQuery;
11+
12+
#[Object]
13+
impl UserQuery {
14+
async fn user<'ctx>(&self, ctx: &Context<'ctx>) -> Result<User> {
15+
tracing::debug!("Running GraphQL query 'user'");
16+
17+
let Some(sub) = ctx.sub() else {
18+
return Err(Error {
19+
code: ErrorCode::Unauthorized,
20+
title: EcoString::inline("Unauthorized"),
21+
details: "You must login to access this API.".into(),
22+
error: None,
23+
}
24+
.to_gql_error());
25+
};
26+
let pool = ctx.data::<db::Pool>()?;
27+
28+
let user = db::get_or_initialize_user(pool, sub).await?;
29+
Ok(user.into())
30+
}
31+
}
32+
33+
#[derive(Debug, Clone, SimpleObject)]
34+
pub struct User {
35+
pub user_id: String,
36+
pub group_id: Option<i64>,
37+
pub created_at: chrono::DateTime<chrono::Utc>,
38+
pub updated_at: chrono::DateTime<chrono::Utc>,
39+
}
40+
41+
impl From<db::User> for User {
42+
fn from(user: db::User) -> Self {
43+
Self {
44+
user_id: user.user_id,
45+
group_id: user.group_id,
46+
created_at: user.created_at,
47+
updated_at: user.updated_at,
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)
This repository has been archived.