1
1
use chrono:: NaiveDate ;
2
- use juniper:: { FieldResult , RootNode , EmptySubscription , Context as JuniperContext } ;
2
+ use juniper:: { Context as JuniperContext , EmptySubscription , FieldResult , RootNode } ;
3
3
use std:: sync:: Arc ;
4
4
use tokio_postgres:: Client ;
5
5
use uuid:: Uuid ;
@@ -13,14 +13,13 @@ pub struct Context {
13
13
impl Context {
14
14
pub fn with ( client : & Arc < Client > ) -> Context {
15
15
Context {
16
- client : Arc :: clone ( client)
16
+ client : Arc :: clone ( client) ,
17
17
}
18
18
}
19
19
}
20
20
21
21
impl JuniperContext for Context { }
22
22
23
- #[ derive( GraphQLObject ) ]
24
23
pub struct Member {
25
24
pub id : String ,
26
25
pub email : String ,
@@ -31,13 +30,29 @@ pub struct Member {
31
30
32
31
#[ graphql_object( Context = Context ) ]
33
32
impl Member {
34
- pub async fn rides ( & self ) -> FieldResult < Vec < Ride > > {
35
- let context: & Context = executor. context ( ) ;
36
-
37
- let rows = context. client . query (
38
- "SELECT id, name, description, distance, started, ended FROM ride WHERE rider = $1" ,
39
- & [ & self . id ] ,
40
- ) . await ?;
33
+ fn id ( & self ) -> & str {
34
+ & self . id
35
+ }
36
+ fn email ( & self ) -> & str {
37
+ & self . email
38
+ }
39
+ fn firstname ( & self ) -> & str {
40
+ & self . firstname
41
+ }
42
+ fn lastname ( & self ) -> & str {
43
+ & self . firstname
44
+ }
45
+ fn birthdate ( & self ) -> NaiveDate {
46
+ self . birthdate
47
+ }
48
+ async fn rides ( & self , context : & Context ) -> FieldResult < Vec < Ride > > {
49
+ let rows = context
50
+ . client
51
+ . query (
52
+ "SELECT id, name, description, distance, started, ended FROM ride WHERE rider = $1" ,
53
+ & [ & self . id ] ,
54
+ )
55
+ . await ?;
41
56
42
57
let mut rides: Vec < Ride > = Vec :: with_capacity ( rows. len ( ) ) ;
43
58
@@ -73,10 +88,13 @@ impl QueryRoot {
73
88
async fn member ( ctx : & Context , id : String ) -> FieldResult < Member > {
74
89
let uuid = Uuid :: parse_str ( & id) ?;
75
90
76
- let row = ctx. client . query_one (
77
- "SELECT email, firstname, lastname, birthdate FROM member WHERE id = $1" ,
78
- & [ & uuid] ,
79
- ) . await ?;
91
+ let row = ctx
92
+ . client
93
+ . query_one (
94
+ "SELECT email, firstname, lastname, birthdate FROM member WHERE id = $1" ,
95
+ & [ & uuid] ,
96
+ )
97
+ . await ?;
80
98
81
99
let member = Member {
82
100
id,
@@ -90,10 +108,13 @@ impl QueryRoot {
90
108
}
91
109
92
110
async fn members ( ctx : & Context ) -> FieldResult < Vec < Member > > {
93
- let rows = ctx. client . query (
94
- "SELECT id, email, firstname, lastname, birthdate FROM member" ,
95
- & [ ] ,
96
- ) . await ?;
111
+ let rows = ctx
112
+ . client
113
+ . query (
114
+ "SELECT id, email, firstname, lastname, birthdate FROM member" ,
115
+ & [ ] ,
116
+ )
117
+ . await ?;
97
118
98
119
let mut members = Vec :: new ( ) ;
99
120
@@ -117,7 +138,13 @@ pub struct MutationRoot;
117
138
118
139
#[ graphql_object( Context = Context ) ]
119
140
impl MutationRoot {
120
- async fn register_member ( ctx : & Context , email : String , firstname : String , lastname : String , birthdate : NaiveDate ) -> FieldResult < Member > {
141
+ async fn register_member (
142
+ ctx : & Context ,
143
+ email : String ,
144
+ firstname : String ,
145
+ lastname : String ,
146
+ birthdate : NaiveDate ,
147
+ ) -> FieldResult < Member > {
121
148
let id = Uuid :: new_v4 ( ) ;
122
149
let email = email. to_lowercase ( ) ;
123
150
@@ -135,7 +162,14 @@ impl MutationRoot {
135
162
} )
136
163
}
137
164
138
- async fn register_ride ( ctx : & Context , rider : String , name : String , description : String , started : NaiveDate , ended : NaiveDate ) -> FieldResult < Ride > {
165
+ async fn register_ride (
166
+ ctx : & Context ,
167
+ rider : String ,
168
+ name : String ,
169
+ description : String ,
170
+ started : NaiveDate ,
171
+ ended : NaiveDate ,
172
+ ) -> FieldResult < Ride > {
139
173
let id = Uuid :: new_v4 ( ) ;
140
174
141
175
ctx. client . execute (
@@ -153,4 +187,3 @@ impl MutationRoot {
153
187
} )
154
188
}
155
189
}
156
-
0 commit comments