mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-26 00:00:23 +02:00
46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
//! Example async-graphql application.
|
|||
|
|
//!
|
||
|
|
//! Run with
|
||
|
|
//!
|
||
|
|
//! ```not_rust
|
||
|
|
//! cd examples && cargo run -p example-async-graphql
|
||
|
|
//! ```
|
||
|
|
|
||
|
|
mod starwars;
|
||
|
|
|
||
|
|
use async_graphql::{
|
||
|
|
http::{playground_source, GraphQLPlaygroundConfig},
|
||
|
|
EmptyMutation, EmptySubscription, Request, Response, Schema,
|
||
|
|
};
|
||
|
|
use axum::{
|
||
|
|
extract::State,
|
||
|
|
response::{Html, IntoResponse},
|
||
|
|
routing::get,
|
||
|
|
Json, Router,
|
||
|
|
};
|
||
|
|
use starwars::{QueryRoot, StarWars, StarWarsSchema};
|
||
|
|
|
||
|
|
async fn graphql_handler(schema: State<StarWarsSchema>, req: Json<Request>) -> Json<Response> {
|
||
|
|
schema.execute(req.0).await.into()
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn graphql_playground() -> impl IntoResponse {
|
||
|
|
Html(playground_source(GraphQLPlaygroundConfig::new("/")))
|
||
|
|
}
|
||
|
|
|
||
|
|
#[tokio::main]
|
||
|
|
async fn main() {
|
||
|
|
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
|
||
|
|
.data(StarWars::new())
|
||
|
|
.finish();
|
||
|
|
|
||
|
|
let app = Router::with_state(schema).route("/", get(graphql_playground).post(graphql_handler));
|
||
|
|
|
||
|
|
println!("Playground: http://localhost:3000");
|
||
|
|
|
||
|
|
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
|
||
|
|
.serve(app.into_make_service())
|
||
|
|
.await
|
||
|
|
.unwrap();
|
||
|
|
}
|