Skip to content

Commit

Permalink
feat: migrate playground
Browse files Browse the repository at this point in the history
  • Loading branch information
amitksingh1490 committed Apr 30, 2024
1 parent 23c9110 commit 4a54047
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 72 deletions.
1 change: 1 addition & 0 deletions src/cli/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn log_launch_and_open_browser(sc: &ServerConfig) {
);
if sc.graphiql() {
let url = sc.graphiql_url();
let url = format!("https://tailcall.run/playground/?u={}/graphql", url);

Check warning on line 19 in src/cli/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/server/mod.rs#L19

Added line #L19 was not covered by tests
tracing::info!("🌍 Playground: {}", url);

let _ = webbrowser::open(url.as_str());
Expand Down
2 changes: 1 addition & 1 deletion src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use data_loader::*;
pub use data_loader_request::*;
pub use method::Method;
pub use request_context::RequestContext;
pub use request_handler::{graphiql, handle_request, API_URL_PREFIX};
pub use request_handler::{handle_request, API_URL_PREFIX};
pub use request_template::RequestTemplate;
pub use response::*;

Expand Down
62 changes: 1 addition & 61 deletions src/http/request_handler.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::ops::Deref;
use std::sync::Arc;

use anyhow::Result;
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::ServerError;
use hyper::header::{self, CONTENT_TYPE};
use hyper::header::{self};
use hyper::http::Method;
use hyper::{Body, HeaderMap, Request, Response, StatusCode};
use opentelemetry::trace::SpanKind;
use opentelemetry_semantic_conventions::trace::{HTTP_REQUEST_METHOD, HTTP_ROUTE};
use prometheus::{Encoder, ProtobufEncoder, TextEncoder, PROTOBUF_FORMAT, TEXT_FORMAT};
use serde::de::DeserializeOwned;
use tracing::Instrument;
use tracing_opentelemetry::OpenTelemetrySpanExt;
Expand All @@ -20,51 +17,9 @@ use super::request_context::RequestContext;
use super::telemetry::{get_response_status_code, RequestCounter};
use super::{showcase, telemetry, AppContext};
use crate::async_graphql_hyper::{GraphQLRequestLike, GraphQLResponse};
use crate::blueprint::telemetry::TelemetryExporter;
use crate::config::{PrometheusExporter, PrometheusFormat};

pub const API_URL_PREFIX: &str = "/api";

pub fn graphiql(req: &Request<Body>) -> Result<Response<Body>> {
let query = req.uri().query();
let endpoint = "/graphql";
let endpoint = if let Some(query) = query {
if query.is_empty() {
Cow::Borrowed(endpoint)
} else {
Cow::Owned(format!("{}?{}", endpoint, query))
}
} else {
Cow::Borrowed(endpoint)
};

Ok(Response::new(Body::from(playground_source(
GraphQLPlaygroundConfig::new(&endpoint).title("Tailcall - GraphQL IDE"),
))))
}

fn prometheus_metrics(prometheus_exporter: &PrometheusExporter) -> Result<Response<Body>> {
let metric_families = prometheus::default_registry().gather();
let mut buffer = vec![];

match prometheus_exporter.format {
PrometheusFormat::Text => TextEncoder::new().encode(&metric_families, &mut buffer)?,
PrometheusFormat::Protobuf => {
ProtobufEncoder::new().encode(&metric_families, &mut buffer)?
}
};

let content_type = match prometheus_exporter.format {
PrometheusFormat::Text => TEXT_FORMAT,
PrometheusFormat::Protobuf => PROTOBUF_FORMAT,
};

Ok(Response::builder()
.status(200)
.header(CONTENT_TYPE, content_type)
.body(Body::from(buffer))?)
}

fn not_found() -> Result<Response<Body>> {
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
Expand Down Expand Up @@ -279,21 +234,6 @@ async fn handle_request_inner<T: DeserializeOwned + GraphQLRequestLike>(
graphql_request::<T>(req, &app_ctx, req_counter).await
}

hyper::Method::GET => {
if let Some(TelemetryExporter::Prometheus(prometheus)) =
app_ctx.blueprint.telemetry.export.as_ref()
{
if req.uri().path() == prometheus.path {
return prometheus_metrics(prometheus);
}
};

if app_ctx.blueprint.server.enable_graphiql {
return graphiql(&req);
}

not_found()
}
_ => not_found(),
}
}
Expand Down
12 changes: 2 additions & 10 deletions tailcall-cloudflare/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, RwLock};

use hyper::{Body, Method, Request, Response};
use hyper::{Body, Request, Response};
use lazy_static::lazy_static;
use tailcall::async_graphql_hyper::GraphQLRequest;
use tailcall::http::{graphiql, handle_request, showcase, AppContext};
use tailcall::http::{handle_request, showcase, AppContext};

use crate::http::{to_request, to_response};
use crate::runtime;
Expand All @@ -28,14 +28,6 @@ pub async fn fetch(
);
let req = to_request(req).await?;

// Quick exit to GraphiQL
//
// Has to be done here, since when using GraphiQL, a config query parameter is
// not specified, and get_app_ctx will fail without it.
if req.method() == Method::GET {
return to_response(graphiql(&req)?).await;
}

let env = Rc::new(env);
let app_ctx = match get_app_ctx(env, &req).await? {
Ok(app_ctx) => app_ctx,
Expand Down

0 comments on commit 4a54047

Please sign in to comment.