From 4c8805aff6c41816d3eed1c42bc01bdaa91fa8c7 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Thu, 19 Oct 2023 11:40:22 +0200 Subject: [PATCH] add a router request builder (#3430) Fix #3267 The builder implementation was missing on the router request side, which means that router service level plugins cannot reuse the context if they unpack the request object --- .../feat_geal_router_request_builder.md | 5 +++ apollo-router/src/services/router.rs | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 .changesets/feat_geal_router_request_builder.md diff --git a/.changesets/feat_geal_router_request_builder.md b/.changesets/feat_geal_router_request_builder.md new file mode 100644 index 0000000000..d61b9f3dde --- /dev/null +++ b/.changesets/feat_geal_router_request_builder.md @@ -0,0 +1,5 @@ +### add a router request builder ([Issue #3267](https://github.com/apollographql/router/issues/3267)) + +The builder implementation was missing on the router request side, which means that router service level plugins cannot reuse the context if they unpack the request object + +By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/3430 \ No newline at end of file diff --git a/apollo-router/src/services/router.rs b/apollo-router/src/services/router.rs index 3d51ec2f15..d259a45799 100644 --- a/apollo-router/src/services/router.rs +++ b/apollo-router/src/services/router.rs @@ -21,6 +21,7 @@ use super::router_service::MULTIPART_DEFER_HEADER_VALUE; use super::router_service::MULTIPART_SUBSCRIPTION_HEADER_VALUE; use super::supergraph; use crate::graphql; +use crate::http_ext::header_map; use crate::json_ext::Path; use crate::services::TryIntoHeaderName; use crate::services::TryIntoHeaderValue; @@ -54,6 +55,41 @@ impl From> for Request { } } +impl From<(http::Request, Context)> for Request { + fn from((router_request, context): (http::Request, Context)) -> Self { + Self { + router_request, + context, + } + } +} + +#[buildstructor::buildstructor] +impl Request { + /// This is the constructor (or builder) to use when constructing a real Request. + /// + /// Required parameters are required in non-testing code to create a Request. + #[allow(clippy::too_many_arguments)] + #[builder(visibility = "pub")] + fn new( + context: Context, + headers: MultiMap, + uri: http::Uri, + method: Method, + body: Body, + ) -> Result { + let mut router_request = http::Request::builder() + .uri(uri) + .method(method) + .body(body)?; + *router_request.headers_mut() = header_map(headers)?; + Ok(Self { + router_request, + context, + }) + } +} + use displaydoc::Display; use thiserror::Error;