Skip to content

Commit

Permalink
add support for handling complex throw errors in responses (#3089)
Browse files Browse the repository at this point in the history
In #2677 we added support for throwing graphql errors from rhai. Support
was added for requests. This extends the mechanism to include responses.

fixes: #3069

<!-- start metadata -->

**Checklist**

Complete the checklist (and note appropriate exceptions) before a final
PR is raised.

- [x] Changes are compatible[^1]
- [x] Documentation[^2] completed
- [x] Performance impact assessed and acceptable
- Tests added and passing[^3]
    - [ ] Unit Tests
    - [ ] Integration Tests
    - [ ] Manual Tests

**Exceptions**

*Note any exceptions here*

**Notes**

[^1]. It may be appropriate to bring upcoming changes to the attention
of other (impacted) groups. Please endeavour to do this before seeking
PR approval. The mechanism for doing this will vary considerably, so use
your judgement as to how and when to do this.
[^2]. Configuration is an important part of many changes. Where
applicable please try to document configuration examples.
[^3]. Tick whichever testing boxes are applicable. If you are adding
Manual Tests:
- please document the manual testing (extensively) in the Exceptions.
- please raise a separate issue to automate the test and label it (or
ask for it to be labeled) as `manual test`
  • Loading branch information
garypen committed May 15, 2023
1 parent 3fd3c94 commit 94e787c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .changesets/fix_garypen_rhai_throw_error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Add support for throwing graphql errors in rhai responses ([Issue #3069](https://github.com/apollographql/router/issues/3069))

It's possible to throw a graphql error from rhai when processing a request. This extends the capability to include when processing a response.

Refer to the `Terminating client requests` section of the [Rhai api documentation](https://www.apollographql.com/docs/router/configuration/rhai) to learn how to throw GraphQL payloads.

By [@garypen](https://github.com/garypen) in https://github.com/apollographql/router/pull/3089
64 changes: 41 additions & 23 deletions apollo-router/src/plugins/rhai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,17 +456,27 @@ macro_rules! gen_map_response {
context: Context,
error_details: ErrorDetails,
) -> $base::Response {
let res = $base::Response::error_builder()
.errors(vec![Error {
// TODO
message: error_details.message.unwrap_or_default(),
..Default::default()
}])
.status_code(error_details.status)
.context(context)
.build()
.expect("can't fail to build our error message");
res
if let Some(body) = error_details.body {
$base::Response::builder()
.extensions(body.extensions)
.errors(body.errors)
.status_code(error_details.status)
.context(context)
.and_data(body.data)
.and_label(body.label)
.and_path(body.path)
.build()
} else {
$base::Response::error_builder()
.errors(vec![Error {
message: error_details.message.unwrap_or_default(),
..Default::default()
}])
.status_code(error_details.status)
.context(context)
.build()
.expect("can't fail to build our error message")
}
}
let shared_response = Shared::new(Mutex::new(Some(response)));
let result: Result<Dynamic, Box<EvalAltResult>> = if $callback.is_curried() {
Expand Down Expand Up @@ -515,18 +525,26 @@ macro_rules! gen_map_deferred_response {
context: Context,
error_details: ErrorDetails,
) -> $response {
let res = $response::error_builder()

.errors(vec![Error {
// TODO
message: error_details.message.unwrap_or_default(),
..Default::default()
}])
.status_code(error_details.status)
.context(context)
.build()
.expect("can't fail to build our error message");
res
if let Some(body) = error_details.body {
$response::builder()
.extensions(body.extensions)
.errors(body.errors)
.status_code(error_details.status)
.context(context)
.and_data(body.data)
.and_label(body.label)
.and_path(body.path)
.build()
} else {
$response::error_builder()
.errors(vec![Error {
message: error_details.message.unwrap_or_default(),
..Default::default()
}])
.status_code(error_details.status)
.context(context)
.build()
}.expect("can't fail to build our error message")
}

// we split the response stream into headers+first response, then a stream of deferred responses
Expand Down
1 change: 1 addition & 0 deletions docs/source/customizations/rhai-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn supergraph_service(service) {
}]
}
};
};
// Map our request using our closure
service.map_request(f);
}
Expand Down

0 comments on commit 94e787c

Please sign in to comment.