Skip to content

Trying to use Session with Graphql using Poem #1149

Answered by sunli829
Alfiec7113 asked this question in Q&A
Discussion options

You must be logged in to vote

You can do it like this:

#[handler]
fn graphql_subscription(
    session: &Session,
    schema: Data<&MySchema>, // extract the graphql schema
    protocol: GraphQLProtocol,
    websocket: WebSocket,
) -> impl IntoResponse {
    let schema = schema.0.clone();
    let mut data = async_graphql::Data::default();

    // extract the username from session and then insert it to data
    let username = session.get::<String>("username");
    if let Some(username) = username {
        data.insert(username);
    }

    websocket
        .protocols(ALL_WEBSOCKET_PROTOCOLS)
        .on_upgrade(move |stream| {
            GraphQLWebSocket::new(stream, schema, protocol)
                .with_data(data) /…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@Alfiec7113
Comment options

@Alfiec7113
Comment options

Answer selected by Alfiec7113
Comment options

You must be logged in to vote
1 reply
@Alfiec7113
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants