Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request.uri.port to rhai engine #6119

Closed
Closed
11 changes: 11 additions & 0 deletions .changesets/feat_feature_rhaiuriport.md
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions apollo-router/src/plugins/rhai/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dynamic, Box<EvalAltResult>> {
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<EvalAltResult>> {
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 {
Expand Down
4 changes: 2 additions & 2 deletions apollo-router/src/plugins/rhai/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,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 229, position 5)'".to_string()));
assert_eq!(processed_error.message, Some("rhai execution error: 'Runtime error: I have raised an error (line 232, position 5)'".to_string()));
} else {
// Test failed
panic!("error processed incorrectly");
Expand Down Expand Up @@ -685,7 +685,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 240, position 5)'"
"rhai execution error: 'Runtime error: #{\"status\": 400} (line 243, position 5)'"
.to_string()
)
);
Expand Down
3 changes: 3 additions & 0 deletions apollo-router/tests/fixtures/request_response_test.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ fn process_common_request(check_context_method_and_id, check_body, 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_router_request(request){
Expand Down
Loading