Skip to content

Commit

Permalink
Add examples for Axum.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Aug 4, 2021
1 parent 45b58bf commit 7f59899
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ members = [

"rocket/starwars",
"rocket/upload",

"axum/starwars",
"axum/subscription",
]
13 changes: 13 additions & 0 deletions axum/starwars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "axum-starwars"
version = "0.1.0"
edition = "2018"

[dependencies]
async-graphql = { path = "../../.." }
async-graphql-axum = { path = "../../../integrations/axum" }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
starwars = { path = "../../models/starwars" }
hyper = "0.14"
#axum = "0.1.2"
axum = { git = "https://github.com/sunli829/axum.git", rev = "f43470a", features = ["ws", "headers"] }
34 changes: 34 additions & 0 deletions axum/starwars/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::response::IntoResponse;
use axum::{prelude::*, AddExtensionLayer};
use starwars::{QueryRoot, StarWars, StarWarsSchema};

async fn graphql_handler(
schema: extract::Extension<StarWarsSchema>,
req: GraphQLRequest,
) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}

async fn graphql_playground() -> impl IntoResponse {
response::Html(playground_source(GraphQLPlaygroundConfig::new("/")))
}

#[tokio::main]
async fn main() {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.data(StarWars::new())
.finish();

let app = route("/", get(graphql_playground).post(graphql_handler))
.layer(AddExtensionLayer::new(schema));

println!("Playground: http://localhost:8000");

hyper::Server::bind(&"0.0.0.0:8000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
13 changes: 13 additions & 0 deletions axum/subscription/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "subscription"
version = "0.1.0"
edition = "2018"

[dependencies]
async-graphql = { path = "../../.." }
async-graphql-axum = { path = "../../../integrations/axum" }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
books = { path = "../../models/books" }
hyper = "0.14"
# axum = { version = "0.1.2", features = ["ws", "headers"] }
axum = { git = "https://github.com/sunli829/axum.git", rev = "f43470a", features = ["ws", "headers"] }
51 changes: 51 additions & 0 deletions axum/subscription/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig, ALL_WEBSOCKET_PROTOCOLS};
use async_graphql::Schema;
use async_graphql_axum::{
graphql_subscription, GraphQLRequest, GraphQLResponse, SecWebsocketProtocol,
};
use axum::extract::TypedHeader;
use axum::response::IntoResponse;
use axum::ws::{ws, WebSocket};
use axum::{prelude::*, AddExtensionLayer};
use books::{BooksSchema, MutationRoot, QueryRoot, Storage, SubscriptionRoot};
use hyper::http::HeaderValue;

async fn graphql_handler(
schema: extract::Extension<BooksSchema>,
req: GraphQLRequest,
) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}

async fn graphql_subscription_handler(
socket: WebSocket,
schema: extract::Extension<BooksSchema>,
protocol: TypedHeader<SecWebsocketProtocol>,
) {
graphql_subscription(socket, schema.0.clone(), protocol.0).await
}

async fn graphql_playground() -> impl IntoResponse {
response::Html(playground_source(GraphQLPlaygroundConfig::new("/")))
}

#[tokio::main]
async fn main() {
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
.data(Storage::default())
.finish();

let app = route("/", get(graphql_playground).post(graphql_handler))
.route(
"/ws",
ws(graphql_subscription_handler).protocols(ALL_WEBSOCKET_PROTOCOLS),
)
.layer(AddExtensionLayer::new(schema));

println!("Playground: http://localhost:8000");

hyper::Server::bind(&"0.0.0.0:8000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}

0 comments on commit 7f59899

Please sign in to comment.