Added GraphQL to the database.
This commit is contained in:
37
src/lib.rs
37
src/lib.rs
@ -1,13 +1,30 @@
|
||||
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 {}
|
||||
pub struct Database {
|
||||
schema: Schema<Query, EmptyMutation, EmptySubscription>,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
Self {
|
||||
schema: Schema::new(Query, EmptyMutation, EmptySubscription),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query(&self, _qry: &str) -> String {
|
||||
"{data{}}".to_string()
|
||||
pub async fn execute(&self, qry: &str) -> String {
|
||||
let res = self.schema.execute(qry).await;
|
||||
serde_json::to_string(&res).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,15 +33,15 @@ mod queries {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn empty_table_query() {
|
||||
let expected = "{data{}}";
|
||||
let testdb = Database::new();
|
||||
let output = testdb.query("{tables{name}}").await;
|
||||
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 '{}' expected '{}'.",
|
||||
"Got '{}' instead of '{}'.",
|
||||
output,
|
||||
expected
|
||||
);
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user