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?;