Docs
Try Apollo Studio

Sending HTTP headers to subgraphs

Configure which headers the Apollo Router sends to which subgraphs


You can configure which HTTP headers the Apollo Router includes in its requests to each of your subgraphs. You can define per-subgraph header rules, along with rules that apply to all subgraphs.

You define header rules in your YAML configuration file, like so:

router.yaml
# ...other configuration...
headers:
all: # Header rules for all subgraphs
request:
- propagate:
matching: ^upstream-header-.*
- remove:
named: "x-legacy-account-id"
subgraphs:
products: # Header rules for just the products subgraph
request:
- insert:
name: "router-subgraph-name"
value: "products"

Supported header rules

The Apollo Router supports the following types of header rules:

propagate

Enables you to selectively pass along headers that were included in the client's request to the router.

You can specify which headers to propagate based on a matching regex pattern:

- propagate:
matching: .*

Note: The Apollo Router never propagates so-called hop-by-hop headers (such as Content-Length) when propagating by pattern.

Alternatively, you can provide a static string via the named option. These named configurations have additional flexibility, because they support the following options:

  • default: A value to set if no value was sent by the client
  • rename: Renames the header's key to the provided value
- propagate:
named: "x-user-id"
default: "abc123"
rename: "account-id"

remove

Enables you to selectively remove headers that were included in the client's request to the router. Like propagate, this option can match either a static string or a regular expression.

# Do not send this subgraph the "Cookie" header.
- remove:
named: "Cookie"
- remove:
# Remove headers that include the legacy 'x-' prefix.
matching: ^x-.*$

insert

Enables you to add custom headers to requests going to a specific subgraph. These headers are strings (statics, coming from request body or from context) that originate in the router, instead of originating in the client.

  • Insert static header
- insert:
name: "sent-from-our-apollo-router"
value: "indeed"
  • Insert header from context
- insert:
name: "sent-from-our-apollo-router-context"
from_context: "my_key_in_context"
  • Insert header from request body
- insert:
name: "sent-from-our-apollo-router-request-body"
path: ".operationName" # It's a JSON path query to fetch the operation name from request body
default: "UNKNOWN" # If no operationName has been specified

Example JSON path queries

Let's say you have a JSON request body with the following structure:

{
"query": "{ products { id name } }",
"extensions": {
"metadata": [
{
"app_name": "random_app_name"
}
]
}
}

To fetch the value of the field app_name, the corresponding path is .extensions.metadata[0].app_name.

JSON path queries always begin with a period .

With this configuration:

headers:
all:
request:
- insert:
name: from_app_name
path: .extensions.metadata[0].app_name

You will pass a header to all your subgraphs: "from_app_name": "random_app_name"

Rule ordering

Header rules are applied in the same order they're declared, and later rules can override the effects of earlier rules. Consider this example:

bad_configuration.yaml
headers:
all:
request:
- remove:
named: "test"
- propagate:
matching: .*

In this example, first any header named test is removed from the list of headers to propagate. However, the list of headers to propagate is currently empty! Next, the propagate rule adds all headers to the propagation list, including test.

To correctly remove a header from the propagation list, make sure to define your remove rule after any propagate rules:

good_configuration.yaml
headers:
all:
request:
- propagate:
matching: .*
- remove:
named: "test"

With this ordering, first all headers are added to the propagation list, then the test header is removed.

Example

Here's a complete example showing all the possible configuration options in use:

router.yaml
headers:
# Header rules for all subgraphs
all:
request:
# Propagate matching headers
- propagate:
matching: ^upstream-header-.*
# Propagate matching headers
- propagate:
named: "some-header"
default: "default-value"
rename: "destination-header"
# Remove the "x-legacy-account-id" header
- remove:
named: "x-legacy-account-id"
# Remove matching headers
- remove:
matching: ^x-deprecated-.*
# Insert the 'my-company' header
- insert:
name: "my-company"
value: "acme"
# Subgraph-specific header rules
subgraphs:
products:
request:
# Calls to the products subgraph have the "router-subgraph-name" header set to `products`.
- insert:
name: "router-subgraph-name"
value: "products"
accounts:
request:
# Calls to the accounts subgraph have the "router-subgraph-name" header set to `accounts`.
- insert:
name: "router-subgraph-name"
value: "accounts"

Response header propagation

It is not currently possible to propagate response headers from subgraphs to clients using YAML configuration alone. It can however be achieved using Rhai scripting.

This approach relies on the fact that all requests have context that can be used to store data for the duration of the request:

  1. For each subgraph response, copy header values into context.
  2. For the supergraph response, copy header values from the context onto the response.

Example router.yaml that will use the Rhai script:

router.yaml
rhai:
main: "main.rhai"

Example Rhai script that collects set-cookie headers from subgraphs and merges them into a single client response header.

./rhai/main.rhai
fn router_service(service) {
let add_cookies_to_response = |response| {
if response.context["set_cookie_headers"]?.len > 0 {
let cookie = "";
for header in response.context["set_cookie_headers"] {
cookie += header + "; ";
}
response.headers["set-cookie"] = cookie;
}
};
service.map_response(add_cookies_to_response);
}
fn subgraph_service(service, subgraph) {
let store_cookies_from_subgraphs = |response| {
if response.headers["set-cookie"] != () {
if response.context["set_cookie_headers"] == () {
response.context.set_cookie_headers = []
}
response.context.set_cookie_headers += response.headers["set-cookie"]
}
};
service.map_response(store_cookies_from_subgraphs);
}

If you would like to see a YAML based solution then please leave us a comment on our issue tracker.

Propagation between subgraphs

It is not currently possible to propagate headers between subgraphs using yaml config alone. It can however be achieved using Rhai scripting.

This approach relies on the fact that all requests have context that can be used to store data for the duration of the request:

  1. For each subgraph response, copy header values into context.
  2. For each subgraph request, copy header values from context into the subgraph request.

Example router.yaml that will use the Rhai script:

router.yaml
rhai:
main: "main.rhai"

Example Rhai script that copies request-id and user headers:

./rhai/main.rhai
fn subgraph_service(service, subgraph) {
// The list of headers that you which to propagate.
let headers = ["request-id", "user"];
// Callback for subgraph requests. Inserts headers from context into the subgraph request.
let request_callback = |request| {
for key in headers {
if request.context[key] != () {
request.subgraph.headers[key] = request.context[key];
}
}
};
// Callback for subgraph responses. Pulls header values out of the response and inserts them into context.
let response_callback = |response| {
for key in headers {
if key in response.headers {
response.context[key] = response.headers[key];
}
}
};
// Register the callbacks.
service.map_request(request_callback);
service.map_response(response_callback);
}

If you would like to see a YAML based solution then please leave us a comment on our issue tracker.

Edit on GitHub
Previous
Subgraph error inclusion
Next
Traffic shaping