query-type-fix

This commit is contained in:
Untone 2023-10-11 21:19:23 +03:00
parent 510ac17375
commit 5526ebe7c9

View File

@ -6,16 +6,16 @@ use std::env;
pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> { pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
let auth_api_base = env::var("AUTH_URL")?; let auth_api_base = env::var("AUTH_URL")?;
let queryname = match auth_api_base.contains("discours.io") { let (query_name, query_type) = match auth_api_base.contains("discours.io") {
true => "getSession", // v2 true => ("getSession", "mutation"), // v2
_ => "session" // authorizer _ => ("session", "query") // authorizer
}; };
let body = format!(r#"{{ let body = format!(r#"{{
"query": "mutation GetSessionMutation {{ {} {{ user {{ id }} }} }}", "query": "{} GetSession {{ {} {{ user {{ id }} }} }}",
"operationName": "GetSessionMutation", "operationName": "GetSession",
"variables": {{}} "variables": {{}}
}}"#, queryname); }}"#, query_type, query_name);
let client = HTTPClient::new(); let client = HTTPClient::new();
let response = client let response = client
@ -25,7 +25,7 @@ pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
.send() .send()
.await?; .await?;
let response_body: Value = response.json().await?; let response_body: Value = response.json().await?;
let id = response_body["data"][queryname]["user"]["id"] let id = response_body["data"][query_name]["user"]["id"]
.as_i64() .as_i64()
.ok_or("Failed to get user id by token")? as i32; .ok_or("Failed to get user id by token")? as i32;
Ok(id) Ok(id)