微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

actix web - sqlx - juniper:我尝试使用同步解析器解析 Some("Query") 类型的异步字段用户'

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?