Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async-graphql actix_web subscriptions don't work #1454

Open
ollyde opened this issue Dec 30, 2023 · 1 comment
Open

Async-graphql actix_web subscriptions don't work #1454

ollyde opened this issue Dec 30, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@ollyde
Copy link

ollyde commented Dec 30, 2023

"error": "Could not connect to websocket endpoint ws://localhost:8000/ws. Please check if the endpoint url is correct."

main.rs

use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Result};
use async_graphql_actix_web::GraphQLSubscription;
// use async_graphql::http::GraphiQLSource;
use async_graphql::{
    http::{playground_source, GraphQLPlaygroundConfig, GraphiQLSource},
    Schema,
};
use async_graphql_actix_web::GraphQL;

mod features;

use features::test::test::{schema_test, TestMutations, TestQueries, TestSubscriptions};

async fn index_ws(
    schema: web::Data<Schema<TestQueries, TestMutations, TestSubscriptions>>,
    req: HttpRequest,
    payload: web::Payload,
) -> actix_web::Result<HttpResponse> {
    GraphQLSubscription::new(Schema::clone(&*schema)).start(&req, payload)
}

#[allow(dead_code)]
async fn index_graphiql() -> Result<HttpResponse> {
    Ok(HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(
            GraphiQLSource::build()
                .endpoint("/")
                .subscription_endpoint("/ws")
                .finish(),
        ))
}

#[allow(dead_code)]
async fn index_playground() -> Result<HttpResponse> {
    Ok(HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(playground_source(
            GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
        )))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    println!("GraphiQL IDE: http://localhost:8000");

    HttpServer::new(move || {
        App::new()
            .service(
                web::resource("/")
                    .guard(guard::Post())
                    .to(GraphQL::new(schema_test())),
            )
            .service(
                web::resource("/ws")
                    .guard(guard::Get())
                    .guard(guard::Header("upgrade", "websocket"))
                    .to(index_ws),
            )
            .service(web::resource("/").guard(guard::Get()).to(index_playground))
    })
    .bind("127.0.0.1:8000")?
    .run()
    .await
}

test_subscription.rs

use std::time::Duration;

use async_graphql::*;
use futures_util::Stream;
use tokio_stream::StreamExt;

#[derive(Default)]
pub struct TestSubscriptionPing;

#[Subscription]
impl TestSubscriptionPing {
    async fn test_subscription_ping(
        &self,
        #[graphql(default = 1)] step: i32,
    ) -> impl Stream<Item = i32> {
        let mut value = 0;
        tokio_stream::wrappers::IntervalStream::new(tokio::time::interval(Duration::from_secs(1)))
            .map(move |_| {
                value += step;
                value
            })
    }
}

test.rs

// Combine schemas into a single schema
use async_graphql::{MergedObject, MergedSubscription, Schema};

use super::{
    test_hello_world::TestHelloWorld, test_pong::TestPong,
    test_subscrption_ping::TestSubscriptionPing,
};

// Query
#[derive(MergedObject, Default)]
pub struct TestQueries(TestHelloWorld);

// Mutation
#[derive(MergedObject, Default)]
pub struct TestMutations(TestPong);

// Subscriptions

#[derive(MergedSubscription, Default)]
pub struct TestSubscriptions(TestSubscriptionPing);

// Merge
pub fn schema_test() -> Schema<TestQueries, TestMutations, TestSubscriptions> {
    return Schema::new(
        TestQueries::default(),
        TestMutations::default(),
        TestSubscriptions::default(),
    );
}

@ollyde ollyde added the bug Something isn't working label Dec 30, 2023
@ollyde
Copy link
Author

ollyde commented Dec 30, 2023

Trying in both playgrounds, nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant