morethantext-web/src/lib.rs

48 lines
1.0 KiB
Rust

use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema};
use serde_json;
struct Query;
#[Object]
impl Query {
async fn pointless(&self) -> String {
"this is pointless.".to_string()
}
}
#[derive(Clone)]
pub struct Database {
schema: Schema<Query, EmptyMutation, EmptySubscription>,
}
impl Database {
pub fn new() -> Self {
Self {
schema: Schema::new(Query, EmptyMutation, EmptySubscription),
}
}
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
)
}
}