Skip to content
Clone

feat(project): add graphql types

zhenyi committed
parent a2d2103 commit d5939db Browse files

1 file changed

+85 −0

Added lib/project/graphql/types.rs View lib/project/graphql/types.rs
@@ -0,0 +1,85 @@
1use async_graphql::SimpleObject;
2use time::OffsetDateTime;
3use uuid::Uuid;
4
5#[derive(SimpleObject, Clone)]
6pub struct ProjectGql {
7 pub id: String,
8 pub space_id: String,
9 pub creator_id: String,
10 pub lead_id: Option<String>,
11 pub name: String,
12 pub description: Option<String>,
13 pub icon: Option<String>,
14 pub color: Option<String>,
15 pub state: String,
16 pub target_date: Option<String>,
17 pub started_at: Option<String>,
18 pub completed_at: Option<String>,
19 pub cancelled_at: Option<String>,
20 pub health: Option<String>,
21 pub sort_order: i32,
22 pub starred: i32,
23 pub created_at: String,
24 pub updated_at: Option<String>,
25}
26
27impl From<db::project::project::Model> for ProjectGql {
28 fn from(m: db::project::project::Model) -> Self {
29 Self {
30 id: m.id.to_string(),
31 space_id: m.space_id.to_string(),
32 creator_id: m.creator_id.to_string(),
33 lead_id: m.lead_id.map(|x| x.to_string()),
34 name: m.name,
35 description: m.description,
36 icon: m.icon,
37 color: m.color,
38 state: format!("{:?}", m.state),
39 target_date: m.target_date.map(dt_to_string),
40 started_at: m.started_at.map(odt_to_string),
41 completed_at: m.completed_at.map(odt_to_string),
42 cancelled_at: m.cancelled_at.map(odt_to_string),
43 health: m.health.map(|h| format!("{:?}", h)),
44 sort_order: m.sort_order,
45 starred: m.starred,
46 created_at: odt_to_string(m.created_at),
47 updated_at: m.updated_at.map(odt_to_string),
48 }
49 }
50}
51
52#[derive(SimpleObject, Clone)]
53pub struct ProjectMemberGql {
54 pub id: String,
55 pub project_id: String,
56 pub user_id: String,
57 pub role: String,
58 pub joined_at: String,
59}
60
61impl From<db::project::project_members::Model> for ProjectMemberGql {
62 fn from(m: db::project::project_members::Model) -> Self {
63 Self {
64 id: m.id.to_string(),
65 project_id: m.project_id.to_string(),
66 user_id: m.user_id.to_string(),
67 role: format!("{:?}", m.role),
68 joined_at: odt_to_string(m.joined_at),
69 }
70 }
71}
72
73pub fn parse_id(id: &str) -> async_graphql::Result<Uuid> {
74 Uuid::parse_str(id)
75 .map_err(|e| async_graphql::Error::new(format!("invalid id `{id}`: {e}")))
76}
77
78fn odt_to_string(v: OffsetDateTime) -> String {
79 v.format(&time::format_description::well_known::Rfc3339)
80 .unwrap_or_default()
81}
82
83fn dt_to_string(v: time::Date) -> String {
84 v.to_string()
85}
No newline at end of file