From eba5a14926c8bde8868684a264ef7f8db51eea50 Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Sun, 25 Apr 2021 08:06:34 -0700 Subject: [PATCH] [honk] wip graphql server --- honk/Cargo.lock | 272 +++++++++++++++++++++++++++++++++++++++++++++++- honk/Cargo.toml | 2 + honk/src/gql.rs | 47 +++++++++ honk/src/lib.rs | 32 ++++-- 4 files changed, 342 insertions(+), 11 deletions(-) create mode 100644 honk/src/gql.rs diff --git a/honk/Cargo.lock b/honk/Cargo.lock index 0a1fdabc..453e4a18 100644 --- a/honk/Cargo.lock +++ b/honk/Cargo.lock @@ -12,6 +12,31 @@ dependencies = [ "regex", ] +[[package]] +name = "actix" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1be241f88f3b1e7e9a3fbe3b5a8a0f6915b5a1d7ee0d9a248d3376d01068cc60" +dependencies = [ + "actix-rt", + "actix_derive", + "bitflags", + "bytes 0.5.6", + "crossbeam-channel", + "derive_more", + "futures-channel", + "futures-util", + "log", + "once_cell", + "parking_lot", + "pin-project 0.4.28", + "smallvec", + "tokio", + "tokio-util", + "trust-dns-proto", + "trust-dns-resolver", +] + [[package]] name = "actix-codec" version = "0.3.0" @@ -91,7 +116,7 @@ dependencies = [ "serde_urlencoded", "sha-1", "slab", - "time", + "time 0.2.26", ] [[package]] @@ -257,11 +282,27 @@ dependencies = [ "serde_json", "serde_urlencoded", "socket2", - "time", + "time 0.2.26", "tinyvec", "url", ] +[[package]] +name = "actix-web-actors" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6edf3c2693e2a8c422800c87ee89a6a4eac7dd01109bc172a1093ce1f4f001" +dependencies = [ + "actix", + "actix-codec", + "actix-http", + "actix-web", + "bytes 0.5.6", + "futures-channel", + "futures-core", + "pin-project 0.4.28", +] + [[package]] name = "actix-web-codegen" version = "0.4.0" @@ -273,6 +314,17 @@ dependencies = [ "syn", ] +[[package]] +name = "actix_derive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b95aceadaf327f18f0df5962fedc1bde2f870566a0b9f65c89508a3b1f79334c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "addr2line" version = "0.14.1" @@ -372,6 +424,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "ascii" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" + [[package]] name = "ascii-canvas" version = "2.0.0" @@ -532,6 +590,23 @@ dependencies = [ "libc", ] +[[package]] +name = "bson" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b6553abdb9d2d8f262f0b5bccf807321d5b7d1a12796bcede8e1f150e85f2e" +dependencies = [ + "base64 0.13.0", + "chrono", + "hex", + "lazy_static", + "linked-hash-map", + "rand", + "serde", + "serde_json", + "uuid", +] + [[package]] name = "bumpalo" version = "3.6.1" @@ -592,6 +667,7 @@ dependencies = [ "libc", "num-integer", "num-traits", + "time 0.1.43", "winapi 0.3.9", ] @@ -637,6 +713,19 @@ dependencies = [ "tracing-error", ] +[[package]] +name = "combine" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" +dependencies = [ + "ascii", + "byteorder", + "either", + "memchr", + "unreachable", +] + [[package]] name = "const_fn" version = "0.4.6" @@ -662,7 +751,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951" dependencies = [ "percent-encoding", - "time", + "time 0.2.26", "version_check", ] @@ -759,6 +848,17 @@ dependencies = [ "syn", ] +[[package]] +name = "derive_utils" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "532b4c15dccee12c7044f1fcad956e98410860b22231e44a3b827464797ca7bf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "diff" version = "0.1.12" @@ -963,6 +1063,7 @@ checksum = "a9d5813545e459ad3ca1bff9915e9ad7f1a47dc6a91b627ce321d5863b7dd253" dependencies = [ "futures-channel", "futures-core", + "futures-executor", "futures-io", "futures-sink", "futures-task", @@ -985,6 +1086,28 @@ version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "098cd1c6dda6ca01650f1a37a794245eb73181d0d4d4e955e2f3c37db7af1815" +[[package]] +name = "futures-enum" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3422d14de7903a52e9dbc10ae05a7e14445ec61890100e098754e120b2bd7b1e" +dependencies = [ + "derive_utils", + "quote", + "syn", +] + +[[package]] +name = "futures-executor" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10f6cb7042eda00f0049b1d2080aa4b93442997ee507eb3828e8bd7577f94c9d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.14" @@ -1108,6 +1231,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "graphql-parser" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1abd4ce5247dfc04a03ccde70f87a048458c9356c7e41d21ad8c407b3dde6f2" +dependencies = [ + "combine", + "thiserror", +] + [[package]] name = "h2" version = "0.2.7" @@ -1152,6 +1285,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "honk" version = "7.0.0" @@ -1163,6 +1302,8 @@ dependencies = [ "crossbeam-channel", "gazebo", "glob", + "juniper", + "juniper_actix", "memofs", "once_cell", "parking_lot", @@ -1229,6 +1370,7 @@ checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" dependencies = [ "autocfg", "hashbrown", + "serde", ] [[package]] @@ -1305,6 +1447,81 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +[[package]] +name = "juniper" +version = "0.15.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e1330b4b3b2fe7de256fd08738f7fba28ffa574109c834988c28972f0760dbd" +dependencies = [ + "async-trait", + "bson", + "chrono", + "fnv", + "futures", + "futures-enum", + "graphql-parser", + "indexmap", + "juniper_codegen", + "serde", + "smartstring", + "static_assertions", + "url", + "uuid", +] + +[[package]] +name = "juniper_actix" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3afc38d7583ee66c8a0f7e18e9b7de55cc5717e19990eb4ae7280a0094a2f3c" +dependencies = [ + "actix", + "actix-web", + "actix-web-actors", + "anyhow", + "futures", + "juniper", + "juniper_graphql_ws", + "serde", + "serde_json", + "thiserror", + "tokio", +] + +[[package]] +name = "juniper_codegen" +version = "0.15.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3faf2a1e2e86cadc6bcc6082d63c73e8eafe6b703a0c5723e585b3d4afcec9cd" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "juniper_graphql_ws" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d4b8208e5a27e81dbc3217a8752adfa661538b91456534baf1c9ae724fdfbd" +dependencies = [ + "juniper", + "juniper_subscriptions", + "serde", + "tokio", +] + +[[package]] +name = "juniper_subscriptions" +version = "0.15.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a20ddcec070ee33abcaa88383dc61a23d13965e40d1cab84e3533fc3b34d03c0" +dependencies = [ + "futures", + "juniper", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -2133,6 +2350,7 @@ version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" dependencies = [ + "indexmap", "itoa", "ryu", "serde", @@ -2216,6 +2434,15 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +[[package]] +name = "smartstring" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ada87540bf8ef4cf8a1789deb175626829bb59b1fefd816cf7f7f55efcdbae9" +dependencies = [ + "static_assertions", +] + [[package]] name = "socket2" version = "0.3.19" @@ -2450,6 +2677,16 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi 0.3.9", +] + [[package]] name = "time" version = "0.2.26" @@ -2519,6 +2756,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" dependencies = [ "bytes 0.5.6", + "fnv", "futures-core", "iovec", "lazy_static", @@ -2529,9 +2767,21 @@ dependencies = [ "pin-project-lite 0.1.12", "signal-hook-registry", "slab", + "tokio-macros", "winapi 0.3.9", ] +[[package]] +name = "tokio-macros" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e44da00bfc73a25f814cd8d7e57a68a5c31b74b3152a0a1d1f590c97ed06265a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-util" version = "0.3.1" @@ -2540,6 +2790,7 @@ checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" dependencies = [ "bytes 0.5.6", "futures-core", + "futures-io", "futures-sink", "log", "pin-project-lite 0.1.12", @@ -2723,6 +2974,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +dependencies = [ + "void", +] + [[package]] name = "url" version = "2.2.1" @@ -2748,6 +3008,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/honk/Cargo.toml b/honk/Cargo.toml index c495de11..e448c8c9 100644 --- a/honk/Cargo.toml +++ b/honk/Cargo.toml @@ -21,6 +21,8 @@ color-eyre = "0.5.6" crossbeam-channel = "0.4.0" gazebo = "0.2.0" glob = "0.3.0" +juniper = "0.15" +juniper_actix = { version = "0.2", features = ["subscriptions"] } memofs = "0.1.2" once_cell = "1" parking_lot = "0.11.1" diff --git a/honk/src/gql.rs b/honk/src/gql.rs new file mode 100644 index 00000000..e1d177b7 --- /dev/null +++ b/honk/src/gql.rs @@ -0,0 +1,47 @@ +use crate::state::WorkspaceState; +use actix_web::{http::header, middleware, web, App, Error, HttpResponse, HttpServer}; +use juniper::{graphql_object, EmptyMutation, EmptySubscription, GraphQLObject, RootNode}; +use juniper_actix::{graphiql_handler, graphql_handler, playground_handler}; + +impl juniper::Context for WorkspaceState {} + +pub struct Query; + +#[graphql_object(context = WorkspaceState)] +impl Query { + fn apiVersion() -> String { + "1.0".to_string() + } + // #[graphql(arguments(id(description = "id of the user")))] + // fn user(database: &Database, id: i32) -> Option<&User> { + // database.get_user(&id) + // } +} + +pub type Schema = + RootNode<'static, Query, EmptyMutation, EmptySubscription>; + +pub fn schema() -> Schema { + Schema::new( + Query, + EmptyMutation::::new(), + EmptySubscription::::new(), + ) +} + +pub async fn graphiql_route() -> Result { + graphiql_handler("/graphgl", None).await +} + +pub async fn playground_route() -> Result { + playground_handler("/graphgl", None).await +} + +pub async fn graphql_route( + req: actix_web::HttpRequest, + // state: &WorkspaceState, + payload: actix_web::web::Payload, + schema: web::Data, +) -> Result { + graphql_handler(&schema, todo!("get the state"), req, payload).await +} diff --git a/honk/src/lib.rs b/honk/src/lib.rs index e5a92dac..e696c78a 100644 --- a/honk/src/lib.rs +++ b/honk/src/lib.rs @@ -1,16 +1,17 @@ -use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; +use crate::{error::Error, revision::EvaluatorExt, state::WorkspaceState}; +use actix_web::{get, middleware, web, App, HttpResponse, HttpServer, Responder}; +use juniper_actix::graphql_handler; use std::{path::Path, thread::JoinHandle}; use tracing::{debug, error, info, instrument}; pub mod builtins; pub mod error; +pub mod gql; pub mod graph; pub mod revision; pub mod state; pub mod vfs; -use crate::{error::Error, revision::EvaluatorExt, state::WorkspaceState}; - pub(crate) type Result = color_eyre::eyre::Result; pub struct Workspace { @@ -58,11 +59,26 @@ pub fn spawn_server(state: crate::WorkspaceState) -> JoinHandle