Skip to content

Commit

Permalink
chore: rename variables to app_ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath authored and mogery committed Feb 1, 2024
1 parent 2a6b33d commit f2137bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
20 changes: 10 additions & 10 deletions src/http/request_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ impl RequestContext {
}

impl From<&AppContext> for RequestContext {
fn from(server_ctx: &AppContext) -> Self {
fn from(app_ctx: &AppContext) -> Self {
Self {
h_client: server_ctx.runtime.http.clone(),
h2_client: server_ctx.runtime.http2_only.clone(),
server: server_ctx.blueprint.server.clone(),
upstream: server_ctx.blueprint.upstream.clone(),
h_client: app_ctx.runtime.http.clone(),
h2_client: app_ctx.runtime.http2_only.clone(),
server: app_ctx.blueprint.server.clone(),
upstream: app_ctx.blueprint.upstream.clone(),
req_headers: HeaderMap::new(),
http_data_loaders: server_ctx.http_data_loaders.clone(),
gql_data_loaders: server_ctx.gql_data_loaders.clone(),
cache: server_ctx.runtime.cache.clone(),
grpc_data_loaders: server_ctx.grpc_data_loaders.clone(),
http_data_loaders: app_ctx.http_data_loaders.clone(),
gql_data_loaders: app_ctx.gql_data_loaders.clone(),
cache: app_ctx.runtime.cache.clone(),
grpc_data_loaders: app_ctx.grpc_data_loaders.clone(),
min_max_age: Arc::new(Mutex::new(None)),
cache_public: Arc::new(Mutex::new(None)),
env_vars: server_ctx.runtime.env.clone(),
env_vars: app_ctx.runtime.env.clone(),
}
}
}
Expand Down
49 changes: 23 additions & 26 deletions src/http/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,46 @@ fn not_found() -> Result<Response<Body>> {
.body(Body::empty())?)
}

fn create_request_context(req: &Request<Body>, server_ctx: &AppContext) -> RequestContext {
let upstream = server_ctx.blueprint.upstream.clone();
fn create_request_context(req: &Request<Body>, app_ctx: &AppContext) -> RequestContext {
let upstream = app_ctx.blueprint.upstream.clone();
let allowed = upstream.get_allowed_headers();
let headers = create_allowed_headers(req.headers(), &allowed);
RequestContext::from(server_ctx).req_headers(headers)
RequestContext::from(app_ctx).req_headers(headers)
}

fn update_cache_control_header(
response: GraphQLResponse,
server_ctx: &AppContext,
app_ctx: &AppContext,
req_ctx: Arc<RequestContext>,
) -> GraphQLResponse {
if server_ctx.blueprint.server.enable_cache_control_header {
if app_ctx.blueprint.server.enable_cache_control_header {
let ttl = req_ctx.get_min_max_age().unwrap_or(0);
let cache_public_flag = req_ctx.is_cache_public().unwrap_or(true);
return response.set_cache_control(ttl, cache_public_flag);
}
response
}

pub fn update_response_headers(resp: &mut hyper::Response<hyper::Body>, server_ctx: &AppContext) {
if !server_ctx.blueprint.server.response_headers.is_empty() {
pub fn update_response_headers(resp: &mut hyper::Response<hyper::Body>, app_ctx: &AppContext) {
if !app_ctx.blueprint.server.response_headers.is_empty() {
resp.headers_mut()
.extend(server_ctx.blueprint.server.response_headers.clone());
.extend(app_ctx.blueprint.server.response_headers.clone());
}
}

pub async fn graphql_request<T: DeserializeOwned + GraphQLRequestLike>(
req: Request<Body>,
server_ctx: &AppContext,
app_ctx: &AppContext,
) -> Result<Response<Body>> {
let req_ctx = Arc::new(create_request_context(&req, server_ctx));
let req_ctx = Arc::new(create_request_context(&req, app_ctx));
let bytes = hyper::body::to_bytes(req.into_body()).await?;
let request = serde_json::from_slice::<T>(&bytes);
match request {
Ok(request) => {
let mut response = request
.data(req_ctx.clone())
.execute(&server_ctx.schema)
.await;
response = update_cache_control_header(response, server_ctx, req_ctx);
let mut response = request.data(req_ctx.clone()).execute(&app_ctx.schema).await;
response = update_cache_control_header(response, app_ctx, req_ctx);
let mut resp = response.to_response()?;
update_response_headers(&mut resp, server_ctx);
update_response_headers(&mut resp, app_ctx);
Ok(resp)
}
Err(err) => {
Expand Down Expand Up @@ -111,37 +108,37 @@ fn create_allowed_headers(headers: &HeaderMap, allowed: &BTreeSet<String>) -> He

pub async fn handle_request<T: DeserializeOwned + GraphQLRequestLike>(
req: Request<Body>,
state: Arc<AppContext>,
app_ctx: Arc<AppContext>,
) -> Result<Response<Body>> {
match *req.method() {
// NOTE:
// The first check for the route should be for `/graphql`
// This is always going to be the most used route.
hyper::Method::POST if req.uri().path() == "/graphql" => {
graphql_request::<T>(req, state.as_ref()).await
graphql_request::<T>(req, app_ctx.as_ref()).await
}
hyper::Method::POST
if state.blueprint.server.enable_showcase
if app_ctx.blueprint.server.enable_showcase
&& req.uri().path().ends_with("/showcase/graphql") =>
{
let resources = ShowcaseResources {
http: state.universal_http_client.clone(),
env: None, // Disallow access to environment varialbes
http: app_ctx.universal_http_client.clone(),
env: None, // Disallow access to environment variables
file: None, // Disallow local file reading

// TODO: Generically accessible way to create new clean cache
cache: state.cache.clone(),
cache: app_ctx.cache.clone(),
};

let server_ctx = match showcase_get_app_ctx::<T>(&req, resources).await? {
Ok(server_ctx) => server_ctx,
let app_ctx = match showcase_get_app_ctx::<T>(&req, resources).await? {
Ok(app_ctx) => app_ctx,
Err(res) => return Ok(res),
};

graphql_request::<T>(req, &server_ctx).await
graphql_request::<T>(req, &app_ctx).await
}

hyper::Method::GET if state.blueprint.server.enable_graphiql => graphiql(&req),
hyper::Method::GET if app_ctx.blueprint.server.enable_graphiql => graphiql(&req),
_ => not_found(),
}
}
2 changes: 1 addition & 1 deletion tests/graphql_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ async fn test_execution() -> std::io::Result<()> {
HeaderName::from_static("authorization"),
HeaderValue::from_static("1"),
);
let req_ctx = Arc::new(RequestContext::from(&server_ctx).req_headers(headers));
let req_ctx = Arc::new(RequestContext::from(&app_ctx).req_headers(headers));
let req = Request::from(q.query.as_str()).data(req_ctx.clone());
let res = schema.execute(req).await;
let json = serde_json::to_string(&res).unwrap();
Expand Down

0 comments on commit f2137bb

Please sign in to comment.