1
- use async_graphql:: { Context , Object , Result , SimpleObject } ;
1
+ use async_graphql:: { ComplexObject , Context , Object , Result , SimpleObject } ;
2
2
use ecow:: EcoString ;
3
3
4
4
use super :: error:: Error ;
@@ -31,6 +31,7 @@ impl UserQuery {
31
31
}
32
32
33
33
#[ derive( Debug , Clone , SimpleObject ) ]
34
+ #[ graphql( complex) ]
34
35
pub struct User {
35
36
pub user_id : String ,
36
37
pub group_id : Option < i64 > ,
@@ -48,3 +49,40 @@ impl From<db::User> for User {
48
49
}
49
50
}
50
51
}
52
+
53
+ #[ ComplexObject ]
54
+ impl User {
55
+ async fn group < ' ctx > ( & self , ctx : & Context < ' ctx > ) -> Result < Option < Group > > {
56
+ tracing:: debug!( "Running GraphQL query 'group'" ) ;
57
+
58
+ let Some ( group_id) = self . group_id else {
59
+ return Ok ( None ) ;
60
+ } ;
61
+
62
+ let pool = ctx. data :: < db:: Pool > ( ) ?;
63
+ let group = db:: get_group ( pool, group_id) . await ?;
64
+
65
+ Ok ( Some ( group. into ( ) ) )
66
+ }
67
+ }
68
+
69
+ #[ derive( SimpleObject ) ]
70
+ pub struct Group {
71
+ pub group_id : i64 ,
72
+ pub name : String ,
73
+ pub description : String ,
74
+ pub created_at : chrono:: DateTime < chrono:: Utc > ,
75
+ pub updated_at : chrono:: DateTime < chrono:: Utc > ,
76
+ }
77
+
78
+ impl From < db:: Group > for Group {
79
+ fn from ( group : db:: Group ) -> Self {
80
+ Self {
81
+ group_id : group. group_id ,
82
+ name : group. name ,
83
+ description : group. description ,
84
+ created_at : group. created_at ,
85
+ updated_at : group. updated_at ,
86
+ }
87
+ }
88
+ }
0 commit comments