From b17ac0844bfee3942f7c701eaf5116871e3a914f Mon Sep 17 00:00:00 2001 From: bryn Date: Fri, 20 Jan 2023 12:40:55 +0000 Subject: [PATCH 1/3] Add docs for subgraph header propagation Fix #2128 --- NEXT_CHANGELOG.md | 8 +++- .../configuration/header-propagation.mdx | 48 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 00e49edd02..9bb6dfd169 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -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)) @@ -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 \ No newline at end of file +By [@Geal](https://github.com/geal) in https://github.com/apollographql/router/pull/2431 diff --git a/docs/source/configuration/header-propagation.mdx b/docs/source/configuration/header-propagation.mdx index e223cb4497..b12b330a4e 100644 --- a/docs/source/configuration/header-propagation.mdx +++ b/docs/source/configuration/header-propagation.mdx @@ -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). From 7cc85b6321c28bc92b2c6ff9342f2162a812ad52 Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 24 Jan 2023 10:08:22 +0000 Subject: [PATCH 2/3] Update NEXT_CHANGELOG.md --- NEXT_CHANGELOG.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 5d83488b56..60425d2d48 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -132,9 +132,3 @@ supergraph: By [@bryncooke](https://github.com/bryncooke) in https://github.com/apollographql/router/pull/2440 -## 🥼 Experimental - -### JWT authentication ([Issue #912](https://github.com/apollographql/router/issues/912)) - -Experimental JWT authentication is now configurable. Here's a typical sample configuration fragment: - From 09ec30bc0299af58fc00bc3d72246cb92f5814f2 Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 24 Jan 2023 10:36:02 +0000 Subject: [PATCH 3/3] Update NEXT_CHANGELOG.md --- NEXT_CHANGELOG.md | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 8fd8ac4da6..d2a02e0f7d 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -98,22 +98,6 @@ By [@bryncooke](https://github.com/bryncooke) in https://github.com/apollographq ## 📚 Documentation - -### Creating custom metrics in plugins ([Issue #2294](https://github.com/apollographql/router/issues/2294)) - -To create your custom metrics in [Prometheus](https://prometheus.io/) you can use the [`tracing` macros](https://docs.rs/tracing/latest/tracing/index.html#macros) to generate an event. If you observe a specific naming pattern for your event, you'll be able to generate your own custom metrics directly in Prometheus. - -To publish a new metric, use tracing macros to generate an event that contains one of the following prefixes: - -`monotonic_counter.` _(non-negative numbers)_: Used when the metric will only ever increase. -`counter.`: For when the metric may increase or decrease over time. -`value.`: For discrete data points (i.e., when taking the sum of values does not make semantic sense) -`histogram.`: For building histograms (takes `f64`) - -This information is also available in [the Apollo Router documentation](https://www.apollographql.com/docs/router/customizations/native#add-custom-metrics). - -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.