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 docs for subgraph header propagation #2446

Merged
merged 5 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ This information is also available in [the Apollo Router documentation](https://

By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/2417

### Documentation on how to propagate headers between subgraph services ([Issue #2128](https://github.com/apollographql/router/issues/2128))

Migrating headers between subgraph services is possible via Rhai script. An example has been added to the header propagation page.

By [@bryncooke](https://github.com/bryncooke) in https://github.com/apollographql/router/pull/2446

## 🥼 Experimental

### JWT authentication ([Issue #912](https://github.com/apollographql/router/issues/912))
Expand All @@ -228,4 +234,4 @@ By [@garypen](https://github.com/garypen) in https://github.com/apollographql/ro

Experimental caching was [already available for APQ and query planning](https://github.com/apollographql/router/blob/dev/CHANGELOG.md#experimental--apq-and-query-planner-redis-caching-fixes-pr-2176) but required a custom router build with the `experimental_cache` Cargo feature. That feature is now removed to make that cache easier to test.

By [@Geal](https://github.com/geal) in https://github.com/apollographql/router/pull/2431
By [@Geal](https://github.com/geal) in https://github.com/apollographql/router/pull/2431
48 changes: 48 additions & 0 deletions docs/source/configuration/header-propagation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,51 @@ headers:
name: "router-subgraph-name"
value: "accounts"
```


## 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]("../customizations/rhai").

The approach relied on the feature that all requests have context that can be used to store data for the duration of the request:
1. On supbgraph response, copy header values into context.
2. On supbgraph request, copy header values from context into the subgraph request.


Example router.yaml that will use the Rhai script:
```rhai title="router.yaml"
rhai:
main: "main.rhai"
```

Example Rhai script that copies `request-id` and `user` headers:
```rhai title="./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](https://github.com/apollographql/router/issues/2444).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this !