From 57ab6a51da155a52e7a855da78b501e655105584 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 6 Jun 2021 21:22:23 +0000 Subject: [PATCH] Avoid random clones in API. --- api/src/schema.rs | 27 +++++++++++---------------- dicebot/src/rpc/service.rs | 18 +++++++++++++++--- rpc/protos/dicebot.proto | 12 +++++++++--- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/api/src/schema.rs b/api/src/schema.rs index dba40c2..165e840 100644 --- a/api/src/schema.rs +++ b/api/src/schema.rs @@ -41,20 +41,17 @@ pub struct Context { pub dicebot_client: DicebotGrpcClient, } -// To make our context usable by Juniper, we have to implement a marker trait. +/// Marker trait to make the context object usable in GraphQL. impl juniper::Context for Context {} #[derive(Clone, Copy, Debug)] pub struct Query; #[graphql_object( - // Here we specify the context type for the object. - // We need to do this in every type that - // needs access to the context. - context = Context, + context = Context, )] impl Query { - fn apiVersion() -> &str { + fn api_version() -> &str { "1.0" } @@ -65,9 +62,9 @@ impl Query { variable: String, ) -> FieldResult { let request = TonicRequest::new(GetVariableRequest { - room_id: room_id.clone(), - user_id: user_id.clone(), - variable_name: variable.clone(), + room_id, + user_id, + variable_name: variable, }); let response = context @@ -78,17 +75,15 @@ impl Query { .into_inner(); Ok(UserVariable { - user_id: user_id.clone(), - room_id: room_id.clone(), - variable_name: variable.clone(), + user_id: response.user_id, + room_id: response.room_id, + variable_name: response.variable_name, value: response.value, }) } async fn user_rooms(context: &Context, user_id: String) -> FieldResult { - let request = TonicRequest::new(UserIdRequest { - user_id: user_id.clone(), - }); + let request = TonicRequest::new(UserIdRequest { user_id }); let response = context .dicebot_client @@ -98,7 +93,7 @@ impl Query { .into_inner(); Ok(UserRoomList { - user_id, + user_id: response.user_id, rooms: response .rooms .into_iter() diff --git a/dicebot/src/rpc/service.rs b/dicebot/src/rpc/service.rs index 1d3136b..eba2e8f 100644 --- a/dicebot/src/rpc/service.rs +++ b/dicebot/src/rpc/service.rs @@ -63,7 +63,12 @@ impl Dicebot for DicebotRpcService { .get_user_variable(&request.user_id, &request.room_id, &request.variable_name) .await?; - Ok(Response::new(GetVariableReply { value })) + Ok(Response::new(GetVariableReply { + user_id: request.user_id.clone(), + room_id: request.room_id.clone(), + variable_name: request.variable_name.clone(), + value, + })) } async fn get_all_variables( @@ -76,7 +81,11 @@ impl Dicebot for DicebotRpcService { .get_user_variables(&request.user_id, &request.room_id) .await?; - Ok(Response::new(GetAllVariablesReply { variables })) + Ok(Response::new(GetAllVariablesReply { + user_id: request.user_id.clone(), + room_id: request.user_id.clone(), + variables, + })) } async fn rooms_for_user( @@ -111,6 +120,9 @@ impl Dicebot for DicebotRpcService { rooms.sort_by(sort); - Ok(Response::new(RoomsListReply { rooms })) + Ok(Response::new(RoomsListReply { + user_id: user_id.into_string(), + rooms, + })) } } diff --git a/rpc/protos/dicebot.proto b/rpc/protos/dicebot.proto index b27f0f0..9f222ce 100644 --- a/rpc/protos/dicebot.proto +++ b/rpc/protos/dicebot.proto @@ -15,7 +15,10 @@ message GetVariableRequest { } message GetVariableReply { - int32 value = 1; + string user_id = 1; + string room_id = 2; + string variable_name = 3; + int32 value = 4; } message GetAllVariablesRequest { @@ -24,7 +27,9 @@ message GetAllVariablesRequest { } message GetAllVariablesReply { - map variables = 1; + string user_id = 1; + string room_id = 2; + map variables = 3; } message SetVariableRequest { @@ -48,5 +53,6 @@ message RoomsListReply { string display_name = 2; } - repeated Room rooms = 1; + string user_id = 1; + repeated Room rooms = 2; } \ No newline at end of file