From a1a37d418af645c804983fd3c8380eda889125b9 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sat, 5 Jun 2021 22:31:59 +0000 Subject: [PATCH] Add schema generating binary to API server, so we can then proceed with the UI client graphql calls. --- Cargo.lock | 231 +++++++++++++++++++++++++++++---- api/src/bin/generate_schema.rs | 4 + api/src/lib.rs | 1 + api/src/main.rs | 93 +------------ api/src/schema.rs | 87 +++++++++++++ web-ui/crate/Cargo.toml | 3 +- web-ui/crate/src/graphql.rs | 7 + web-ui/crate/src/lib.rs | 1 + 8 files changed, 315 insertions(+), 112 deletions(-) create mode 100644 api/src/bin/generate_schema.rs create mode 100644 api/src/lib.rs create mode 100644 api/src/schema.rs create mode 100644 web-ui/crate/src/graphql.rs diff --git a/Cargo.lock b/Cargo.lock index dfc1478..898757c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,20 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "addr2line" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a2e47a1fbe209ee101dd6d61285226744c6c8d3c21c8dc878ba6cb9f467f3a" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aead" version = "0.3.2" @@ -218,6 +233,21 @@ dependencies = [ "tokio", ] +[[package]] +name = "backtrace" +version = "0.3.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7815ea54e4d821e791162e078acbebfd6d8c8939cd559c9335dceb1c8ca7282" +dependencies = [ + "addr2line", + "cc", + "cfg-if 1.0.0", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "barrel" version = "0.6.5" @@ -685,6 +715,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "dotenv" version = "0.15.0" @@ -709,6 +745,28 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "fallible-iterator" version = "0.2.0" @@ -807,6 +865,12 @@ dependencies = [ "new_debug_unreachable", ] +[[package]] +name = "futures" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" + [[package]] name = "futures" version = "0.3.15" @@ -872,7 +936,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50c4e684ddb2d8a4db5ca8a02b35156da129674ba4412b6f528698d58c594954" dependencies = [ - "futures", + "futures 0.3.15", ] [[package]] @@ -997,6 +1061,12 @@ dependencies = [ "polyval", ] +[[package]] +name = "gimli" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" + [[package]] name = "glob" version = "0.3.0" @@ -1059,6 +1129,25 @@ dependencies = [ "web-sys", ] +[[package]] +name = "graphql-introspection-query" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "610aac641dbd2a457ad4cef34aa2827dae3f035fd214cb38c2d62d8543f3973f" +dependencies = [ + "serde", +] + +[[package]] +name = "graphql-parser" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5613c31f18676f164112732202124f373bb2103ff017b3b85ca954ea6a66ada" +dependencies = [ + "combine 3.8.1", + "failure", +] + [[package]] name = "graphql-parser" version = "0.3.0" @@ -1069,6 +1158,64 @@ dependencies = [ "thiserror", ] +[[package]] +name = "graphql_client" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0bb4f09181e4f80018d01c612125b07e0156f3753bfac37055fe2a25e031ca8" +dependencies = [ + "doc-comment", + "failure", + "futures 0.1.31", + "graphql_query_derive", + "js-sys", + "log", + "serde", + "serde_json", + "wasm-bindgen", + "wasm-bindgen-futures 0.3.27", + "web-sys", +] + +[[package]] +name = "graphql_client_codegen" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e304c223c809b3bff4614018f8e6d9edb176b31d64ed9ea48b6ae8b1a03abb9" +dependencies = [ + "failure", + "graphql-introspection-query", + "graphql-parser 0.2.3", + "heck", + "lazy_static", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn", +] + +[[package]] +name = "graphql_client_web" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36901b00f3608f510a091b70848869e1ff1f9b44970b0b882ee62502fca431f0" +dependencies = [ + "graphql_client", +] + +[[package]] +name = "graphql_query_derive" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1f6b14d5ce549227aa9e649cd9d36d008b91021275a8e0a67d71cef815adc2f" +dependencies = [ + "failure", + "graphql_client_codegen", + "proc-macro2", + "syn", +] + [[package]] name = "grpc-web-client" version = "0.1.2" @@ -1078,7 +1225,7 @@ dependencies = [ "base64", "byteorder", "bytes", - "futures", + "futures 0.3.15", "getrandom 0.2.3", "http", "http-body", @@ -1088,7 +1235,7 @@ dependencies = [ "prost", "tonic", "wasm-bindgen", - "wasm-bindgen-futures", + "wasm-bindgen-futures 0.4.24", "wasm-streams", "web-sys", ] @@ -1367,9 +1514,9 @@ dependencies = [ "bson", "chrono", "fnv", - "futures", + "futures 0.3.15", "futures-enum", - "graphql-parser", + "graphql-parser 0.3.0", "indexmap", "juniper_codegen", "serde", @@ -1395,7 +1542,7 @@ name = "juniper_rocket_async" version = "0.5.1" source = "git+https://github.com/graphql-rust/juniper?branch=master#92ad8b46ff4d34cf10925f0a86b56950a354d2ec" dependencies = [ - "futures", + "futures 0.3.15", "juniper", "rocket", "serde_json", @@ -1544,7 +1691,7 @@ dependencies = [ "backoff", "bytes", "dashmap", - "futures", + "futures 0.3.15", "futures-timer", "http", "matrix-sdk-base", @@ -1567,7 +1714,7 @@ source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#0df782 dependencies = [ "chacha20poly1305", "dashmap", - "futures", + "futures 0.3.15", "hmac", "lru", "matrix-sdk-common", @@ -1590,14 +1737,14 @@ version = "0.2.0" source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#0df782e93ee2d9d9c338775adbe2cf1b35048740" dependencies = [ "async-trait", - "futures", + "futures 0.3.15", "futures-locks", "instant", "ruma", "serde", "tokio", "uuid", - "wasm-bindgen-futures", + "wasm-bindgen-futures 0.4.24", ] [[package]] @@ -1611,7 +1758,7 @@ dependencies = [ "base64", "byteorder", "dashmap", - "futures", + "futures 0.3.15", "getrandom 0.2.3", "hmac", "matrix-sdk-common", @@ -1647,6 +1794,16 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + [[package]] name = "mio" version = "0.7.11" @@ -1782,6 +1939,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8bc1d42047cf336f0f939c99e97183cf31551bf0f2865a2ec9c8d91fd4ffb5e" +dependencies = [ + "memchr", +] + [[package]] name = "olm-rs" version = "1.0.1" @@ -2425,7 +2591,7 @@ dependencies = [ "tokio-native-tls", "url", "wasm-bindgen", - "wasm-bindgen-futures", + "wasm-bindgen-futures 0.4.24", "web-sys", "winreg", ] @@ -2458,7 +2624,7 @@ dependencies = [ "bytes", "either", "figment", - "futures", + "futures 0.3.15", "indexmap", "log", "memchr", @@ -2752,6 +2918,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "rustc-demangle" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "410f7acf3cb3a44527c5d9546bad4bf4e6c460915d5f9f2fc524498bfe8f70ce" + [[package]] name = "rustc_version" version = "0.2.3" @@ -3066,7 +3238,7 @@ checksum = "01caee2b3935b4efe152f3262afbe51546ce3b1fc27ad61014e1b3cf5f55366e" dependencies = [ "dotenv", "either", - "futures", + "futures 0.3.15", "heck", "hex", "once_cell", @@ -3301,7 +3473,7 @@ dependencies = [ "combine 4.5.2", "dirs", "fuse-rust", - "futures", + "futures 0.3.15", "html2text", "indoc", "itertools 0.10.0", @@ -3340,10 +3512,12 @@ dependencies = [ name = "tenebrous-web-ui" version = "0.1.0" dependencies = [ + "graphql_client", + "graphql_client_web", "grpc-web-client", "js-sys", "wasm-bindgen", - "wasm-bindgen-futures", + "wasm-bindgen-futures 0.4.24", "web-sys", "yew", "yew-router", @@ -3903,6 +4077,19 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83420b37346c311b9ed822af41ec2e82839bfe99867ec6c54e2da43b7538771c" +dependencies = [ + "cfg-if 0.1.10", + "futures 0.1.31", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-futures" version = "0.4.24" @@ -3950,10 +4137,10 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4f8387cd5b13b87d140234b5be9ab91e9324184fa5170b2778bc4bd1390683d" dependencies = [ - "futures", + "futures 0.3.15", "js-sys", "wasm-bindgen", - "wasm-bindgen-futures", + "wasm-bindgen-futures 0.4.24", "web-sys", ] @@ -4069,7 +4256,7 @@ dependencies = [ "cfg-if 0.1.10", "cfg-match", "console_error_panic_hook", - "futures", + "futures 0.3.15", "gloo", "http", "indexmap", @@ -4083,7 +4270,7 @@ dependencies = [ "slab", "thiserror", "wasm-bindgen", - "wasm-bindgen-futures", + "wasm-bindgen-futures 0.4.24", "web-sys", "yew-macro", ] @@ -4160,10 +4347,10 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "003f4f533ee696c1f69d753db2c66096dec5700a93bd0bc801ab5891fec041c6" dependencies = [ - "futures", + "futures 0.3.15", "log", "wasm-bindgen", - "wasm-bindgen-futures", + "wasm-bindgen-futures 0.4.24", "yew", ] diff --git a/api/src/bin/generate_schema.rs b/api/src/bin/generate_schema.rs new file mode 100644 index 0000000..b53f3a8 --- /dev/null +++ b/api/src/bin/generate_schema.rs @@ -0,0 +1,4 @@ +use tenebrous_api::schema; +fn main() { + println!("{}", schema::schema().as_schema_language()); +} diff --git a/api/src/lib.rs b/api/src/lib.rs new file mode 100644 index 0000000..1ce7e17 --- /dev/null +++ b/api/src/lib.rs @@ -0,0 +1 @@ +pub mod schema; diff --git a/api/src/main.rs b/api/src/main.rs index 2824b92..131ba9c 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -1,94 +1,8 @@ -use juniper::{ - graphql_object, EmptyMutation, EmptySubscription, FieldResult, GraphQLInputObject, - GraphQLObject, RootNode, -}; use rocket::{response::content, Rocket, State}; use std::env; -use tenebrous_rpc::protos::dicebot::dicebot_client::DicebotClient; -use tenebrous_rpc::protos::dicebot::GetVariableRequest; -use tonic::{transport::Channel as TonicChannel, Request as TonicRequest}; +use tenebrous_api::schema::{self, Context, Schema}; use tracing_subscriber::filter::EnvFilter; -//api stuff -#[derive(GraphQLInputObject)] -struct UserVariableArgument { - room_id: String, - user_id: String, - variable_name: String, -} - -#[derive(GraphQLObject)] -#[graphql(description = "User variable in a room.")] -struct UserVariable { - room_id: String, - user_id: String, - variable_name: String, - value: i32, -} - -//graphql shit -#[derive(Clone)] -struct Context { - dicebot_client: DicebotClient, -} - -// To make our context usable by Juniper, we have to implement a marker trait. -impl juniper::Context for Context {} - -#[derive(Clone, Copy, Debug)] -struct Query; - -#[graphql_object( - // Here we specify the context type for the object. - // We need to do this in every type that - // needs access to the context. - context = Context, -)] -impl Query { - fn apiVersion() -> &str { - "1.0" - } - - async fn variable( - context: &mut Context, - room_id: String, - user_id: String, - variable: String, - ) -> FieldResult { - let request = TonicRequest::new(GetVariableRequest { - room_id: room_id.clone(), - user_id: user_id.clone(), - variable_name: variable.clone(), - }); - - let response = context - .dicebot_client - .clone() - .get_variable(request) - .await? - .into_inner(); - - Ok(UserVariable { - user_id: user_id.clone(), - room_id: room_id.clone(), - variable_name: variable.clone(), - value: response.value, - }) - } -} - -type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>; - -fn schema() -> Schema { - Schema::new( - Query, - EmptyMutation::::new(), - EmptySubscription::::new(), - ) -} - -//rocket stuff - #[rocket::get("/")] fn graphiql() -> content::Html { juniper_rocket_async::graphiql_source("/graphql", None) @@ -114,7 +28,6 @@ async fn post_graphql_handler( #[rocket::main] pub async fn main() -> Result<(), Box> { - println!("hi"); let filter = if env::var("RUST_LOG").is_ok() { EnvFilter::from_default_env() } else { @@ -131,9 +44,11 @@ pub async fn main() -> Result<(), Box> { dicebot_client: client, }; + let schema = schema::schema(); + Rocket::build() .manage(context) - .manage(schema()) + .manage(schema) .mount( "/", rocket::routes![graphiql, get_graphql_handler, post_graphql_handler], diff --git a/api/src/schema.rs b/api/src/schema.rs new file mode 100644 index 0000000..9ab961a --- /dev/null +++ b/api/src/schema.rs @@ -0,0 +1,87 @@ +use juniper::{ + graphql_object, EmptyMutation, EmptySubscription, FieldResult, GraphQLInputObject, + GraphQLObject, RootNode, +}; +use rocket::{response::content, Rocket, State}; +use std::env; +use tenebrous_rpc::protos::dicebot::dicebot_client::DicebotClient; +use tenebrous_rpc::protos::dicebot::GetVariableRequest; +use tonic::{transport::Channel as TonicChannel, Request as TonicRequest}; + +//api stuff +#[derive(GraphQLInputObject)] +struct UserVariableArgument { + room_id: String, + user_id: String, + variable_name: String, +} + +#[derive(GraphQLObject)] +#[graphql(description = "User variable in a room.")] +struct UserVariable { + room_id: String, + user_id: String, + variable_name: String, + value: i32, +} + +//graphql shit +#[derive(Clone)] +pub struct Context { + pub dicebot_client: DicebotClient, +} + +// To make our context usable by Juniper, we have to implement a marker trait. +impl juniper::Context for Context {} + +#[derive(Clone, Copy, Debug)] +pub struct Query; + +#[graphql_object( + // Here we specify the context type for the object. + // We need to do this in every type that + // needs access to the context. + context = Context, +)] +impl Query { + fn apiVersion() -> &str { + "1.0" + } + + async fn variable( + context: &Context, + room_id: String, + user_id: String, + variable: String, + ) -> FieldResult { + let request = TonicRequest::new(GetVariableRequest { + room_id: room_id.clone(), + user_id: user_id.clone(), + variable_name: variable.clone(), + }); + + let response = context + .dicebot_client + .clone() + .get_variable(request) + .await? + .into_inner(); + + Ok(UserVariable { + user_id: user_id.clone(), + room_id: room_id.clone(), + variable_name: variable.clone(), + value: response.value, + }) + } +} + +pub type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>; + +pub fn schema() -> Schema { + Schema::new( + Query, + EmptyMutation::::new(), + EmptySubscription::::new(), + ) +} diff --git a/web-ui/crate/Cargo.toml b/web-ui/crate/Cargo.toml index 8fc564a..ad242c2 100644 --- a/web-ui/crate/Cargo.toml +++ b/web-ui/crate/Cargo.toml @@ -19,7 +19,8 @@ wasm-bindgen-futures = "0.4" js-sys = "0.3" grpc-web-client = "0.1" web-sys = "0.3" - +graphql_client = "0.9" +graphql_client_web = "0.9" # hopefully we can add grpc-web later instead of graphql. # prost = { version = "0.7.0", default-features = false } # tonic = { git = "https://github.com/hyperium/tonic", branch = "master", default-features = false, features = ["codegen", "prost"] } diff --git a/web-ui/crate/src/graphql.rs b/web-ui/crate/src/graphql.rs new file mode 100644 index 0000000..2caec07 --- /dev/null +++ b/web-ui/crate/src/graphql.rs @@ -0,0 +1,7 @@ +// #[derive(GraphQLQuery)] +// #[graphql( +// schema_path = "schema.json", +// query_path = "examples/puppy_smiles.graphql", +// response_derives = "Debug" +// )] +// struct PuppySmiles; diff --git a/web-ui/crate/src/lib.rs b/web-ui/crate/src/lib.rs index 1504519..317cd8c 100644 --- a/web-ui/crate/src/lib.rs +++ b/web-ui/crate/src/lib.rs @@ -3,6 +3,7 @@ use wasm_bindgen::prelude::*; use yew::prelude::*; use yew_router::{components::RouterAnchor, prelude::*}; +pub mod graphql; pub mod grpc; pub mod rooms;