Skip to content

How do I access an Actix Web session store from a resolver?  #1128

Closed
@forty1thousand

Description

@forty1thousand

I have a project going where I need to store state inside of a session object and need to access it from inside of both a query and mutation resolver.

Here is a simple version of my current code.

graphql.rs

use actix_session::Session;
use juniper::graphql_object;

pub struct MyCtx {
    ses: Session,
}

#[graphql_object(context = MyCtx)]
impl Query {
    // ...
}
// ...

endpoints.rs

use crate::graphql::{Schema, MyCtx};
use actix_session::Session;
use actix_web::{HttpResponse, Error, HttpRequest, web::{Payload, Data}};

pub async fn graphql(
    req: HttpRequest,
    payload: Payload,
    schema: Data<Schema>,
    ses: Session
) -> Result<HttpResponse, Error> {
    let ctx = MyCtx { ses };
    graphql_handler(&schema, &ctx, req, payload).await
}
// ...

This code gives me a thread safety error when I run it.

`Rc<RefCell<actix_session::session::SessionInner>>` cannot be shared between threads safely
within `MyCtx`, the trait `Sync` is not implemented for `Rc<RefCell<actix_session::session::SessionInner>>`

I know the session store can also be accessed from a HttpRequest object but that also returns the same error.

Is there a workaround or an example of this being done?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions