-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,7 @@ members = [ | |
|
||
"rocket/starwars", | ||
"rocket/upload", | ||
|
||
"axum/starwars", | ||
"axum/subscription", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |