Avoid random clones in API.

This commit is contained in:
projectmoon 2021-06-06 21:22:23 +00:00
parent 05157507aa
commit 57ab6a51da
3 changed files with 35 additions and 22 deletions

View File

@ -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<UserVariable> {
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<UserRoomList> {
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()

View File

@ -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,
}))
}
}

View File

@ -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<string, int32> variables = 1;
string user_id = 1;
string room_id = 2;
map<string, int32> 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;
}