如何解决actix web - sqlx - juniper:我尝试使用同步解析器解析 Some("Query") 类型的异步字段用户'
我正在尝试在我的 Rust 项目中使用 Actix-sqlx-Juniper。我遵循并结合了我发现的任何教程,它成功编译并运行。但是当我尝试发布查询时,我在终端中收到此错误:
thread 'actix-web' panicked at 'Tried to resolve async field users on type Some("Query") with a sync resolver',src/graphql.rs:15:1
我的 GraphiQL 显示 "Thread pool is gone"
这是src/graphql.rs
:
#[derive(Clone,Debug)]
pub struct Context {
pub pool: PgPool,}
impl juniper::Context for Context {}
pub struct Query;
#[graphql_object(Context = Context)] // this is line 15
impl Query {
fn apiVersion() -> &str {
"1.0"
}
#[graphql(description = "Hello")]
pub async fn users(ctx: &Context) -> FieldResult<Vec<User>> {
println!("{:#?}",ctx);
sqlx::query_as::<_,User>("SELECT * FROM users")
.fetch_all(&ctx.pool)
.await
.map_err(|e| e.into())
}
}
pub type Schema = RootNode<'static,Query,EmptyMutation<Context>,EmptySubscription<Context>>;
pub fn create_schema() -> Schema {
Schema::new(Query {},EmptyMutation::new(),EmptySubscription::new())
}
但是在我跟踪错误之后,当我尝试在我的 execute_sync
中使用 src/handler.rs
时它惊慌失措:
pub fn graphql_handlers(config: &mut ServiceConfig) {
config
.data(create_schema())
.route("/graphql",web::get().to(graphql_playground))
.route("/graphql",web::post().to(graphql));
}
...
...
async fn graphql(
pool: web::Data<PgPool>,schema: web::Data<Schema>,data: web::Json<GraphQLRequest>,) -> Result<HttpResponse,Error> {
let ctx = Context {
pool: pool.get_ref().to_owned(),};
let res = block(move || {
let res = data.execute_sync(&schema,&ctx);
Ok::<_,serde_json::error::Error>(serde_json::to_string(&res)?)
})
.await
.map_err(Error::from)?;
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(res))
}
这是我的main.rs
:
#[actix_web::main]
async fn main() -> Result<()> {
let pool = create_pool().await.expect("Failed connecting to postgres");
HttpServer::new(move || {
App::new()
.data(pool.clone())
.wrap(Logger::default())
.configure(graphql_handlers)
})
.bind("127.0.0.1:8000")?
.run()
.await
}
这是我的依赖项:
actix-web = "3"
juniper = "0.15"
serde = { version = "1.0",features = ["derive"] }
serde_json = "1.0.64"
uuid = { version = "0.8",features = [ "serde","v4"] }
sqlx = { version = "0.4",features = [ "runtime-actix-rustls","postgres","uuid" ] }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。