Skip to content

Commit

Permalink
add a router request builder (#3430)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Geal authored Oct 19, 2023
1 parent 2db50ea commit 4c8805a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changesets/feat_geal_router_request_builder.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions apollo-router/src/services/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,41 @@ impl From<http::Request<Body>> for Request {
}
}

impl From<(http::Request<Body>, Context)> for Request {
fn from((router_request, context): (http::Request<Body>, 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<TryIntoHeaderName, TryIntoHeaderValue>,
uri: http::Uri,
method: Method,
body: Body,
) -> Result<Request, BoxError> {
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;

Expand Down

0 comments on commit 4c8805a

Please sign in to comment.