From c78391398e3a49bdf61596b239e23afd6d1d48d9 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Thu, 11 Apr 2024 17:53:56 +0530 Subject: [PATCH 1/3] fix: Specifying @link in config skips upstream.allowedHeaders (#1699) Co-authored-by: Tushar Mathur --- src/config/server.rs | 68 +++++++++++++++++++ src/config/upstream.rs | 68 ++++++++++++++++--- src/lib.rs | 1 + src/merge_right.rs | 12 ++-- src/primitive.rs | 37 ++++++++++ .../test-merge-right-with-link-config.md | 17 +++++ ...erge-right-with-link-config.md_client.snap | 27 ++++++++ ...erge-right-with-link-config.md_merged.snap | 11 +++ 8 files changed, 229 insertions(+), 12 deletions(-) create mode 100644 src/primitive.rs create mode 100644 tests/execution/test-merge-right-with-link-config.md create mode 100644 tests/snapshots/execution_spec__test-merge-right-with-link-config.md_client.snap create mode 100644 tests/snapshots/execution_spec__test-merge-right-with-link-config.md_merged.snap diff --git a/src/config/server.rs b/src/config/server.rs index f691bd8612..797e169425 100644 --- a/src/config/server.rs +++ b/src/config/server.rs @@ -103,6 +103,12 @@ pub struct ScriptOptions { pub timeout: Option, } +impl MergeRight for ScriptOptions { + fn merge_right(self, other: Self) -> Self { + ScriptOptions { timeout: self.timeout.merge_right(other.timeout) } + } +} + #[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, Default, schemars::JsonSchema)] pub enum HttpVersion { #[default] @@ -110,6 +116,12 @@ pub enum HttpVersion { HTTP2, } +impl MergeRight for HttpVersion { + fn merge_right(self, other: Self) -> Self { + other + } +} + impl Server { pub fn enable_apollo_tracing(&self) -> bool { self.apollo_tracing.unwrap_or(false) @@ -195,6 +207,7 @@ impl Server { self.pipeline_flush.unwrap_or(true) } } + impl MergeRight for Server { fn merge_right(mut self, other: Self) -> Self { self.apollo_tracing = self.apollo_tracing.merge_right(other.apollo_tracing); @@ -229,3 +242,58 @@ impl MergeRight for Server { self } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::config::ScriptOptions; + + fn server_with_script_options(so: ScriptOptions) -> Server { + Server { script: Some(so), ..Default::default() } + } + + #[test] + fn script_options_merge_both() { + let a = server_with_script_options(ScriptOptions { timeout: Some(100) }); + let b = server_with_script_options(ScriptOptions { timeout: Some(200) }); + let merged = a.merge_right(b); + let expected = ScriptOptions { timeout: Some(200) }; + assert_eq!(merged.script, Some(expected)); + } + + #[test] + fn script_options_merge_first() { + let a = server_with_script_options(ScriptOptions { timeout: Some(100) }); + let b = server_with_script_options(ScriptOptions { timeout: None }); + let merged = a.merge_right(b); + let expected = ScriptOptions { timeout: Some(100) }; + assert_eq!(merged.script, Some(expected)); + } + + #[test] + fn script_options_merge_second() { + let a = server_with_script_options(ScriptOptions { timeout: None }); + let b = server_with_script_options(ScriptOptions { timeout: Some(100) }); + let merged = a.merge_right(b); + let expected = ScriptOptions { timeout: Some(100) }; + assert_eq!(merged.script, Some(expected)); + } + + #[test] + fn script_options_merge_second_default() { + let a = server_with_script_options(ScriptOptions { timeout: Some(100) }); + let b = Server::default(); + let merged = a.merge_right(b); + let expected = ScriptOptions { timeout: Some(100) }; + assert_eq!(merged.script, Some(expected)); + } + + #[test] + fn script_options_merge_first_default() { + let a = Server::default(); + let b = server_with_script_options(ScriptOptions { timeout: Some(100) }); + let merged = a.merge_right(b); + let expected = ScriptOptions { timeout: Some(100) }; + assert_eq!(merged.script, Some(expected)); + } +} diff --git a/src/config/upstream.rs b/src/config/upstream.rs index 7a3ba9c6ad..47e8d9b367 100644 --- a/src/config/upstream.rs +++ b/src/config/upstream.rs @@ -13,6 +13,7 @@ pub struct Batch { pub headers: BTreeSet, pub max_size: usize, } + impl Default for Batch { fn default() -> Self { Batch { max_size: 100, delay: 0, headers: BTreeSet::new() } @@ -24,6 +25,12 @@ pub struct Proxy { pub url: String, } +impl MergeRight for Proxy { + fn merge_right(self, other: Self) -> Self { + other + } +} + #[derive( Serialize, Deserialize, PartialEq, Eq, Clone, Debug, Setters, Default, schemars::JsonSchema, )] @@ -170,14 +177,7 @@ impl Upstream { impl MergeRight for Upstream { // TODO: add unit tests for merge fn merge_right(mut self, other: Self) -> Self { - self.allowed_headers = other.allowed_headers.map(|other| { - if let Some(mut self_headers) = self.allowed_headers { - self_headers = self_headers.merge_right(other); - self_headers - } else { - other - } - }); + self.allowed_headers = self.allowed_headers.merge_right(other.allowed_headers); self.base_url = self.base_url.merge_right(other.base_url); self.connect_timeout = self.connect_timeout.merge_right(other.connect_timeout); self.http_cache = self.http_cache.merge_right(other.http_cache); @@ -212,3 +212,55 @@ impl MergeRight for Upstream { self } } + +#[cfg(test)] +mod tests { + use super::*; + + fn setup_upstream_with_headers(headers: &[&str]) -> Upstream { + Upstream { + allowed_headers: Some(headers.iter().map(|s| s.to_string()).collect()), + ..Default::default() + } + } + + #[test] + fn allowed_headers_merge_both() { + let a = setup_upstream_with_headers(&["a", "b", "c"]); + let b = setup_upstream_with_headers(&["d", "e", "f"]); + let merged = a.merge_right(b); + assert_eq!( + merged.allowed_headers, + Some( + ["a", "b", "c", "d", "e", "f"] + .iter() + .map(|s| s.to_string()) + .collect() + ) + ); + } + + #[test] + fn allowed_headers_merge_first() { + let a = setup_upstream_with_headers(&["a", "b", "c"]); + let b = Upstream::default(); + let merged = a.merge_right(b); + + assert_eq!( + merged.allowed_headers, + Some(["a", "b", "c"].iter().map(|s| s.to_string()).collect()) + ); + } + + #[test] + fn allowed_headers_merge_second() { + let a = Upstream::default(); + let b = setup_upstream_with_headers(&["a", "b", "c"]); + let merged = a.merge_right(b); + + assert_eq!( + merged.allowed_headers, + Some(["a", "b", "c"].iter().map(|s| s.to_string()).collect()) + ); + } +} diff --git a/src/lib.rs b/src/lib.rs index 8f14ce302d..5242bd7ade 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ pub mod lambda; pub mod merge_right; pub mod mustache; pub mod path; +pub mod primitive; pub mod print_schema; mod proto_reader; mod resource_reader; diff --git a/src/merge_right.rs b/src/merge_right.rs index 31920365cd..e9e7f9c5ca 100644 --- a/src/merge_right.rs +++ b/src/merge_right.rs @@ -4,10 +4,14 @@ pub trait MergeRight { fn merge_right(self, other: Self) -> Self; } -impl MergeRight for Option { - fn merge_right(mut self, other: Self) -> Self { - self = other.or(self); - self +impl MergeRight for Option { + fn merge_right(self, other: Self) -> Self { + match (self, other) { + (Some(this), Some(that)) => Some(this.merge_right(that)), + (None, Some(that)) => Some(that), + (Some(this), None) => Some(this), + (None, None) => None, + } } } diff --git a/src/primitive.rs b/src/primitive.rs new file mode 100644 index 0000000000..2c205009f6 --- /dev/null +++ b/src/primitive.rs @@ -0,0 +1,37 @@ +use crate::merge_right::MergeRight; + +pub trait Primitive {} + +impl Primitive for u64 {} + +impl Primitive for u32 {} + +impl Primitive for u16 {} + +impl Primitive for u8 {} + +impl Primitive for usize {} + +impl Primitive for i64 {} + +impl Primitive for i32 {} + +impl Primitive for i16 {} + +impl Primitive for i8 {} + +impl Primitive for f64 {} + +impl Primitive for f32 {} + +impl Primitive for bool {} + +impl Primitive for char {} + +impl Primitive for String {} + +impl MergeRight for A { + fn merge_right(self, other: Self) -> Self { + other + } +} diff --git a/tests/execution/test-merge-right-with-link-config.md b/tests/execution/test-merge-right-with-link-config.md new file mode 100644 index 0000000000..4d3b3eea8f --- /dev/null +++ b/tests/execution/test-merge-right-with-link-config.md @@ -0,0 +1,17 @@ +# test-merge-right-with-link-config + +```graphql @file:stripe-types.graphql +type Foo { + bar: String +} +``` + +```graphql @server +schema @upstream(allowedHeaders: ["Authorization"]) @link(src: "stripe-types.graphql", type: Config) { + query: Query +} + +type Query { + foo: Foo @expr(body: {bar: "foo"}) +} +``` diff --git a/tests/snapshots/execution_spec__test-merge-right-with-link-config.md_client.snap b/tests/snapshots/execution_spec__test-merge-right-with-link-config.md_client.snap new file mode 100644 index 0000000000..ca6c4ff3a0 --- /dev/null +++ b/tests/snapshots/execution_spec__test-merge-right-with-link-config.md_client.snap @@ -0,0 +1,27 @@ +--- +source: tests/execution_spec.rs +expression: client +--- +scalar Date + +scalar Email + +scalar Empty + +type Foo { + bar: String +} + +scalar JSON + +scalar PhoneNumber + +type Query { + foo: Foo +} + +scalar Url + +schema { + query: Query +} diff --git a/tests/snapshots/execution_spec__test-merge-right-with-link-config.md_merged.snap b/tests/snapshots/execution_spec__test-merge-right-with-link-config.md_merged.snap new file mode 100644 index 0000000000..095502b241 --- /dev/null +++ b/tests/snapshots/execution_spec__test-merge-right-with-link-config.md_merged.snap @@ -0,0 +1,11 @@ +--- +source: tests/execution_spec.rs +expression: merged +--- +schema @server @upstream(allowedHeaders: ["Authorization"]) @link(src: "stripe-types.graphql", type: Config) { + query: Query +} + +type Query { + foo: Foo @expr(body: {bar: "foo"}) +} From eb8975b544cc7fd0fdf2c33f98e1a94f8527e5c0 Mon Sep 17 00:00:00 2001 From: Kunam Balaram Reddy Date: Thu, 11 Apr 2024 19:36:17 +0530 Subject: [PATCH 2/3] refactor: Rename cloudflare package (#1703) --- .github/workflows/ci.yml | 4 +- Cargo.lock | 44 +++++++++---------- Cargo.toml | 4 +- .../Cargo.toml | 2 +- .../package-lock.json | 0 .../package.json | 0 .../src/cache.rs | 0 .../src/env.rs | 0 .../src/file.rs | 0 .../src/handle.rs | 0 .../src/http.rs | 0 .../src/lib.rs | 0 .../src/runtime.rs | 0 .../tests/cf_tests.spec.ts | 0 .../tests/mf.ts | 0 .../wrangler.toml | 0 16 files changed, 27 insertions(+), 27 deletions(-) rename {cloudflare => tailcall-cloudflare}/Cargo.toml (95%) rename {cloudflare => tailcall-cloudflare}/package-lock.json (100%) rename {cloudflare => tailcall-cloudflare}/package.json (100%) rename {cloudflare => tailcall-cloudflare}/src/cache.rs (100%) rename {cloudflare => tailcall-cloudflare}/src/env.rs (100%) rename {cloudflare => tailcall-cloudflare}/src/file.rs (100%) rename {cloudflare => tailcall-cloudflare}/src/handle.rs (100%) rename {cloudflare => tailcall-cloudflare}/src/http.rs (100%) rename {cloudflare => tailcall-cloudflare}/src/lib.rs (100%) rename {cloudflare => tailcall-cloudflare}/src/runtime.rs (100%) rename {cloudflare => tailcall-cloudflare}/tests/cf_tests.spec.ts (100%) rename {cloudflare => tailcall-cloudflare}/tests/mf.ts (100%) rename {cloudflare => tailcall-cloudflare}/wrangler.toml (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a88d21c01..d1a9d37fbc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,7 +63,7 @@ jobs: runs-on: ubuntu-latest defaults: run: - working-directory: ./cloudflare + working-directory: ./tailcall-cloudflare steps: - uses: actions/checkout@v4 @@ -75,7 +75,7 @@ jobs: - name: Build WASM run: cargo check --lib --target wasm32-unknown-unknown - working-directory: ./cloudflare + working-directory: ./tailcall-cloudflare - name: Install Node.js uses: actions/setup-node@v4 with: diff --git a/Cargo.lock b/Cargo.lock index 3f780a48d3..ec8f3c3ab8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -994,28 +994,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" -[[package]] -name = "cloudflare" -version = "0.1.0" -dependencies = [ - "anyhow", - "async-graphql-value", - "async-std", - "async-trait", - "console_error_panic_hook", - "hyper 0.14.28", - "lazy_static", - "protox", - "reqwest", - "serde_json", - "serde_qs", - "tailcall", - "tracing", - "tracing-subscriber", - "tracing-subscriber-wasm", - "worker", -] - [[package]] name = "colorchoice" version = "1.0.0" @@ -5344,6 +5322,28 @@ dependencies = [ "which 6.0.1", ] +[[package]] +name = "tailcall-cloudflare" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-graphql-value", + "async-std", + "async-trait", + "console_error_panic_hook", + "hyper 0.14.28", + "lazy_static", + "protox", + "reqwest", + "serde_json", + "serde_qs", + "tailcall", + "tracing", + "tracing-subscriber", + "tracing-subscriber-wasm", + "worker", +] + [[package]] name = "tap" version = "1.0.1" diff --git a/Cargo.toml b/Cargo.toml index 25e405c6a4..f330c5d1e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -178,7 +178,7 @@ default = ["cli", "js"] [workspace] -members = [".", "autogen", "aws-lambda", "cloudflare"] +members = [".", "autogen", "aws-lambda", "tailcall-cloudflare"] # Boost execution_spec snapshot diffing performance [profile.dev.package] @@ -194,7 +194,7 @@ debug = false incremental = false overflow-checks = false -[profile.release.package.cloudflare] +[profile.release.package.tailcall-cloudflare] strip = true codegen-units = 1 opt-level = 'z' diff --git a/cloudflare/Cargo.toml b/tailcall-cloudflare/Cargo.toml similarity index 95% rename from cloudflare/Cargo.toml rename to tailcall-cloudflare/Cargo.toml index 3f8dcebd57..9316fdf148 100644 --- a/cloudflare/Cargo.toml +++ b/tailcall-cloudflare/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "cloudflare" +name = "tailcall-cloudflare" version = "0.1.0" edition = "2021" diff --git a/cloudflare/package-lock.json b/tailcall-cloudflare/package-lock.json similarity index 100% rename from cloudflare/package-lock.json rename to tailcall-cloudflare/package-lock.json diff --git a/cloudflare/package.json b/tailcall-cloudflare/package.json similarity index 100% rename from cloudflare/package.json rename to tailcall-cloudflare/package.json diff --git a/cloudflare/src/cache.rs b/tailcall-cloudflare/src/cache.rs similarity index 100% rename from cloudflare/src/cache.rs rename to tailcall-cloudflare/src/cache.rs diff --git a/cloudflare/src/env.rs b/tailcall-cloudflare/src/env.rs similarity index 100% rename from cloudflare/src/env.rs rename to tailcall-cloudflare/src/env.rs diff --git a/cloudflare/src/file.rs b/tailcall-cloudflare/src/file.rs similarity index 100% rename from cloudflare/src/file.rs rename to tailcall-cloudflare/src/file.rs diff --git a/cloudflare/src/handle.rs b/tailcall-cloudflare/src/handle.rs similarity index 100% rename from cloudflare/src/handle.rs rename to tailcall-cloudflare/src/handle.rs diff --git a/cloudflare/src/http.rs b/tailcall-cloudflare/src/http.rs similarity index 100% rename from cloudflare/src/http.rs rename to tailcall-cloudflare/src/http.rs diff --git a/cloudflare/src/lib.rs b/tailcall-cloudflare/src/lib.rs similarity index 100% rename from cloudflare/src/lib.rs rename to tailcall-cloudflare/src/lib.rs diff --git a/cloudflare/src/runtime.rs b/tailcall-cloudflare/src/runtime.rs similarity index 100% rename from cloudflare/src/runtime.rs rename to tailcall-cloudflare/src/runtime.rs diff --git a/cloudflare/tests/cf_tests.spec.ts b/tailcall-cloudflare/tests/cf_tests.spec.ts similarity index 100% rename from cloudflare/tests/cf_tests.spec.ts rename to tailcall-cloudflare/tests/cf_tests.spec.ts diff --git a/cloudflare/tests/mf.ts b/tailcall-cloudflare/tests/mf.ts similarity index 100% rename from cloudflare/tests/mf.ts rename to tailcall-cloudflare/tests/mf.ts diff --git a/cloudflare/wrangler.toml b/tailcall-cloudflare/wrangler.toml similarity index 100% rename from cloudflare/wrangler.toml rename to tailcall-cloudflare/wrangler.toml From a9bfc78fda47a7c1605c6b29e928430d0fd9e509 Mon Sep 17 00:00:00 2001 From: Kunam Balaram Reddy Date: Thu, 11 Apr 2024 21:19:43 +0530 Subject: [PATCH 3/3] refactor: Rename aws-lambda package to tailcall-aws-lambda (#1704) --- .github/workflows/ci.yml | 8 ++--- Cargo.lock | 34 +++++++++---------- Cargo.toml | 2 +- .../Cargo.toml | 2 +- .../src/http.rs | 0 .../src/main.rs | 0 .../src/runtime.rs | 0 7 files changed, 23 insertions(+), 23 deletions(-) rename {aws-lambda => tailcall-aws-lambda}/Cargo.toml (96%) rename {aws-lambda => tailcall-aws-lambda}/src/http.rs (100%) rename {aws-lambda => tailcall-aws-lambda}/src/main.rs (100%) rename {aws-lambda => tailcall-aws-lambda}/src/runtime.rs (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d1a9d37fbc..4acde149ca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,7 +39,7 @@ jobs: runs-on: ubuntu-latest defaults: run: - working-directory: ./aws-lambda + working-directory: ./tailcall-aws-lambda steps: - uses: actions/checkout@v4 @@ -424,18 +424,18 @@ jobs: env: APP_VERSION: ${{ needs.draft_release.outputs.create_release_name }} NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - run: cargo lambda build -p aws-lambda --release + run: cargo lambda build -p tailcall-aws-lambda --release - name: Rename Binary with Target Name run: | pwd - cp target/lambda/aws-lambda/bootstrap target/lambda/aws-lambda/tailcall-aws-lambda-bootstrap + cp target/lambda/tailcall-aws-lambda/bootstrap target/lambda/tailcall-aws-lambda/tailcall-aws-lambda-bootstrap - name: Upload AWS Lambda Bootstrap Binary uses: xresloader/upload-to-github-release@v1 with: release_id: ${{ needs.draft_release.outputs.create_release_id }} - file: target/lambda/aws-lambda/tailcall-aws-lambda-bootstrap + file: target/lambda/tailcall-aws-lambda/tailcall-aws-lambda-bootstrap overwrite: true semantic_release: name: Semantic Release diff --git a/Cargo.lock b/Cargo.lock index ec8f3c3ab8..944dcd97ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -559,23 +559,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "aws-lambda" -version = "0.1.0" -dependencies = [ - "anyhow", - "async-trait", - "dotenvy", - "hyper 0.14.28", - "lambda_http", - "lambda_runtime", - "reqwest", - "tailcall", - "tokio", - "tracing", - "tracing-subscriber", -] - [[package]] name = "aws_lambda_events" version = "0.15.0" @@ -5322,6 +5305,23 @@ dependencies = [ "which 6.0.1", ] +[[package]] +name = "tailcall-aws-lambda" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "dotenvy", + "hyper 0.14.28", + "lambda_http", + "lambda_runtime", + "reqwest", + "tailcall", + "tokio", + "tracing", + "tracing-subscriber", +] + [[package]] name = "tailcall-cloudflare" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index f330c5d1e0..bcf7f11d5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -178,7 +178,7 @@ default = ["cli", "js"] [workspace] -members = [".", "autogen", "aws-lambda", "tailcall-cloudflare"] +members = [".", "autogen", "tailcall-aws-lambda", "tailcall-cloudflare"] # Boost execution_spec snapshot diffing performance [profile.dev.package] diff --git a/aws-lambda/Cargo.toml b/tailcall-aws-lambda/Cargo.toml similarity index 96% rename from aws-lambda/Cargo.toml rename to tailcall-aws-lambda/Cargo.toml index 76bbd05e63..18ac824b7e 100644 --- a/aws-lambda/Cargo.toml +++ b/tailcall-aws-lambda/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "aws-lambda" +name = "tailcall-aws-lambda" version = "0.1.0" edition = "2021" diff --git a/aws-lambda/src/http.rs b/tailcall-aws-lambda/src/http.rs similarity index 100% rename from aws-lambda/src/http.rs rename to tailcall-aws-lambda/src/http.rs diff --git a/aws-lambda/src/main.rs b/tailcall-aws-lambda/src/main.rs similarity index 100% rename from aws-lambda/src/main.rs rename to tailcall-aws-lambda/src/main.rs diff --git a/aws-lambda/src/runtime.rs b/tailcall-aws-lambda/src/runtime.rs similarity index 100% rename from aws-lambda/src/runtime.rs rename to tailcall-aws-lambda/src/runtime.rs