From 30cb65914d20eeb24ebcab753d929c7fcdc0f925 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 2 Jul 2022 12:19:59 -0400 Subject: [PATCH] Added table query. --- Cargo.toml | 16 ++++++++-------- src/lib.rs | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d9705ff..ead48d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,13 +6,13 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -async-graphql = "4.0" -async-std = { version = "1.11", features = ["attributes"] } -config = "0.13" -serde = "1.0" -serde_json = "1.0" -tide = "0.16" +async-graphql = "*" +async-std = { version = "*", features = ["attributes"] } +config = "*" +serde = "*" +serde_json = "*" +tide = "*" [dev-dependencies] -serial_test = "0.7" -tide-testing = "0.1" +serial_test = "*" +tide-testing = "*" diff --git a/src/lib.rs b/src/lib.rs index 74da442..855a897 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,17 @@ struct Query; #[Object] impl Query { + async fn table(&self, ctx: &Context<'_>, name: String) -> Result> { + let tbls = ctx + .data::>>() + .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 { ctx.data::>>() .unwrap() @@ -41,6 +52,7 @@ impl Mutation { let mut tables = ctx.data::>>().unwrap().write().await; let output = Table::new(name).await; tables.push(output.clone()); + tables.sort_by_key(|k| k.name.clone()); Ok(Some(output)) } } @@ -85,6 +97,18 @@ mod support { mod queries { 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 fn list_tables() { let db = Database::new(); @@ -93,7 +117,7 @@ mod queries { db.execute(r#"mutation {createTable(name: "barney"){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); } }