From ffc52dfc7657ce4147f3252bb05464c0ec2b5d5a Mon Sep 17 00:00:00 2001 From: Andrew McGivery Date: Fri, 4 Oct 2024 01:37:51 -0400 Subject: [PATCH 1/2] Add request.uri.port to rhai engine --- apollo-router/src/plugins/rhai/engine.rs | 16 ++++++++++++++++ apollo-router/src/plugins/rhai/tests.rs | 4 ++-- .../tests/fixtures/request_response_test.rhai | 3 +++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apollo-router/src/plugins/rhai/engine.rs b/apollo-router/src/plugins/rhai/engine.rs index 062143711f..32732bb435 100644 --- a/apollo-router/src/plugins/rhai/engine.rs +++ b/apollo-router/src/plugins/rhai/engine.rs @@ -1147,6 +1147,22 @@ mod router_plugin { Ok(()) } + // Uri.port + #[rhai_fn(get = "port", pure, return_raw)] + pub(crate) fn uri_port_get(x: &mut Uri) -> Result> { + to_dynamic(x.port_u16().map(|port| port.to_string())) + } + + #[rhai_fn(set = "port", pure, return_raw)] + pub(crate) fn uri_port_set(x: &mut Uri, value: &str) -> Result<(), Box> { + let mut parts: Parts = x.clone().into_parts(); + let host = x.host().unwrap_or_default(); + parts.authority = + Some(Authority::from_str(&format!("{host}:{value}")).map_err(|e| e.to_string())?); + *x = Uri::from_parts(parts).map_err(|e| e.to_string())?; + Ok(()) + } + // Response.label #[rhai_fn(get = "label", pure)] pub(crate) fn response_label_get(x: &mut Response) -> Dynamic { diff --git a/apollo-router/src/plugins/rhai/tests.rs b/apollo-router/src/plugins/rhai/tests.rs index 11c4f3a03a..226349b1d7 100644 --- a/apollo-router/src/plugins/rhai/tests.rs +++ b/apollo-router/src/plugins/rhai/tests.rs @@ -641,7 +641,7 @@ async fn it_can_process_string_subgraph_forbidden() { if let Err(error) = call_rhai_function("process_subgraph_response_string").await { let processed_error = process_error(error); assert_eq!(processed_error.status, StatusCode::INTERNAL_SERVER_ERROR); - assert_eq!(processed_error.message, Some("rhai execution error: 'Runtime error: I have raised an error (line 223, position 5)'".to_string())); + assert_eq!(processed_error.message, Some("rhai execution error: 'Runtime error: I have raised an error (line 226, position 5)'".to_string())); } else { // Test failed panic!("error processed incorrectly"); @@ -669,7 +669,7 @@ async fn it_cannot_process_om_subgraph_missing_message_and_body() { assert_eq!( processed_error.message, Some( - "rhai execution error: 'Runtime error: #{\"status\": 400} (line 234, position 5)'" + "rhai execution error: 'Runtime error: #{\"status\": 400} (line 237, position 5)'" .to_string() ) ); diff --git a/apollo-router/tests/fixtures/request_response_test.rhai b/apollo-router/tests/fixtures/request_response_test.rhai index 4dc88d42ad..c904fcfd8b 100644 --- a/apollo-router/tests/fixtures/request_response_test.rhai +++ b/apollo-router/tests/fixtures/request_response_test.rhai @@ -33,6 +33,9 @@ fn process_common_request(check_context_method_and_id, request) { if request.uri.path != "/" { throw(`query: expected: "/", actual: ${request.uri.path}`); } + if request.uri.port != () { + throw(`query: expected: (), actual: ${request.uri.host}`); + } } fn process_supergraph_request(request) { From 7a5273d226c4a4edad27c05ca86bd5848d9f217c Mon Sep 17 00:00:00 2001 From: Andrew McGivery Date: Fri, 4 Oct 2024 01:42:31 -0400 Subject: [PATCH 2/2] Added changeset --- .changesets/feat_feature_rhaiuriport.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .changesets/feat_feature_rhaiuriport.md diff --git a/.changesets/feat_feature_rhaiuriport.md b/.changesets/feat_feature_rhaiuriport.md new file mode 100644 index 0000000000..f010486e5b --- /dev/null +++ b/.changesets/feat_feature_rhaiuriport.md @@ -0,0 +1,11 @@ +### Add request.uri.port to rhai engine ([PR #6119](https://github.com/apollographql/router/pull/6119)) + +`request.uri.port` and `request.subgraph.uri.port` to rhai engine for both read and write. An example fo where this may be useful is if you need to dynamically change the uri of a subgraph call, including setting a port: + +```rhai +fn subgraph_request_callback (request) { + request.subgraph.uri.port = "4001"; +} +``` + +By [@andrewmcgivery](https://github.com/andrewmcgivery) in https://github.com/apollographql/router/pull/6119