48 lines
1.0 KiB
Rust
Raw Normal View History

2022-06-30 08:37:27 -04:00
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)]
2022-06-30 08:37:27 -04:00
pub struct Database {
schema: Schema<Query, EmptyMutation, EmptySubscription>,
}
impl Database {
pub fn new() -> Self {
2022-06-30 08:37:27 -04:00
Self {
schema: Schema::new(Query, EmptyMutation, EmptySubscription),
}
}
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
}
}
#[cfg(test)]
mod queries {
use super::*;
#[async_std::test]
2022-06-30 08:37:27 -04:00
async fn working() {
let db = Database::new();
let output = db.execute("{pointless}").await;
let expected = r#"{"data":{"pointless":"this is pointless."}}"#;
2022-06-27 12:15:12 -04:00
assert!(
output == expected,
2022-06-30 08:37:27 -04:00
"Got '{}' instead of '{}'.",
2022-06-27 12:15:12 -04:00
output,
expected
2022-06-30 08:37:27 -04:00
)
2022-06-27 12:15:12 -04:00
}
}