Using Actix Identity with Juniper GraphQL

Using Actix Identity with Juniper GraphQL


1

I use Actix Identity with Cookie Policy to store user session and about to migrate from REST to GraphQL using Juniper.

It would be great to create a login endpoint in GraphQL by using actix identity.

Using Juniper in Actix, I’ve to move GraphQL request into web::block

actix_web::web::block(|| move {
    let graphql_response = request.execute(&data, &context);

    Ok::<_, serde_json::error::Error>(serde_json::to_string(&graphql_response)?)
})

Then pass Identity as Juniper Context to Juniper to execute query login.

struct Context {
    identity: Identity,
    connection_pool: DatabaseConnectionPool
}

The only problem is, I’m unable to pass Actix Identity to web::block because of threads safety.

std::rc::Rc<actix_web::request::HttpRequestInner>` cannot be sent between threads safely

Having the problem of unable to pass the identity to thread safety, I think of another solution.

By forcing Juniper to return an additional field to read outside thread of web::block and execute, for instance:

struct Result {
  data: Juniper_output_or_something,
  action: special_action_field_which_return_on_every_query_to_outside_thread_safety
}

The workaround I could think of is somehow passing Actix Identity into web::block or forcing Juniper to return additional field to execute outside web::block.

Any Suggestion?

1 Answer
1


1

Identity encodes the data in a string, which you can pull out and pass around instead.

Change your Context to:

struct Context {
    identity: Option<String>,
    connection_pool: DatabaseConnectionPool,
}

And when you construct it, use identity.identity() to obtain the Option<String>.



Leave a Reply

Your email address will not be published. Required fields are marked *