Add graphql
This commit is contained in:
@@ -6,7 +6,11 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.4" }
|
||||
axum = { version = "0.5.6" }
|
||||
futures = "0.3.21"
|
||||
tower-http = {version = "0.3.3", features = ["cors"]}
|
||||
async-graphql = { version = "4.0.0" }
|
||||
async-graphql-axum = { version = "4.0.0" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.68"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
|
@@ -1,6 +1,34 @@
|
||||
mod mutation;
|
||||
mod query;
|
||||
mod schema;
|
||||
mod subscription;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{routing, Router};
|
||||
use async_graphql::{
|
||||
http::{playground_source, GraphQLPlaygroundConfig},
|
||||
Request, Response, Schema,
|
||||
};
|
||||
use async_graphql_axum::GraphQLSubscription;
|
||||
use axum::{
|
||||
http::Method,
|
||||
response::{Html, IntoResponse},
|
||||
routing, Extension, Json, Router,
|
||||
};
|
||||
use mutation::MutationRoot;
|
||||
use query::QueryRoot;
|
||||
use schema::ScelSchema;
|
||||
use subscription::SubscriptionRoot;
|
||||
use tower_http::cors::CorsLayer;
|
||||
|
||||
async fn graphql_playground() -> impl IntoResponse {
|
||||
Html(playground_source(
|
||||
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
|
||||
))
|
||||
}
|
||||
async fn graphql_handler(schema: Extension<ScelSchema>, req: Json<Request>) -> Json<Response> {
|
||||
schema.execute(req.0).await.into()
|
||||
}
|
||||
|
||||
pub struct Server {
|
||||
app: Router,
|
||||
@@ -9,7 +37,22 @@ pub struct Server {
|
||||
|
||||
impl Server {
|
||||
pub fn new() -> Server {
|
||||
let app = Router::new().route("/", routing::get(root));
|
||||
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot).finish();
|
||||
|
||||
let cors = vec!["http://localhost:3000"
|
||||
.parse()
|
||||
.expect("Could not parse url")];
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", routing::get(graphql_playground).post(graphql_handler))
|
||||
.route("/ws", GraphQLSubscription::new(schema.clone()))
|
||||
.layer(Extension(schema))
|
||||
.layer(
|
||||
CorsLayer::new()
|
||||
.allow_origin(cors)
|
||||
.allow_headers([axum::http::header::CONTENT_TYPE])
|
||||
.allow_methods([Method::GET, Method::POST, Method::OPTIONS]),
|
||||
);
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
|
||||
return Server { app, addr };
|
||||
@@ -27,8 +70,3 @@ impl Server {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn root() -> &'static str {
|
||||
tracing::info!("some scel");
|
||||
return "scel";
|
||||
}
|
||||
|
15
src/lib/scel_api/src/mutation.rs
Normal file
15
src/lib/scel_api/src/mutation.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use async_graphql::{Context, Object, Result, SimpleObject, ID};
|
||||
|
||||
pub struct MutationRoot;
|
||||
|
||||
#[derive(SimpleObject)]
|
||||
struct RequestDownloadResponse {
|
||||
id: ID,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl MutationRoot {
|
||||
async fn request_download(&self, ctx: &Context<'_>) -> Result<RequestDownloadResponse> {
|
||||
Err("not implemented 123".into())
|
||||
}
|
||||
}
|
10
src/lib/scel_api/src/query.rs
Normal file
10
src/lib/scel_api/src/query.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use async_graphql::Object;
|
||||
|
||||
pub struct QueryRoot;
|
||||
|
||||
#[Object]
|
||||
impl QueryRoot {
|
||||
async fn hello_world(&self) -> &str {
|
||||
"Hello, world!"
|
||||
}
|
||||
}
|
5
src/lib/scel_api/src/schema.rs
Normal file
5
src/lib/scel_api/src/schema.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
use async_graphql::Schema;
|
||||
|
||||
use crate::{mutation::MutationRoot, query::QueryRoot, subscription::SubscriptionRoot};
|
||||
|
||||
pub type ScelSchema = Schema<QueryRoot, MutationRoot, SubscriptionRoot>;
|
25
src/lib/scel_api/src/subscription.rs
Normal file
25
src/lib/scel_api/src/subscription.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use async_graphql::{
|
||||
async_stream::stream, futures_util::Stream, Context, Object, Subscription, ID,
|
||||
};
|
||||
|
||||
pub struct SubscriptionRoot;
|
||||
|
||||
struct DownloadChanged {
|
||||
id: ID,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl DownloadChanged {
|
||||
async fn id(&self) -> &ID {
|
||||
&self.id
|
||||
}
|
||||
}
|
||||
|
||||
#[Subscription]
|
||||
impl SubscriptionRoot {
|
||||
async fn get_download(&self, ctx: &Context<'_>) -> impl Stream<Item = DownloadChanged> {
|
||||
stream! {
|
||||
yield DownloadChanged {id: "Some-id".into()}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user