Skip to content

Commit

Permalink
Refactor for global static values update #567 (#570)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
artech-git and tusharmath authored Nov 6, 2023
1 parent 4a0613e commit 3168cff
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async-graphql = { version = "6.0", features = [
async-graphql-value = "6.0"
base64 = "0.21"
indexmap = "2.1"
lazy_static = "1.4.0"
once_cell = "1.18.0"
mini-v8 = { version = "0.4.1", optional = true }
clap = { version = "4.4.7", features = ["derive"] }
colored = "2"
Expand Down
56 changes: 27 additions & 29 deletions benches/impl_path_string_for_evaluation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use hyper::header::HeaderValue;
use hyper::HeaderMap;
use indexmap::IndexMap;
use once_cell::sync::Lazy;
use tailcall::http::RequestContext;
use tailcall::lambda::{EvaluationContext, ResolverContextLike};
use tailcall::path_string::PathString;
Expand All @@ -32,47 +33,45 @@ const HEADERS_VALUE: &[&[&str]] = &[&["headers", "existing"], &["headers", "miss

const VARS_VALUE: &[&[&str]] = &[&["vars", "existing"], &["vars", "missing"]];

lazy_static::lazy_static! {
static ref TEST_VALUES: Value = {
let mut root = IndexMap::new();
let mut nested = IndexMap::new();
static TEST_VALUES: Lazy<Value> = Lazy::new(|| {
let mut root = IndexMap::new();
let mut nested = IndexMap::new();

nested.insert(Name::new("existing"), Value::String("nested-test".to_owned()));
nested.insert(Name::new("existing"), Value::String("nested-test".to_owned()));

root.insert(Name::new("root"), Value::String("root-test".to_owned()));
root.insert(Name::new("nested"), Value::Object(nested));
root.insert(Name::new("root"), Value::String("root-test".to_owned()));
root.insert(Name::new("nested"), Value::Object(nested));

Value::Object(root)
};
Value::Object(root)
});

static ref TEST_ARGS: IndexMap<Name, Value> = {
let mut root = IndexMap::new();
let mut nested = IndexMap::new();
static TEST_ARGS: Lazy<IndexMap<Name, Value>> = Lazy::new(|| {
let mut root = IndexMap::new();
let mut nested = IndexMap::new();

nested.insert(Name::new("existing"), Value::String("nested-test".to_owned()));
nested.insert(Name::new("existing"), Value::String("nested-test".to_owned()));

root.insert(Name::new("root"), Value::String("root-test".to_owned()));
root.insert(Name::new("nested"), Value::Object(nested));
root.insert(Name::new("root"), Value::String("root-test".to_owned()));
root.insert(Name::new("nested"), Value::Object(nested));

root
};
root
});

static ref TEST_HEADERS: HeaderMap = {
let mut map = HeaderMap::new();
static TEST_HEADERS: Lazy<HeaderMap> = Lazy::new(|| {
let mut map = HeaderMap::new();

map.insert("x-existing", HeaderValue::from_static("header"));
map.insert("x-existing", HeaderValue::from_static("header"));

map
};
map
});

static ref TEST_VARS: BTreeMap<String, String> = {
let mut map = BTreeMap::new();
static TEST_VARS: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
let mut map = BTreeMap::new();

map.insert("existing".to_owned(), "var".to_owned());
map.insert("existing".to_owned(), "var".to_owned());

map
};
}
map
});

fn to_bench_id(input: &[&str]) -> BenchmarkId {
BenchmarkId::new("input", input.join("."))
Expand Down Expand Up @@ -132,7 +131,6 @@ fn bench_main(c: &mut Criterion) {
let mut req_ctx = RequestContext::default().req_headers(TEST_HEADERS.clone());

req_ctx.server.vars = TEST_VARS.clone();

let eval_ctx = EvaluationContext::new(&req_ctx, &MockGraphqlContext);

assert_test(&eval_ctx);
Expand Down
5 changes: 2 additions & 3 deletions src/async_graphql_hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use async_graphql::{BatchResponse, Executor};
use hyper::header::{HeaderValue, CACHE_CONTROL, CONTENT_TYPE};
use hyper::{Body, Response, StatusCode};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

#[derive(Debug)]
Expand Down Expand Up @@ -85,9 +86,7 @@ impl GraphQLQuery {
}
}

lazy_static::lazy_static! {
static ref APPLICATION_JSON:HeaderValue = HeaderValue::from_static("application/json");
}
static APPLICATION_JSON: Lazy<HeaderValue> = Lazy::new(|| HeaderValue::from_static("application/json"));

impl GraphQLResponse {
pub fn to_response(self) -> Result<Response<hyper::Body>> {
Expand Down
5 changes: 2 additions & 3 deletions src/lambda/evaluation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::Duration;

use async_graphql::{Name, Value};
use derive_setters::Setters;
use once_cell::sync::Lazy;
use reqwest::header::HeaderMap;

use super::{EmptyResolverContext, ResolverContextLike};
Expand All @@ -18,9 +19,7 @@ pub struct EvaluationContext<'a, Ctx: ResolverContextLike<'a>> {
pub timeout: Duration,
}

lazy_static::lazy_static! {
static ref REQUEST_CTX: RequestContext = RequestContext::default();
}
static REQUEST_CTX: Lazy<RequestContext> = Lazy::new(RequestContext::default);

impl Default for EvaluationContext<'static, EmptyResolverContext> {
fn default() -> Self {
Expand Down

0 comments on commit 3168cff

Please sign in to comment.