Added table query.
This commit is contained in:
parent
768a61d1d5
commit
30cb65914d
16
Cargo.toml
16
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 = "*"
|
||||
|
26
src/lib.rs
26
src/lib.rs
@ -24,6 +24,17 @@ struct Query;
|
||||
|
||||
#[Object]
|
||||
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> {
|
||||
ctx.data::<RwLock<Vec<Table>>>()
|
||||
.unwrap()
|
||||
@ -41,6 +52,7 @@ impl Mutation {
|
||||
let mut tables = ctx.data::<RwLock<Vec<Table>>>().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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user