morethantext-web/src/morethantext/graphql.rs

180 lines
4.9 KiB
Rust
Raw Normal View History

2022-07-03 10:33:50 -04:00
use async_graphql::{Context, EmptySubscription, Error, Object, Result, Schema};
2022-07-01 12:03:47 -04:00
use async_std::sync::RwLock;
2022-06-30 08:37:27 -04:00
use serde_json;
mod database;
2022-07-01 12:03:47 -04:00
#[derive(Clone)]
2022-06-30 23:56:40 -04:00
struct Table {
name: String,
}
impl Table {
async fn new(name: String) -> Self {
Self { name: name }
}
}
#[Object]
impl Table {
async fn name(&self) -> String {
self.name.to_string()
}
2022-07-08 09:03:35 -04:00
async fn describe(&self) -> Vec<u64> {
Vec::new()
}
2022-06-30 23:56:40 -04:00
}
2022-06-30 08:37:27 -04:00
struct Query;
#[Object]
impl Query {
2022-07-02 12:19:59 -04:00
async fn table(&self, ctx: &Context<'_>, name: String) -> Result<Option<Table>> {
let tbls = ctx
.data::<RwLock<Vec<Table>>>()
.unwrap()
.read()
.await
.to_vec();
2022-07-03 08:43:59 -04:00
match tbls.binary_search_by(|t| t.name.cmp(&name)) {
Ok(idx) => Ok(Some(tbls[idx].clone())),
Err(_) => Ok(None),
}
2022-07-02 12:19:59 -04:00
}
2022-07-01 12:03:47 -04:00
async fn tables(&self, ctx: &Context<'_>) -> Vec<Table> {
ctx.data::<RwLock<Vec<Table>>>()
.unwrap()
.read()
.await
.to_vec()
}
2022-06-30 23:56:40 -04:00
}
struct Mutation;
#[Object]
impl Mutation {
2022-07-01 12:03:47 -04:00
async fn create_table(&self, ctx: &Context<'_>, name: String) -> Result<Option<Table>> {
let mut tables = ctx.data::<RwLock<Vec<Table>>>().unwrap().write().await;
2022-07-03 10:33:50 -04:00
match tables.binary_search_by(|t| t.name.cmp(&name)) {
Ok(_) => Err(Error::new(format!("Table {} already exists.", &name))),
Err(_) => {
let output = Table::new(name).await;
tables.push(output.clone());
tables.sort_by_key(|k| k.name.clone());
Ok(Some(output))
}
}
2022-06-30 23:56:40 -04:00
}
2022-06-30 08:37:27 -04:00
}
#[derive(Clone)]
pub struct MoreThanText {
2022-06-30 23:56:40 -04:00
schema: Schema<Query, Mutation, EmptySubscription>,
2022-06-30 08:37:27 -04:00
}
impl MoreThanText {
pub fn new() -> Self {
2022-07-01 12:03:47 -04:00
let tables: Vec<Table> = Vec::new();
2022-06-30 08:37:27 -04:00
Self {
2022-07-01 12:03:47 -04:00
schema: Schema::build(Query, Mutation, EmptySubscription)
.data(RwLock::new(tables))
.finish(),
2022-06-30 08:37:27 -04:00
}
}
2022-06-27 12:15:12 -04:00
2022-06-30 08:37:27 -04:00
pub async fn execute(&self, qry: &str) -> String {
let res = self.schema.execute(qry).await;
serde_json::to_string(&res).unwrap()
2022-06-27 12:15:12 -04:00
}
}
2022-07-01 12:29:02 -04:00
#[cfg(test)]
mod support {
use super::*;
pub fn compare(db: &MoreThanText, output: &str, expected: &str) {
2022-07-01 12:29:02 -04:00
assert!(
output == expected,
"\n\n{}\nGot: {}\nWant: {}\n\n",
2022-07-01 12:49:09 -04:00
db.schema.sdl(),
2022-07-01 12:29:02 -04:00
output,
expected
);
}
}
2022-06-27 12:15:12 -04:00
#[cfg(test)]
mod queries {
use super::*;
2022-07-02 12:19:59 -04:00
#[async_std::test]
async fn list_table() {
let db = MoreThanText::new();
2022-07-02 12:19:59 -04:00
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);
}
2022-07-03 08:43:59 -04:00
#[async_std::test]
async fn list_no_table() {
let db = MoreThanText::new();
2022-07-03 08:43:59 -04:00
let output = db.execute(r#"{table(name: "slade"){name}}"#).await;
let expected = r#"{"data":{"table":null}}"#;
support::compare(&db, &output, &expected);
}
2022-07-01 12:03:47 -04:00
#[async_std::test]
async fn list_tables() {
let db = MoreThanText::new();
2022-07-01 12:03:47 -04:00
db.execute(r#"mutation {createTable(name: "fred"){name}}"#)
.await;
db.execute(r#"mutation {createTable(name: "barney"){name}}"#)
.await;
let output = db.execute(r#"{tables{name}}"#).await;
2022-07-02 12:19:59 -04:00
let expected = r#"{"data":{"tables":[{"name":"barney"},{"name":"fred"}]}}"#;
2022-07-01 12:29:02 -04:00
support::compare(&db, &output, &expected);
2022-07-01 12:03:47 -04:00
}
2022-07-08 09:03:35 -04:00
#[async_std::test]
async fn empty_table_description() {
let db = MoreThanText::new();
2022-07-08 09:03:35 -04:00
let output = db
.execute(r#"mutation {createTable(name: "pebbles"){name describe}}"#)
.await;
let expected = r#"{"data":{"createTable":{"name":"pebbles","describe":[]}}}"#;
support::compare(&db, &output, &expected);
}
}
2022-06-30 23:56:40 -04:00
#[cfg(test)]
mod mutations {
use super::*;
#[async_std::test]
async fn add_table() {
let db = MoreThanText::new();
2022-06-30 23:56:40 -04:00
let output = db
.execute(r#"mutation {createTable(name: "william"){name}}"#)
.await;
let expected = r#"{"data":{"createTable":{"name":"william"}}}"#;
2022-07-03 10:33:50 -04:00
support::compare(&db, &output, &expected);
}
#[async_std::test]
async fn cannot_add_duplicate_table() {
let db = MoreThanText::new();
2022-07-03 10:33:50 -04:00
let qry = r#"mutation {createTable(name: "gadzoo"){name}}"#;
db.execute(&qry).await;
let output = db.execute(qry).await;
let expected = r#"{"data":null,"errors":[{"message":"Table gadzoo already exists.","locations":[{"line":1,"column":11}],"path":["createTable"]}]}"#;
2022-07-01 12:29:02 -04:00
support::compare(&db, &output, &expected);
2022-06-30 23:56:40 -04:00
}
}