Compile error when upgrading the axum async_graphql example to shared Arc state #1575
Answered
by
davidpdrsn
LasseRosenow
asked this question in
Q&A
-
Hi everyone, I am writing a graphql api and I need to have a shared state object that I can continuously upgrade to react to configuration changes. This is why I decided to use the shared_state from the key_store example. But I get a compile error... I tried for many hours, but I cannot find out what is causing it. I get this error:
This is my code: use std::sync::{Arc, RwLock};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Request, Response, Schema, Object, Context,
};
use axum::{
extract::State,
response::{Html, IntoResponse},
routing::get,
Json, Router,
};
pub struct QueryRoot;
#[Object]
impl QueryRoot {
async fn example(&self, ctx: &Context<'_>, id: i32) -> Option<String> {
Some(String::new())
}
}
type SharedState = Arc<RwLock<AppState>>;
struct AppState {
schema: Schema<QueryRoot, EmptyMutation, EmptySubscription>,
}
async fn graphql_handler(state: State<SharedState>, req: Json<Request>) -> Json<Response> {
state.read().unwrap().schema.execute(req.0).await.into()
}
async fn graphql_playground() -> impl IntoResponse {
Html(playground_source(GraphQLPlaygroundConfig::new("/")))
}
#[tokio::main]
async fn main() {
let state = Arc::new(RwLock::new(AppState {
schema: Schema::new(QueryRoot, EmptyMutation, EmptySubscription)
}));
let app = Router::new()
.route("/", get(graphql_playground).post(graphql_handler))
.with_state(Arc::clone(&state));
println!("Playground: http://localhost:3000");
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
|
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Nov 26, 2022
Replies: 1 comment 5 replies
-
Have you tried https://docs.rs/axum/latest/axum/attr.debug_handler.html? |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
LasseRosenow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you tried https://docs.rs/axum/latest/axum/attr.debug_handler.html?