Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic GraphQL API #63

Closed
wants to merge 12 commits into from
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions aquadoggo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition = "2018"

[dependencies]
anyhow = "1.0.43"
apollo-parser = "0.2.1"
async-graphql = "3.0.25"
async-graphql-tide = "3.0.25"
async-std = { version = "1.10.0", features = ["attributes"] }
Expand All @@ -34,6 +35,7 @@ openssl-probe = "0.1.4"
# @TODO: Move this back to crate as soon as upcoming changes have been published
p2panda-rs = { git = "https://github.com/p2panda/p2panda", branch = "document" }
rand = "0.8.4"
sea-query = "0.20.0"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.67"
sqlformat = "0.1.7"
Expand Down
7 changes: 6 additions & 1 deletion aquadoggo/src/graphql/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_graphql::{EmptyMutation, EmptySubscription, Schema};
use tide::{http::mime, Body, Response, StatusCode};

use crate::db::Pool;
use crate::graphql::convert::gql_to_sql;
use crate::graphql::QueryRoot;
use crate::server::{ApiRequest, ApiResult, ApiState};

Expand Down Expand Up @@ -56,7 +57,11 @@ impl tide::Endpoint<ApiState> for Endpoint {
///
/// This handler resolves all incoming GraphQL requests and answers them both for dynamically
/// and statically generated schemas.
async fn call(&self, request: ApiRequest) -> ApiResult {
async fn call(&self, mut request: ApiRequest) -> ApiResult {
let graphql_request = request.body_json::<async_graphql::Request>().await?;
let sql = gql_to_sql(&graphql_request.query).unwrap();
println!("{:?}", sql);

// @TODO: Implement handling of dynamic GraphQL requests. Route all GraphQL requests to
// static handler for now.
self.static_endpoint.call(request).await
Expand Down
44 changes: 44 additions & 0 deletions aquadoggo/src/graphql/convert/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

mod query;
mod schema;
mod sql;

pub use query::{AbstractQuery, AbstractQueryError, Argument, Field, MetaField};
pub use schema::{Schema, SchemaParseError};
pub use sql::query_to_sql;

pub fn gql_to_sql(query: &str) -> anyhow::Result<String> {
// Convert GraphQL query to our own abstract query representation
let root = AbstractQuery::new(query)?;

// Convert to SQL query
let sql = query_to_sql(root).unwrap();

Ok(sql)
}

#[cfg(test)]
mod tests {
use super::gql_to_sql;

#[test]
fn parser() {
let query = "{
festival_events_0020c65567ae37efea293e34a9c7d13f8f2bf23dbdc3b5c7b9ab46293111c48fc78b(document: \"0020b177ec1bf26dfb3b7010d473e6d44713b29b765b99c6e60ecbfae742de496543\") {
document
fields {
title
description
}
}
}";

let sql = gql_to_sql(query).unwrap();

assert_eq!(
sql,
"SELECT \"title\", \"description\", \"document\" FROM \"festival_events_0020c65567ae37efea293e34a9c7d13f8f2bf23dbdc3b5c7b9ab46293111c48fc78b\" WHERE \"document\" = '0020b177ec1bf26dfb3b7010d473e6d44713b29b765b99c6e60ecbfae742de496543'"
);
}
}
Loading