Skip to content

Commit

Permalink
[docs] Update Rhai docs around response headers (#3927)
Browse files Browse the repository at this point in the history
This is called out in our docs so it should probably live in the
examples too
  • Loading branch information
smyrick authored Nov 8, 2023
1 parent 4474e7e commit 8231c89
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion docs/source/customizations/rhai-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ The following fields are identical in behavior to their `request` counterparts:
* [`body`](#requestbody)
* [`body.extensions`](#requestbodyextensions)

Note: Be particularly careful when interacting with headers in a response context. For router_service(), supergraph_service() and execution_service(), response headers only exist for the first response in a deferred response stream. You can handle this by making use of the `is_primary()` function which will return true if a response is the first (or primary) response. If you do try to access the headers in a non-primary response, then you'll raise an exception which can be handled like any other rhai exception, but is not so convenient as using the `is_primary()` method.
### `response.is_primary()`

Be particularly careful when interacting with headers in a response context. For router_service(), supergraph_service() and execution_service(), response headers only exist for the first response in a deferred response stream. You can handle this by making use of the `is_primary()` function which will return true if a response is the first (or primary) response. If you do try to access the headers in a non-primary response, then you'll raise an exception which can be handled like any other rhai exception, but is not so convenient as using the `is_primary()` method.

```rhai
if response.is_primary() {
Expand Down
17 changes: 11 additions & 6 deletions examples/cache-control/rhai/src/cache_control.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ fn subgraph_service(service, subgraph) {
fn supergraph_service(service) {
// attach the cache-control header if enough data is available
service.map_response(|response| {
let uncacheable = response.context.cache_control_uncacheable;
let max_age = response.context.cache_control_max_age;
let scope = response.context.cache_control_scope;

if uncacheable != true && max_age != () && scope != () {
response.headers["cache-control"] = `max-age=${max_age}, ${scope}`;
}
// Note the check for is_primary() to support deferred response headers
// https://www.apollographql.com/docs/router/customizations/rhai-api/#response-interface
if response.is_primary() {
let uncacheable = response.context.cache_control_uncacheable;
let max_age = response.context.cache_control_max_age;
let scope = response.context.cache_control_scope;

if uncacheable != true && max_age != () && scope != () {
response.headers["cache-control"] = `max-age=${max_age}, ${scope}`;
}
}
});
}

Expand Down

0 comments on commit 8231c89

Please sign in to comment.