Added table query.

This commit is contained in:
Jeff Baskin 2022-07-02 12:19:59 -04:00
parent 768a61d1d5
commit 30cb65914d
2 changed files with 33 additions and 9 deletions

View File

@ -6,13 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
async-graphql = "4.0" async-graphql = "*"
async-std = { version = "1.11", features = ["attributes"] } async-std = { version = "*", features = ["attributes"] }
config = "0.13" config = "*"
serde = "1.0" serde = "*"
serde_json = "1.0" serde_json = "*"
tide = "0.16" tide = "*"
[dev-dependencies] [dev-dependencies]
serial_test = "0.7" serial_test = "*"
tide-testing = "0.1" tide-testing = "*"

View File

@ -24,6 +24,17 @@ struct Query;
#[Object] #[Object]
impl Query { impl Query {
async fn table(&self, ctx: &Context<'_>, name: String) -> Result<Option<Table>> {
let tbls = ctx
.data::<RwLock<Vec<Table>>>()
.unwrap()
.read()
.await
.to_vec();
let idx = tbls.binary_search_by(|t| t.name.cmp(&name)).unwrap();
Ok(Some(tbls[idx].clone()))
}
async fn tables(&self, ctx: &Context<'_>) -> Vec<Table> { async fn tables(&self, ctx: &Context<'_>) -> Vec<Table> {
ctx.data::<RwLock<Vec<Table>>>() ctx.data::<RwLock<Vec<Table>>>()
.unwrap() .unwrap()
@ -41,6 +52,7 @@ impl Mutation {
let mut tables = ctx.data::<RwLock<Vec<Table>>>().unwrap().write().await; let mut tables = ctx.data::<RwLock<Vec<Table>>>().unwrap().write().await;
let output = Table::new(name).await; let output = Table::new(name).await;
tables.push(output.clone()); tables.push(output.clone());
tables.sort_by_key(|k| k.name.clone());
Ok(Some(output)) Ok(Some(output))
} }
} }
@ -85,6 +97,18 @@ mod support {
mod queries { mod queries {
use super::*; use super::*;
#[async_std::test]
async fn list_table() {
let db = Database::new();
db.execute(r#"mutation {createTable(name: "wilma"){name}}"#)
.await;
db.execute(r#"mutation {createTable(name: "betty"){name}}"#)
.await;
let output = db.execute(r#"{table(name: "wilma"){name}}"#).await;
let expected = r#"{"data":{"table":{"name":"wilma"}}}"#;
support::compare(&db, &output, &expected);
}
#[async_std::test] #[async_std::test]
async fn list_tables() { async fn list_tables() {
let db = Database::new(); let db = Database::new();
@ -93,7 +117,7 @@ mod queries {
db.execute(r#"mutation {createTable(name: "barney"){name}}"#) db.execute(r#"mutation {createTable(name: "barney"){name}}"#)
.await; .await;
let output = db.execute(r#"{tables{name}}"#).await; let output = db.execute(r#"{tables{name}}"#).await;
let expected = r#"{"data":{"tables":[{"name":"fred"},{"name":"barney"}]}}"#; let expected = r#"{"data":{"tables":[{"name":"barney"},{"name":"fred"}]}}"#;
support::compare(&db, &output, &expected); support::compare(&db, &output, &expected);
} }
} }