Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ By [@USERNAME](https://github.com/USERNAME) in https://github.com/apollographql/

## ❗ BREAKING ❗

### Rename map_future_with_context to map_future_with_request_data ([PR #1547](https://github.com/apollographql/router/pull/1547))

The function is not very well named since it's in fact used to extract any data from a request for use in a future. This rename makes it clear.

By [@garypen](https://github.com/garypen)

### Rename traffic shaping deduplication options ([PR #1540](https://github.com/apollographql/router/pull/1540))

In the traffic shaping module:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,54 @@ use tower::Layer;
use tower::Service;

#[derive(Clone)]
pub struct MapFutureWithContextLayer<C, F> {
ctx_fn: C,
map_fn: F,
pub struct MapFutureWithRequestDataLayer<RF, MF> {
req_fn: RF,
map_fn: MF,
}

impl<C, F> MapFutureWithContextLayer<C, F> {
pub fn new(ctx_fn: C, map_fn: F) -> Self {
Self { ctx_fn, map_fn }
impl<RF, MF> MapFutureWithRequestDataLayer<RF, MF> {
pub fn new(req_fn: RF, map_fn: MF) -> Self {
Self { req_fn, map_fn }
}
}

impl<S, C, F> Layer<S> for MapFutureWithContextLayer<C, F>
impl<S, RF, MF> Layer<S> for MapFutureWithRequestDataLayer<RF, MF>
where
F: Clone,
C: Clone,
RF: Clone,
MF: Clone,
{
type Service = MapFutureWithContextService<S, C, F>;
type Service = MapFutureWithRequestDataService<S, RF, MF>;

fn layer(&self, inner: S) -> Self::Service {
MapFutureWithContextService::new(inner, self.ctx_fn.clone(), self.map_fn.clone())
MapFutureWithRequestDataService::new(inner, self.req_fn.clone(), self.map_fn.clone())
}
}

pub struct MapFutureWithContextService<S, C, F> {
pub struct MapFutureWithRequestDataService<S, RF, MF> {
inner: S,
ctx_fn: C,
map_fn: F,
req_fn: RF,
map_fn: MF,
}

impl<S, C, F> MapFutureWithContextService<S, C, F> {
pub fn new(inner: S, ctx_fn: C, map_fn: F) -> MapFutureWithContextService<S, C, F>
impl<S, RF, MF> MapFutureWithRequestDataService<S, RF, MF> {
pub fn new(inner: S, req_fn: RF, map_fn: MF) -> MapFutureWithRequestDataService<S, RF, MF>
where
C: Clone,
F: Clone,
RF: Clone,
MF: Clone,
{
MapFutureWithContextService {
MapFutureWithRequestDataService {
inner,
ctx_fn,
req_fn,
map_fn,
}
}
}

impl<R, S, F, C, T, E, Fut, Ctx> Service<R> for MapFutureWithContextService<S, C, F>
impl<R, S, MF, RF, T, E, Fut, ReqData> Service<R> for MapFutureWithRequestDataService<S, RF, MF>
where
S: Service<R>,
C: FnMut(&R) -> Ctx,
F: FnMut(Ctx, S::Future) -> Fut,
RF: FnMut(&R) -> ReqData,
MF: FnMut(ReqData, S::Future) -> Fut,
E: From<S::Error>,
Fut: Future<Output = Result<T, E>>,
{
Expand All @@ -71,8 +71,8 @@ where
}

fn call(&mut self, req: R) -> Self::Future {
let ctx = (self.ctx_fn)(&req);
(self.map_fn)(ctx, self.inner.call(req))
let data = (self.req_fn)(&req);
(self.map_fn)(data, self.inner.call(req))
}
}

Expand All @@ -98,18 +98,21 @@ mod test {
.returning(|_| Ok(SupergraphResponse::fake_builder().build().unwrap()));

let mut service = ServiceBuilder::new()
.map_future_with_context(
.map_future_with_request_data(
|req: &SupergraphRequest| {
req.originating_request
.headers()
.get("hello")
.cloned()
.unwrap()
},
|ctx: HeaderValue, resp| async move {
|value: HeaderValue, resp| async move {
let resp: Result<SupergraphResponse, BoxError> = resp.await;
resp.map(|mut response| {
response.response.headers_mut().insert("hello", ctx.clone());
response
.response
.headers_mut()
.insert("hello", value.clone());
response
})
},
Expand Down
42 changes: 21 additions & 21 deletions apollo-router/src/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use tracing::Span;

use crate::layers::async_checkpoint::AsyncCheckpointLayer;
use crate::layers::instrument::InstrumentLayer;
use crate::layers::map_future_with_context::MapFutureWithContextLayer;
use crate::layers::map_future_with_context::MapFutureWithContextService;
use crate::layers::map_future_with_request_data::MapFutureWithRequestDataLayer;
use crate::layers::map_future_with_request_data::MapFutureWithRequestDataService;
use crate::layers::sync_checkpoint::CheckpointLayer;

pub mod map_future_with_context;
pub mod map_future_with_request_data;

pub mod async_checkpoint;
pub mod instrument;
Expand Down Expand Up @@ -204,10 +204,10 @@ pub trait ServiceBuilderExt<L>: Sized {
///
/// # Arguments
///
/// * `ctx_fn`: The callback to extract a context from the request.
/// * `req_fn`: The callback to extract data from the request.
/// * `map_fn`: The callback to map the future.
///
/// returns: ServiceBuilder<Stack<MapFutureWithContextLayer<C, F>, L>>
/// returns: ServiceBuilder<Stack<MapFutureWithRequestDataLayer<RF, MF>, L>>
///
/// # Examples
///
Expand All @@ -222,19 +222,19 @@ pub trait ServiceBuilderExt<L>: Sized {
/// # use apollo_router::layers::ServiceBuilderExt;
/// # fn test(service: supergraph::BoxService) {
/// let _ : supergraph::BoxService = ServiceBuilder::new()
/// .map_future_with_context(
/// .map_future_with_request_data(
/// |req: &supergraph::Request| req.context.clone(),
/// |ctx : Context, fut| async { fut.await })
/// .service(service)
/// .boxed();
/// # }
/// ```
fn map_future_with_context<C, F>(
fn map_future_with_request_data<RF, MF>(
self,
ctx_fn: C,
map_fn: F,
) -> ServiceBuilder<Stack<MapFutureWithContextLayer<C, F>, L>> {
self.layer(MapFutureWithContextLayer::new(ctx_fn, map_fn))
req_fn: RF,
map_fn: MF,
) -> ServiceBuilder<Stack<MapFutureWithRequestDataLayer<RF, MF>, L>> {
self.layer(MapFutureWithRequestDataLayer::new(req_fn, map_fn))
}

/// Utility function to allow us to specify default methods on this trait rather than duplicating in the impl.
Expand Down Expand Up @@ -266,10 +266,10 @@ pub trait ServiceExt<Request>: Service<Request> {
///
/// # Arguments
///
/// * `ctx_fn`: The callback to extract a context from the request.
/// * `req_fn`: The callback to extract data from the request.
/// * `map_fn`: The callback to map the future.
///
/// returns: ServiceBuilder<Stack<MapFutureWithContextLayer<C, F>, L>>
/// returns: ServiceBuilder<Stack<MapFutureWithRequestDataLayer<RF, MF>, L>>
///
/// # Examples
///
Expand All @@ -285,24 +285,24 @@ pub trait ServiceExt<Request>: Service<Request> {
/// # use apollo_router::layers::ServiceExt as ApolloServiceExt;
/// # fn test(service: supergraph::BoxService) {
/// let _ : supergraph::BoxService = service
/// .map_future_with_context(
/// .map_future_with_request_data(
/// |req: &supergraph::Request| req.context.clone(),
/// |ctx : Context, fut| async { fut.await }
/// )
/// .boxed();
/// # }
/// ```
fn map_future_with_context<C, F>(
fn map_future_with_request_data<RF, MF>(
self,
cxt_fn: C,
map_fn: F,
) -> MapFutureWithContextService<Self, C, F>
req_fn: RF,
map_fn: MF,
) -> MapFutureWithRequestDataService<Self, RF, MF>
where
Self: Sized,
C: Clone,
F: Clone,
RF: Clone,
MF: Clone,
{
MapFutureWithContextService::new(self, cxt_fn, map_fn)
MapFutureWithRequestDataService::new(self, req_fn, map_fn)
}
}
impl<T: ?Sized, Request> ServiceExt<Request> for T where T: Service<Request> {}
2 changes: 1 addition & 1 deletion apollo-router/src/plugins/expose_query_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Plugin for ExposeQueryPlan {
fn supergraph_service(&self, service: supergraph::BoxService) -> supergraph::BoxService {
let conf_enabled = self.enabled;
service
.map_future_with_context(move |req: &supergraph::Request| {
.map_future_with_request_data(move |req: &supergraph::Request| {
let is_enabled = conf_enabled && req.originating_request.headers().get(EXPOSE_QUERY_PLAN_HEADER_NAME) == Some(&HeaderValue::from_static("true"));
if is_enabled {
req.context.insert(ENABLED_CONTEXT_KEY, true).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions apollo-router/src/plugins/telemetry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl Plugin for Telemetry {
.instrument(Self::supergraph_service_span(
config.apollo.clone().unwrap_or_default(),
))
.map_future_with_context(
.map_future_with_request_data(
move |req: &SupergraphRequest| {
Self::populate_context(config.clone(), req);
req.context.clone()
Expand Down Expand Up @@ -392,7 +392,7 @@ impl Plugin for Telemetry {
"otel.kind" = %SpanKind::Internal,
)
})
.map_future_with_context(
.map_future_with_request_data(
move |sub_request: &SubgraphRequest| {
let subgraph_metrics_conf = subgraph_metrics_conf.clone();
let mut attributes = HashMap::new();
Expand Down