From 2aca9dbe62f311edf6f31b80ff5f0d72c615d060 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Mon, 30 Aug 2021 09:50:10 +0200 Subject: [PATCH] app: migrate user creation to GraphQL --- app/queries/create_user.graphql | 6 ++++ app/src/api.rs | 14 +------- app/src/create_user.rs | 57 +++++++++++++++++++++------------ 3 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 app/queries/create_user.graphql diff --git a/app/queries/create_user.graphql b/app/queries/create_user.graphql new file mode 100644 index 0000000..bf269e0 --- /dev/null +++ b/app/queries/create_user.graphql @@ -0,0 +1,6 @@ +mutation CreateUser($user: UserInput!) { + createUser(user: $user) { + id + creationDate + } +} diff --git a/app/src/api.rs b/app/src/api.rs index 3e9afca..149c58d 100644 --- a/app/src/api.rs +++ b/app/src/api.rs @@ -1,7 +1,7 @@ use crate::cookies::set_cookie; use anyhow::{anyhow, Context, Result}; use graphql_client::GraphQLQuery; -use lldap_model::*; +use lldap_model::{login, registration, JWTClaims}; use yew::callback::Callback; use yew::format::Json; @@ -231,16 +231,4 @@ impl HostService { "Could not logout", ) } - - pub fn create_user( - request: CreateUserRequest, - callback: Callback>, - ) -> Result { - call_server_empty_response_with_error_message( - "/api/users/create", - &request, - callback, - "Could not create a user", - ) - } } diff --git a/app/src/create_user.rs b/app/src/create_user.rs index 90dad3d..c4587d5 100644 --- a/app/src/create_user.rs +++ b/app/src/create_user.rs @@ -1,6 +1,7 @@ use crate::api::HostService; use anyhow::{anyhow, Context, Result}; -use lldap_model::*; +use graphql_client::GraphQLQuery; +use lldap_model::{opaque, registration}; use yew::prelude::*; use yew::services::{fetch::FetchTask, ConsoleService}; use yew_router::{ @@ -8,6 +9,15 @@ use yew_router::{ route::Route, }; +#[derive(GraphQLQuery)] +#[graphql( + schema_path = "../schema.graphql", + query_path = "queries/create_user.graphql", + response_derives = "Debug", + custom_scalars_module = "crate::graphql" +)] +pub struct CreateUser; + pub struct CreateUserForm { link: ComponentLink, route_dispatcher: RouteAgentDispatcher, @@ -19,7 +29,7 @@ pub struct CreateUserForm { } pub enum Msg { - CreateUserResponse(Result<()>), + CreateUserResponse(Result), SubmitForm, SuccessfulCreation, RegistrationStartResponse(Result>), @@ -35,26 +45,33 @@ impl CreateUserForm { fn handle_msg(&mut self, msg: ::Message) -> Result<()> { match msg { Msg::SubmitForm => { - let req = CreateUserRequest { - user_id: get_element("username") - .filter(not_empty) - .ok_or_else(|| anyhow!("Missing username"))?, - email: get_element("email") - .filter(not_empty) - .ok_or_else(|| anyhow!("Missing email"))?, - display_name: get_element("displayname").filter(not_empty), - first_name: get_element("firstname").filter(not_empty), - last_name: get_element("lastname").filter(not_empty), + let req = create_user::Variables { + user: create_user::UserInput { + id: get_element("username") + .filter(not_empty) + .ok_or_else(|| anyhow!("Missing username"))?, + email: get_element("email") + .filter(not_empty) + .ok_or_else(|| anyhow!("Missing email"))?, + displayName: get_element("displayname").filter(not_empty), + firstName: get_element("firstname").filter(not_empty), + lastName: get_element("lastname").filter(not_empty), + }, }; - self._task = Some( - HostService::create_user(req, self.link.callback(Msg::CreateUserResponse)) - .context("Error trying to create user")?, - ); + self._task = Some(HostService::graphql_query::( + req, + self.link.callback(Msg::CreateUserResponse), + "Error trying to create user", + )?); } Msg::CreateUserResponse(r) => { - if r.is_err() { - return r; - } + match r { + Err(e) => return Err(e), + Ok(r) => ConsoleService::log(&format!( + "Created user '{}' at '{}'", + &r.create_user.id, &r.create_user.creation_date + )), + }; let user_id = get_element("username") .filter(not_empty) .ok_or_else(|| anyhow!("Missing username"))?; @@ -183,7 +200,7 @@ impl Component for CreateUserForm { - +
{ if let Some(e) = &self.error { html! { e.to_string() }