use async_graphql::{Context, EmptySubscription, Object, Result, Schema}; use async_std::sync::RwLock; use serde_json; #[derive(Clone)] 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() } } struct Query; #[Object] impl Query { async fn pointless(&self) -> String { "this is pointless.".to_string() } async fn tables(&self, ctx: &Context<'_>) -> Vec { ctx.data::>>() .unwrap() .read() .await .to_vec() } } struct Mutation; #[Object] impl Mutation { async fn create_table(&self, ctx: &Context<'_>, name: String) -> Result> { let mut tables = ctx.data::>>().unwrap().write().await; let output = Table::new(name).await; tables.push(output.clone()); Ok(Some(output)) } } #[derive(Clone)] pub struct Database { schema: Schema, } impl Database { pub fn new() -> Self { let tables: Vec
= Vec::new(); Self { schema: Schema::build(Query, Mutation, EmptySubscription) .data(RwLock::new(tables)) .finish(), } } pub async fn execute(&self, qry: &str) -> String { let res = self.schema.execute(qry).await; serde_json::to_string(&res).unwrap() } } #[cfg(test)] mod queries { use super::*; #[async_std::test] async fn working() { let db = Database::new(); let output = db.execute("{pointless}").await; let expected = r#"{"data":{"pointless":"this is pointless."}}"#; assert!( output == expected, "Got '{}' instead of '{}'.", output, expected ) } #[async_std::test] async fn list_tables() { let db = Database::new(); 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; let expected = r#"{"data":{"tables":[{"name":"fred"},{"name":"barney"}]}}"#; assert!( output == expected, "\n\n{}\nGot: {}\nWant: {}\n\n", &db.schema.sdl(), output, expected ); } } #[cfg(test)] mod mutations { use super::*; #[async_std::test] async fn add_table() { let db = Database::new(); let output = db .execute(r#"mutation {createTable(name: "william"){name}}"#) .await; let expected = r#"{"data":{"createTable":{"name":"william"}}}"#; println!("{}", &db.schema.sdl()); assert!( output == expected, "Got '{}' instead of '{}'.", output, expected ) } }