From a669c9cfd90923ccd49045c3f8b2d3752ba6ba79 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Thu, 25 Nov 2021 16:05:26 +0100 Subject: [PATCH] Add `isUsernameUsed` to query methods Fixes #13 Have fun @David! --- src/graphql.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/graphql.rs b/src/graphql.rs index 1e6468e..b75c99e 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -395,6 +395,17 @@ impl Query { .map_err(FieldError::from) } + async fn isUsernameUsed(context: &Context, username: String) -> FieldResult { + if !is_valid_user_name(&username) { + return Err(format!("{:?} is not a valid username", username).into()); + } + + Ok(sqlx::query(format!( + r#"SELECT user_name FROM users WHERE user_name="{}""#, + username, + ).as_str()).fetch_optional(&context.db).await?.is_some()) + } + async fn users(context: &Context) -> FieldResult> { let rows = sqlx::query("SELECT * FROM users") .fetch_all(&context.db).await?;