diff --git a/.changes/fixed/3184.md b/.changes/fixed/3184.md new file mode 100644 index 00000000000..bab3140b4df --- /dev/null +++ b/.changes/fixed/3184.md @@ -0,0 +1 @@ +Modify url parsing to allow passing args through to query url \ No newline at end of file diff --git a/crates/fuel-core/src/graphql_api/api_service.rs b/crates/fuel-core/src/graphql_api/api_service.rs index 01b5c754307..15957db1cbc 100644 --- a/crates/fuel-core/src/graphql_api/api_service.rs +++ b/crates/fuel-core/src/graphql_api/api_service.rs @@ -44,6 +44,7 @@ use axum::{ extract::{ DefaultBodyLimit, Extension, + RawQuery, }, http::{ HeaderValue, @@ -83,10 +84,7 @@ use std::{ TcpListener, }, pin::Pin, - sync::{ - Arc, - OnceLock, - }, + sync::Arc, }; use tokio_stream::StreamExt; use tower::limit::ConcurrencyLimitLayer; @@ -323,8 +321,9 @@ where let graphql_endpoint = "/v1/graphql"; let graphql_subscription_endpoint = "/v1/graphql-sub"; - let graphql_playground = - || render_graphql_playground(graphql_endpoint, graphql_subscription_endpoint); + let graphql_playground = |query: RawQuery| { + render_graphql_playground(graphql_endpoint, graphql_subscription_endpoint, query) + }; let router = Router::new() .route("/v1/playground", get(graphql_playground)) @@ -373,43 +372,54 @@ where )) } -/// Single initialization of the GraphQL playground HTML. -/// This is because the rendering and replacing is expensive -static GRAPHQL_PLAYGROUND_HTML: OnceLock> = OnceLock::new(); - fn _render_graphql_playground( endpoint: &str, subscription_endpoint: &str, + query: RawQuery, ) -> impl IntoResponse + Send + Sync { - let html = GRAPHQL_PLAYGROUND_HTML.get_or_init(|| { - let raw_html = GraphiQLSource::build() - .endpoint(endpoint) - .subscription_endpoint(subscription_endpoint) - .title("Fuel Graphql Playground") - .finish(); - - // this may not be necessary in the future, - // but we need it to patch: https://github.com/async-graphql/async-graphql/issues/1703 - let raw_html = raw_html.replace( - "https://unpkg.com/graphiql/graphiql.min.js", - "https://unpkg.com/graphiql@3/graphiql.min.js", - ); - let raw_html = raw_html.replace( - "https://unpkg.com/graphiql/graphiql.min.css", - "https://unpkg.com/graphiql@3/graphiql.min.css", - ); - - Arc::new(raw_html) - }); + let qs = query + .0 + .as_deref() + .filter(|q| !q.is_empty()) + .map(|q| format!("?{q}")) + .unwrap_or_default(); + + let endpoint = format!("{}{}", endpoint, qs); + let subscription_endpoint = format!("{}{}", subscription_endpoint, qs); + + // let html = GRAPHQL_PLAYGROUND_HTML.get_or_init(|| { + let raw_html = GraphiQLSource::build() + .endpoint(&endpoint) + .subscription_endpoint(&subscription_endpoint) + .title("Fuel Graphql Playground") + .finish(); - Html(html.as_str()) + // this may not be necessary in the future, + // but we need it to patch: https://github.com/async-graphql/async-graphql/issues/1703 + let raw_html = raw_html.replace( + "https://unpkg.com/graphiql/graphiql.min.js", + "https://unpkg.com/graphiql@3/graphiql.min.js", + ); + let raw_html = raw_html.replace( + "https://unpkg.com/graphiql/graphiql.min.css", + "https://unpkg.com/graphiql@3/graphiql.min.css", + ); + + let raw_html = raw_html.replace( + "const url = new URL(endpoint, window.location.origin);", + r#"const url = new URL(endpoint, window.location.origin); +url.search = window.location.search;"#, + ); + + Html(raw_html.clone()) } async fn render_graphql_playground( endpoint: &str, subscription_endpoint: &str, + raw_query: RawQuery, ) -> impl IntoResponse + Send + Sync { - _render_graphql_playground(endpoint, subscription_endpoint) + _render_graphql_playground(endpoint, subscription_endpoint, raw_query) } async fn health() -> Json {