async-graphql / examples

217 stars 54 forks source link

Warp example issue #39

Closed servonlewis closed 3 years ago

servonlewis commented 3 years ago

Hey guys,

I've used the Warp example in the docs to create a graphql application. That said, I am trying to now serve a react spa directory, but is running into issues.

It seems that anything after the "graphql_post" route from the example, does not work at all, making it impossible to serve the static files on fall through cases.

How would i use this example but end it with or(other) without it giving this error:

{"data":null,"errors":[{"message":" --> 1:1\n  |\n1 | \n  | ^---\n  |\n  = expected executable_definition","locations":[{"line":1,"column":1}]}]}

    let graphql_post = warp::header::optional::<String>("auth")
        .and(async_graphql_warp::graphql(schema.clone()))
        .and_then(
            |token,
             (schema, mut request): (
                Schema<QueryRoot, MutationRoot, SubscriptionRoot>,
                async_graphql::Request,
            )| async move {
                if let Some(token) = token {
                    request = request.data(MyToken(token));
                }
                let resp = schema.execute(request).await;
                Ok::<_, Infallible>(Response::from(resp))
            },
        );

    let graphql_playground = warp::path("api").and(warp::get()).map(|| {
        HttpResponse::builder()
            .header("content-type", "text/html")
            .body(playground_source(
                GraphQLPlaygroundConfig::new("/api").subscription_endpoint("/api"),
            ))
    });

    let app_modeling_out_csv = warp::path("app_modeling.csv")
        .and(warp::get())
        .and(with_db(database_connections.0.clone()))
        .and_then(out_csv_app_modeling);

    let db_modeling_out_csv = warp::path("db_modeling.csv")
        .and(warp::get())
        .and(with_db(database_connections.0.clone()))
        .and_then(out_csv_db_modeling);

    let cors = warp::cors()
        .allow_any_origin()
        .allow_headers(vec![
            "auth",
            "accept",
            "sec-ch-ua",
            "sec-ch-ua-mobile",
            "User-Agent",
            "Sec-Fetch-Mode",
            "Referer",
            "Origin",
            "Access-Control-Request-Method",
            "Access-Control-Request-Headers",
            "content-type",
        ])
        .allow_methods(vec!["OPTIONS", "GET", "POST", "DELETE", "PUT"]);

    let react_app = warp::fs::dir("build");

    // let other = warp::path::end().and(warp::fs::dir("build"));
    let other = warp::fs::file("build/index.html");
    let warp_log = warp::log("info");
    let routes = react_app
        .or(graphql_subscription(schema))
        .or(app_modeling_out_csv)
        .or(db_modeling_out_csv)
        .or(graphql_playground)
        .or(graphql_post)
        .or(other) // THIS DOES NOT GET READ, if i put it above graphql post, it works, but graphql post does not.
        .recover(recover_api)
        .with(cors)
        .with(warp_log);

    warp::serve(routes)
        .run(([0, 0, 0, 0], 5000))
        .await;
servonlewis commented 3 years ago

oops, i didnt realize graphql post did not have a route on it. fixed it by adding warp::path("api").and(wrap it all)