diff --git a/src/graphql/error_types.rs b/src/graphql/error_types.rs index c31c2ea..8716581 100644 --- a/src/graphql/error_types.rs +++ b/src/graphql/error_types.rs @@ -38,4 +38,16 @@ pub fn field_does_not_exist_on_table(field: &str, table: &str) -> String { println!("{}", error); "Field does not exist on table".to_string() +} + +pub fn table_name_does_not_end_with_collection(table_name: &str) -> String { + let error: String = format!("\x1b[1;31mTable name does not end with \x1b[1;34m`Collection`\x1b[1;31m: {}\x1b[0m", table_name).to_string(); + let arrow_amount: String = "^".repeat(table_name.len()); + + println!("{}", error); + println!("\x1b[1;34m{}\x1b[0m\x1b[1;32mCollection\x1b[0m", table_name); + println!("\x1b[1;34m{}\x1b[0m", arrow_amount); + println!("Add Collection to the end of the table name and try again"); + + "Table name does not end with Collection".to_string() } \ No newline at end of file diff --git a/src/graphql/parse.rs b/src/graphql/parse.rs index b411ade..18221c5 100644 --- a/src/graphql/parse.rs +++ b/src/graphql/parse.rs @@ -1,5 +1,6 @@ use anyhow::{Result, Error}; use serde_json::Value; +use crate::graphql::error_types::{illegal_table_name, table_does_not_exist, field_does_not_exist_on_table, table_name_does_not_end_with_collection}; pub fn parse_outer(query: &Value) -> bool { @@ -18,20 +19,31 @@ pub fn parse_outer(query: &Value) -> bool { pub fn get_table_name(query: &Value) -> Result { if parse_outer(query) { - let table_name: String = query["query"].as_str().unwrap_or("").to_string(); - - // remove the brackets - let table_name: String = table_name.replace("{", "").replace("}", ""); - - // first word is table name - let table_name: String = table_name.split_whitespace().next().unwrap().to_string(); - - // break the word off as soon as its not alphanumeric - let table_name: String = table_name.chars().take_while(|c| c.is_alphanumeric()).collect(); - + let query_str: &str = query["query"].as_str().unwrap_or(""); + println!("Query: {}", query_str); + // remove all the { } and then get the first alphanumeric word from the query + let query_str: String = query_str.replace("{", "").replace("}", ""); + let query_str: String = query_str.trim().to_string(); + let query_str: Vec<&str> = query_str.split_whitespace().collect(); + let mut table_name: String = query_str[0].to_string(); + + // remove all beyond the last alphanumeric char + for (i, c) in table_name.chars().enumerate() { + if !c.is_alphanumeric() { + table_name = table_name[..i].to_string(); + break; + } + } + + // if the table name doesnt end with Collection, add it + if !table_name.ends_with("Collection") { + table_name_does_not_end_with_collection(&table_name); + } Ok(table_name) } else { Err(Error::msg("Invalid outer structure")) } -} \ No newline at end of file +} + + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d23a3f2..af70a61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,9 @@ async fn main() { usersCollection(first: 1) { edges { node { - user_id + user_id, + username, + email } } }