app: Migrate list_users to use the graphql client

This commit is contained in:
Valentin Tolmer 2021-08-27 00:02:11 +02:00 committed by nitnelave
parent 83ed58bff2
commit f860b8fe43
7 changed files with 196 additions and 12 deletions

128
Cargo.lock generated
View File

@ -289,6 +289,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "addr2line"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e61f2b7f93d2c7d2b08263acaa4a363b3e276806c68af6134c44f523bf1aacd"
dependencies = [
"gimli",
]
[[package]] [[package]]
name = "adler" name = "adler"
version = "1.0.2" version = "1.0.2"
@ -429,6 +438,21 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7a905d892734eea339e896738c14b9afce22b5318f64b951e70bf3844419b01"
dependencies = [
"addr2line",
"cc",
"cfg-if 1.0.0",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]] [[package]]
name = "base-x" name = "base-x"
version = "0.2.8" version = "0.2.8"
@ -961,6 +985,28 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
] ]
[[package]]
name = "failure"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]] [[package]]
name = "figment" name = "figment"
version = "0.10.5" version = "0.10.5"
@ -1188,6 +1234,12 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "gimli"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7"
[[package]] [[package]]
name = "gloo" name = "gloo"
version = "0.2.1" version = "0.2.1"
@ -1242,6 +1294,25 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "graphql-introspection-query"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f2a4732cf5140bd6c082434494f785a19cfb566ab07d1382c3671f5812fed6d"
dependencies = [
"serde",
]
[[package]]
name = "graphql-parser"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5613c31f18676f164112732202124f373bb2103ff017b3b85ca954ea6a66ada"
dependencies = [
"combine",
"failure",
]
[[package]] [[package]]
name = "graphql-parser" name = "graphql-parser"
version = "0.3.0" version = "0.3.0"
@ -1252,6 +1323,45 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "graphql_client"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9b58571cfc3cc42c3e8ff44fc6cfbb6c0dea17ed22d20f9d8f1efc4e8209a3f"
dependencies = [
"graphql_query_derive",
"serde",
"serde_json",
]
[[package]]
name = "graphql_client_codegen"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4bf9cd823359d74ad3d3ecf1afd4a975f4ff2f891cdf9a66744606daf52de8c"
dependencies = [
"graphql-introspection-query",
"graphql-parser 0.2.3",
"heck",
"lazy_static",
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
]
[[package]]
name = "graphql_query_derive"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e56b093bfda71de1da99758b036f4cc811fd2511c8a76f75680e9ffbd2bb4251"
dependencies = [
"graphql_client_codegen",
"proc-macro2",
"syn",
]
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.3" version = "0.3.3"
@ -1445,7 +1555,7 @@ dependencies = [
"fnv", "fnv",
"futures", "futures",
"futures-enum", "futures-enum",
"graphql-parser", "graphql-parser 0.3.0",
"indexmap", "indexmap",
"juniper_codegen", "juniper_codegen",
"serde", "serde",
@ -1637,6 +1747,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",
"graphql_client",
"http", "http",
"jwt", "jwt",
"lldap_model", "lldap_model",
@ -1970,6 +2081,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "object"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c55827317fb4c08822499848a14237d2874d6f139828893017237e7ab93eb386"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.7.2" version = "1.7.2"
@ -2430,6 +2550,12 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]] [[package]]
name = "rustc_version" name = "rustc_version"
version = "0.2.3" version = "0.2.3"

View File

@ -15,6 +15,7 @@ serde_json = "1"
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
yew = "0.18" yew = "0.18"
yew-router = "0.15" yew-router = "0.15"
graphql_client = "0.10.0"
[dependencies.web-sys] [dependencies.web-sys]
version = "0.3" version = "0.3"

View File

@ -0,0 +1,9 @@
query ListUsersQuery($filters: RequestFilter) {
users(filters: $filters) {
id
email
groups {
id
}
}
}

View File

@ -1,5 +1,6 @@
use crate::cookies::set_cookie; use crate::cookies::set_cookie;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use graphql_client::GraphQLQuery;
use lldap_model::*; use lldap_model::*;
use yew::callback::Callback; use yew::callback::Callback;
@ -109,11 +110,31 @@ where
} }
impl HostService { impl HostService {
pub fn list_users( pub fn graphql_query<QueryType>(
request: ListUsersRequest, variables: QueryType::Variables,
callback: Callback<Result<Vec<User>>>, callback: Callback<Result<QueryType::ResponseData>>,
) -> Result<FetchTask> { error_message: &'static str,
call_server_json_with_error_message("/api/users", &request, callback, "") ) -> Result<FetchTask>
where
QueryType: GraphQLQuery + 'static,
{
let request_body = QueryType::build_query(variables);
call_server(
"/api/graphql",
&request_body,
callback,
move |status: http::StatusCode, data: String| {
if status.is_success() {
serde_json::from_str(&data)
.context("Could not parse response")
.and_then(|graphql_client::Response { data, errors }| {
data.ok_or_else(|| anyhow!("Errors: {:?}", errors.unwrap_or(vec![])))
})
} else {
Err(anyhow!("{}[{}]: {}", error_message, status, data))
}
},
)
} }
pub fn get_user_details(username: &str, callback: Callback<Result<User>>) -> Result<FetchTask> { pub fn get_user_details(username: &str, callback: Callback<Result<User>>) -> Result<FetchTask> {

8
app/src/graphql_api.rs Normal file
View File

@ -0,0 +1,8 @@
use graphql_client::GraphQLQuery;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../schema.graphql",
query_path = "queries/list_users.graphql"
)]
pub struct ListUsersQuery;

View File

@ -4,6 +4,7 @@ mod api;
mod app; mod app;
mod cookies; mod cookies;
mod create_user; mod create_user;
mod graphql_api;
mod login; mod login;
mod logout; mod logout;
mod user_details; mod user_details;

View File

@ -1,4 +1,10 @@
use crate::api::HostService; use crate::{
api::HostService,
graphql_api::{
list_users_query::{self, RequestFilter, ResponseData},
ListUsersQuery,
},
};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use lldap_model::*; use lldap_model::*;
use yew::format::Json; use yew::format::Json;
@ -13,12 +19,16 @@ pub struct UserTable {
} }
pub enum Msg { pub enum Msg {
ListUsersResponse(Result<Vec<User>>), ListUsersResponse(Result<ResponseData>),
} }
impl UserTable { impl UserTable {
fn get_users(&mut self, req: ListUsersRequest) { fn get_users(&mut self, req: Option<RequestFilter>) {
match HostService::list_users(req, self.link.callback(Msg::ListUsersResponse)) { match HostService::graphql_query::<ListUsersQuery>(
list_users_query::Variables { filters: req },
self.link.callback(Msg::ListUsersResponse),
"",
) {
Ok(task) => self._task = Some(task), Ok(task) => self._task = Some(task),
Err(e) => { Err(e) => {
self._task = None; self._task = None;
@ -38,14 +48,22 @@ impl Component for UserTable {
_task: None, _task: None,
users: None, users: None,
}; };
table.get_users(ListUsersRequest { filters: None }); table.get_users(None);
table table
} }
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg { match msg {
Msg::ListUsersResponse(Ok(users)) => { Msg::ListUsersResponse(Ok(users)) => {
self.users = Some(Ok(users)); self.users = Some(Ok(users
.users
.into_iter()
.map(|u| User {
user_id: u.id,
email: u.email,
..Default::default()
})
.collect()));
ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str()); ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str());
true true
} }