From 595cd99b0be0672ec5896ca9424988d5f45cae36 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 30 Jun 2022 23:56:40 -0400 Subject: [PATCH] Added tables to database. --- src/lib.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0349d84..ff02fdf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,23 @@ -use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema}; +use async_graphql::{EmptySubscription, Object, Result, Schema}; use serde_json; +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] @@ -8,17 +25,30 @@ impl Query { async fn pointless(&self) -> String { "this is pointless.".to_string() } + + //async fn create_table(&self, name: String) -> Result> { + // Ok(Some(Table::new(name).await)) + //} +} + +struct Mutation; + +#[Object] +impl Mutation { + async fn create_table(&self, name: String) -> Result> { + Ok(Some(Table::new(name).await)) + } } #[derive(Clone)] pub struct Database { - schema: Schema, + schema: Schema, } impl Database { pub fn new() -> Self { Self { - schema: Schema::new(Query, EmptyMutation, EmptySubscription), + schema: Schema::new(Query, Mutation, EmptySubscription), } } @@ -45,3 +75,24 @@ mod queries { ) } } + +#[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 + ) + } +}