From f860b8fe43a214f5a6da67573d11d1b4323165e8 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Fri, 27 Aug 2021 00:02:11 +0200 Subject: [PATCH] app: Migrate list_users to use the graphql client --- Cargo.lock | 128 ++++++++++++++++++++++++++++++++- app/Cargo.toml | 1 + app/queries/list_users.graphql | 9 +++ app/src/api.rs | 31 ++++++-- app/src/graphql_api.rs | 8 +++ app/src/lib.rs | 1 + app/src/user_table.rs | 30 ++++++-- 7 files changed, 196 insertions(+), 12 deletions(-) create mode 100644 app/queries/list_users.graphql create mode 100644 app/src/graphql_api.rs diff --git a/Cargo.lock b/Cargo.lock index 378dfec..1ffae44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -289,6 +289,15 @@ dependencies = [ "syn", ] +[[package]] +name = "addr2line" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e61f2b7f93d2c7d2b08263acaa4a363b3e276806c68af6134c44f523bf1aacd" +dependencies = [ + "gimli", +] + [[package]] name = "adler" version = "1.0.2" @@ -429,6 +438,21 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "base-x" version = "0.2.8" @@ -961,6 +985,28 @@ dependencies = [ "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]] name = "figment" version = "0.10.5" @@ -1188,6 +1234,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gimli" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" + [[package]] name = "gloo" version = "0.2.1" @@ -1242,6 +1294,25 @@ dependencies = [ "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]] name = "graphql-parser" version = "0.3.0" @@ -1252,6 +1323,45 @@ dependencies = [ "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]] name = "h2" version = "0.3.3" @@ -1445,7 +1555,7 @@ dependencies = [ "fnv", "futures", "futures-enum", - "graphql-parser", + "graphql-parser 0.3.0", "indexmap", "juniper_codegen", "serde", @@ -1637,6 +1747,7 @@ version = "0.1.0" dependencies = [ "anyhow", "chrono", + "graphql_client", "http", "jwt", "lldap_model", @@ -1970,6 +2081,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c55827317fb4c08822499848a14237d2874d6f139828893017237e7ab93eb386" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.7.2" @@ -2430,6 +2550,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "rustc-demangle" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" + [[package]] name = "rustc_version" version = "0.2.3" diff --git a/app/Cargo.toml b/app/Cargo.toml index 3a04413..15f61b0 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -15,6 +15,7 @@ serde_json = "1" wasm-bindgen = "0.2" yew = "0.18" yew-router = "0.15" +graphql_client = "0.10.0" [dependencies.web-sys] version = "0.3" diff --git a/app/queries/list_users.graphql b/app/queries/list_users.graphql new file mode 100644 index 0000000..4c2defb --- /dev/null +++ b/app/queries/list_users.graphql @@ -0,0 +1,9 @@ +query ListUsersQuery($filters: RequestFilter) { + users(filters: $filters) { + id + email + groups { + id + } + } +} diff --git a/app/src/api.rs b/app/src/api.rs index feda0e7..6d688a1 100644 --- a/app/src/api.rs +++ b/app/src/api.rs @@ -1,5 +1,6 @@ use crate::cookies::set_cookie; use anyhow::{anyhow, Context, Result}; +use graphql_client::GraphQLQuery; use lldap_model::*; use yew::callback::Callback; @@ -109,11 +110,31 @@ where } impl HostService { - pub fn list_users( - request: ListUsersRequest, - callback: Callback>>, - ) -> Result { - call_server_json_with_error_message("/api/users", &request, callback, "") + pub fn graphql_query( + variables: QueryType::Variables, + callback: Callback>, + error_message: &'static str, + ) -> Result + 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 { diff --git a/app/src/graphql_api.rs b/app/src/graphql_api.rs new file mode 100644 index 0000000..a32257d --- /dev/null +++ b/app/src/graphql_api.rs @@ -0,0 +1,8 @@ +use graphql_client::GraphQLQuery; + +#[derive(GraphQLQuery)] +#[graphql( + schema_path = "../schema.graphql", + query_path = "queries/list_users.graphql" +)] +pub struct ListUsersQuery; diff --git a/app/src/lib.rs b/app/src/lib.rs index b8fee89..82b54d3 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -4,6 +4,7 @@ mod api; mod app; mod cookies; mod create_user; +mod graphql_api; mod login; mod logout; mod user_details; diff --git a/app/src/user_table.rs b/app/src/user_table.rs index 91fc798..7c2d58f 100644 --- a/app/src/user_table.rs +++ b/app/src/user_table.rs @@ -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 lldap_model::*; use yew::format::Json; @@ -13,12 +19,16 @@ pub struct UserTable { } pub enum Msg { - ListUsersResponse(Result>), + ListUsersResponse(Result), } impl UserTable { - fn get_users(&mut self, req: ListUsersRequest) { - match HostService::list_users(req, self.link.callback(Msg::ListUsersResponse)) { + fn get_users(&mut self, req: Option) { + match HostService::graphql_query::( + list_users_query::Variables { filters: req }, + self.link.callback(Msg::ListUsersResponse), + "", + ) { Ok(task) => self._task = Some(task), Err(e) => { self._task = None; @@ -38,14 +48,22 @@ impl Component for UserTable { _task: None, users: None, }; - table.get_users(ListUsersRequest { filters: None }); + table.get_users(None); table } fn update(&mut self, msg: Self::Message) -> ShouldRender { match msg { 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()); true }