@@ -12,38 +12,40 @@ impl SearchUsers {
1212 async fn search ( & self , ctx : & Context < ' _ > , query_string : String ) -> Result < Vec < User > > {
1313 use crate :: schema:: users:: dsl:: users;
1414
15- let app_data = ctx. data :: < GraphQlData > ( ) . map_err ( |e| {
15+ let GraphQlData { pool , client } = ctx. data ( ) . map_err ( |e| {
1616 log:: error!( "Failed to get app data: {:?}" , e) ;
1717 e
1818 } ) ?;
1919
20- let mut connection = app_data . pool . get ( ) . await ?;
20+ let mut connection = pool. get ( ) . await ?;
2121
2222 let list_users = users. load :: < User > ( & mut connection) . await ?;
2323
24- match app_data. client . get_index ( "users" ) . await {
24+ match client. get_index ( "users" ) . await {
25+ //If getting the index is successful, we add documents to it
2526 Ok ( index) => {
2627 index. add_documents ( & list_users, Some ( "id" ) ) . await ?;
2728 }
29+
30+ //If getting the index fails, we create it and then add documents to the new index
2831 Err ( _) => {
29- let task = app_data. client . create_index ( "users" , Some ( "id" ) ) . await ?;
30- let task = task
31- . wait_for_completion ( & app_data. client , None , None )
32- . await ?;
33- let index = task. try_make_index ( & app_data. client ) . unwrap ( ) ;
32+ let task = client. create_index ( "users" , Some ( "id" ) ) . await ?;
33+ let task = task. wait_for_completion ( client, None , None ) . await ?;
34+ let index = task. try_make_index ( client) . unwrap ( ) ;
3435
3536 index. add_documents ( & list_users, Some ( "id" ) ) . await ?;
3637 }
3738 }
3839
39- let index = app_data . client . get_index ( "users" ) . await ?;
40+ let index = client. get_index ( "users" ) . await ?;
4041
42+ //We build the query
4143 let query = SearchQuery :: new ( & index) . with_query ( & query_string) . build ( ) ;
4244
4345 let results: SearchResults < User > = index. execute_query ( & query) . await ?;
4446
45- log :: error! ( "{:#?}" , results. hits ) ;
46-
47+ //Tranform the results into a type that implements OutputType
48+ //Required for return types to implement this trait
4749 let search_results: Vec < User > = results
4850 . hits
4951 . into_iter ( )
0 commit comments