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
9 changes: 8 additions & 1 deletion apollo-router/src/axum_factory/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,14 @@ async fn test_supergraph_timeout() {

// we do the entire supergraph rebuilding instead of using `from_supergraph_mock_callback_and_configuration`
// because we need the plugins to apply on the supergraph
let mut plugins = create_plugins(&conf, &schema, planner.subgraph_schemas(), None, None)
let subgraph_schemas = Arc::new(
planner
.subgraph_schemas()
.iter()
.map(|(k, v)| (k.clone(), v.schema.clone()))
.collect(),
);
let mut plugins = create_plugins(&conf, &schema, subgraph_schemas, None, None)
.await
.unwrap();

Expand Down
39 changes: 38 additions & 1 deletion apollo-router/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ pub(crate) mod test_utils {
pub(crate) fn collect_metrics() -> Metrics {
let mut metrics = Metrics::default();
let (_, reader) = meter_provider_and_readers();
reader.collect(&mut metrics.resource_metrics).unwrap();
reader
.collect(&mut metrics.resource_metrics)
.expect("Failed to collect metrics. Did you forget to use `async{}.with_metrics()`? See dev-docs/metrics.md");
metrics
}

Expand Down Expand Up @@ -926,6 +928,10 @@ macro_rules! assert_metric {
};
}

/// Assert the value of a counter metric that has the given name and attributes.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
macro_rules! assert_counter {
($($name:ident).+, $value: expr, $($attr_key:literal = $attr_value:expr),+) => {
Expand Down Expand Up @@ -965,6 +971,10 @@ macro_rules! assert_counter {
};
}

/// Assert the value of a counter metric that has the given name and attributes.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
macro_rules! assert_up_down_counter {

Expand Down Expand Up @@ -998,6 +1008,10 @@ macro_rules! assert_up_down_counter {
};
}

/// Assert the value of a gauge metric that has the given name and attributes.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
macro_rules! assert_gauge {

Expand Down Expand Up @@ -1064,6 +1078,10 @@ macro_rules! assert_histogram_count {
};
}

/// Assert the sum value of a histogram metric with the given name and attributes.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
macro_rules! assert_histogram_sum {

Expand Down Expand Up @@ -1097,6 +1115,10 @@ macro_rules! assert_histogram_sum {
};
}

/// Assert that a histogram metric exists with the given name and attributes.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
macro_rules! assert_histogram_exists {

Expand Down Expand Up @@ -1130,6 +1152,10 @@ macro_rules! assert_histogram_exists {
};
}

/// Assert that a histogram metric does not exist with the given name and attributes.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
macro_rules! assert_histogram_not_exists {

Expand Down Expand Up @@ -1163,6 +1189,13 @@ macro_rules! assert_histogram_not_exists {
};
}

/// Assert that all metrics match an [insta] snapshot.
///
/// Consider using [assert_non_zero_metrics_snapshot] to produce more grokkable snapshots if
/// zero-valued metrics are not relevant to your test.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
#[allow(unused_macros)]
macro_rules! assert_metrics_snapshot {
Expand All @@ -1181,6 +1214,10 @@ macro_rules! assert_metrics_snapshot {
};
}

/// Assert that all metrics with a non-zero value match an [insta] snapshot.
///
/// In asynchronous tests, you must use [`FutureMetricsExt::with_metrics`]. See dev-docs/metrics.md
/// for details: <https://github.com/apollographql/router/blob/4fc63d55104c81c77e6e0a3cca615eac28e39dc3/dev-docs/metrics.md#testing>
#[cfg(test)]
#[allow(unused_macros)]
macro_rules! assert_non_zero_metrics_snapshot {
Expand Down
9 changes: 4 additions & 5 deletions apollo-router/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ use tower::ServiceBuilder;
use crate::graphql;
use crate::layers::ServiceBuilderExt;
use crate::notification::Notify;
use crate::query_planner::fetch::SubgraphSchemas;
use crate::router_factory::Endpoint;
use crate::services::execution;
use crate::services::router;
Expand Down Expand Up @@ -75,7 +74,7 @@ pub struct PluginInit<T> {
pub(crate) supergraph_schema: Arc<Valid<Schema>>,

/// The parsed subgraph schemas from the query planner, keyed by subgraph name
pub(crate) subgraph_schemas: Arc<SubgraphSchemas>,
pub(crate) subgraph_schemas: Arc<HashMap<String, Arc<Valid<Schema>>>>,

/// Launch ID
pub(crate) launch_id: Option<Arc<String>>,
Expand Down Expand Up @@ -176,7 +175,7 @@ where
supergraph_sdl: Arc<String>,
supergraph_schema_id: Arc<String>,
supergraph_schema: Arc<Valid<Schema>>,
subgraph_schemas: Option<Arc<SubgraphSchemas>>,
subgraph_schemas: Option<Arc<HashMap<String, Arc<Valid<Schema>>>>>,
launch_id: Option<Option<Arc<String>>>,
notify: Notify<String, graphql::Response>,
) -> Self {
Expand All @@ -201,7 +200,7 @@ where
supergraph_sdl: Arc<String>,
supergraph_schema_id: Arc<String>,
supergraph_schema: Arc<Valid<Schema>>,
subgraph_schemas: Option<Arc<SubgraphSchemas>>,
subgraph_schemas: Option<Arc<HashMap<String, Arc<Valid<Schema>>>>>,
launch_id: Option<Arc<String>>,
notify: Notify<String, graphql::Response>,
) -> Result<Self, BoxError> {
Expand All @@ -224,7 +223,7 @@ where
supergraph_sdl: Option<Arc<String>>,
supergraph_schema_id: Option<Arc<String>>,
supergraph_schema: Option<Arc<Valid<Schema>>>,
subgraph_schemas: Option<Arc<SubgraphSchemas>>,
subgraph_schemas: Option<Arc<HashMap<String, Arc<Valid<Schema>>>>>,
launch_id: Option<Arc<String>>,
notify: Option<Notify<String, graphql::Response>>,
) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ mod tests {
let mut demand_controlled_subgraph_schemas = HashMap::new();
for (subgraph_name, subgraph_schema) in planner.subgraph_schemas().iter() {
let demand_controlled_subgraph_schema =
DemandControlledSchema::new(subgraph_schema.clone()).unwrap();
DemandControlledSchema::new(subgraph_schema.schema.clone()).unwrap();
demand_controlled_subgraph_schemas
.insert(subgraph_name.to_string(), demand_controlled_subgraph_schema);
}
Expand Down
8 changes: 7 additions & 1 deletion apollo-router/src/plugins/include_subgraph_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,13 @@ mod test {
.await
.unwrap();
let schema = planner.schema();
let subgraph_schemas = planner.subgraph_schemas();
let subgraph_schemas = Arc::new(
planner
.subgraph_schemas()
.iter()
.map(|(k, v)| (k.clone(), v.schema.clone()))
.collect(),
);

let builder = PluggableSupergraphServiceBuilder::new(planner);

Expand Down
7 changes: 6 additions & 1 deletion apollo-router/src/plugins/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ impl<T: Into<Box<dyn DynPlugin + 'static>> + 'static> PluginTestHarness<T> {
.supergraph_schema_id(crate::spec::Schema::schema_id(&supergraph_sdl).into())
.supergraph_sdl(supergraph_sdl)
.supergraph_schema(Arc::new(parsed_schema))
.subgraph_schemas(subgraph_schemas)
.subgraph_schemas(Arc::new(
subgraph_schemas
.iter()
.map(|(k, v)| (k.clone(), v.schema.clone()))
.collect(),
))
.notify(Notify::default())
.build();

Expand Down
8 changes: 7 additions & 1 deletion apollo-router/src/plugins/traffic_shaping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,13 @@ mod test {
let planner = QueryPlannerService::new(schema.clone(), config.clone())
.await
.unwrap();
let subgraph_schemas = planner.subgraph_schemas();
let subgraph_schemas = Arc::new(
planner
.subgraph_schemas()
.iter()
.map(|(k, v)| (k.clone(), v.schema.clone()))
.collect(),
);

let mut builder =
PluggableSupergraphServiceBuilder::new(planner).with_configuration(config.clone());
Expand Down
10 changes: 3 additions & 7 deletions apollo-router/src/query_planner/caching_query_planner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;
use std::hash::Hash;
use std::hash::Hasher;
use std::sync::Arc;
use std::task;

use apollo_compiler::validation::Valid;
use futures::future::BoxFuture;
use indexmap::IndexMap;
use query_planner::QueryPlannerPlugin;
Expand Down Expand Up @@ -61,7 +59,7 @@ pub(crate) struct CachingQueryPlanner<T: Clone> {
>,
delegate: T,
schema: Arc<Schema>,
subgraph_schemas: Arc<HashMap<String, Arc<Valid<apollo_compiler::Schema>>>>,
subgraph_schemas: Arc<SubgraphSchemas>,
plugins: Arc<Plugins>,
enable_authorization_directives: bool,
config_mode_hash: Arc<QueryHash>,
Expand Down Expand Up @@ -94,7 +92,7 @@ where
pub(crate) async fn new(
delegate: T,
schema: Arc<Schema>,
subgraph_schemas: Arc<HashMap<String, Arc<Valid<apollo_compiler::Schema>>>>,
subgraph_schemas: Arc<SubgraphSchemas>,
configuration: &Configuration,
plugins: Plugins,
) -> Result<CachingQueryPlanner<T>, BoxError> {
Expand Down Expand Up @@ -339,9 +337,7 @@ where
}

impl CachingQueryPlanner<QueryPlannerService> {
pub(crate) fn subgraph_schemas(
&self,
) -> Arc<HashMap<String, Arc<Valid<apollo_compiler::Schema>>>> {
pub(crate) fn subgraph_schemas(&self) -> Arc<SubgraphSchemas> {
self.delegate.subgraph_schemas()
}

Expand Down
6 changes: 3 additions & 3 deletions apollo-router/src/query_planner/execution.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::sync::Arc;

use apollo_compiler::validation::Valid;
use futures::future::join_all;
use futures::prelude::*;
use tokio::sync::broadcast;
Expand All @@ -25,6 +24,7 @@ use crate::json_ext::Value;
use crate::json_ext::ValueExt;
use crate::plugins::subscription::SubscriptionConfig;
use crate::query_planner::fetch::FetchNode;
use crate::query_planner::fetch::SubgraphSchemas;
use crate::query_planner::fetch::Variables;
use crate::query_planner::FlattenNode;
use crate::query_planner::Primary;
Expand Down Expand Up @@ -56,7 +56,7 @@ impl QueryPlan {
service_factory: &'a Arc<FetchServiceFactory>,
supergraph_request: &'a Arc<http::Request<Request>>,
schema: &'a Arc<Schema>,
subgraph_schemas: &'a Arc<HashMap<String, Arc<Valid<apollo_compiler::Schema>>>>,
subgraph_schemas: &'a Arc<SubgraphSchemas>,
sender: mpsc::Sender<Response>,
subscription_handle: Option<SubscriptionHandle>,
subscription_config: &'a Option<SubscriptionConfig>,
Expand Down Expand Up @@ -112,7 +112,7 @@ pub(crate) struct ExecutionParameters<'a> {
pub(crate) context: &'a Context,
pub(crate) service_factory: &'a Arc<FetchServiceFactory>,
pub(crate) schema: &'a Arc<Schema>,
pub(crate) subgraph_schemas: &'a Arc<HashMap<String, Arc<Valid<apollo_compiler::Schema>>>>,
pub(crate) subgraph_schemas: &'a Arc<SubgraphSchemas>,
pub(crate) supergraph_request: &'a Arc<http::Request<Request>>,
pub(crate) deferred_fetches: &'a HashMap<String, broadcast::Sender<(Value, Vec<Error>)>>,
pub(crate) query: &'a Arc<Query>,
Expand Down
19 changes: 13 additions & 6 deletions apollo-router/src/query_planner/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;
use std::fmt::Display;
use std::sync::Arc;

use apollo_compiler::ast;
use apollo_compiler::collections::HashMap;
use apollo_compiler::validation::Valid;
use apollo_compiler::ExecutableDocument;
use apollo_compiler::Name;
use indexmap::IndexSet;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -95,7 +96,12 @@ impl From<ast::OperationType> for OperationKind {
}
}

pub(crate) type SubgraphSchemas = HashMap<String, Arc<Valid<apollo_compiler::Schema>>>;
pub(crate) type SubgraphSchemas = HashMap<String, SubgraphSchema>;

pub(crate) struct SubgraphSchema {
pub(crate) schema: Arc<Valid<apollo_compiler::Schema>>,
pub(crate) implementers_map: HashMap<Name, apollo_compiler::schema::Implementers>,
}

/// A fetch node.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
Expand Down Expand Up @@ -411,7 +417,7 @@ impl FetchNode {
pub(crate) fn deferred_fetches(
current_dir: &Path,
id: &Option<String>,
deferred_fetches: &HashMap<String, Sender<(Value, Vec<Error>)>>,
deferred_fetches: &std::collections::HashMap<String, Sender<(Value, Vec<Error>)>>,
value: &Value,
errors: &[Error],
) {
Expand Down Expand Up @@ -575,7 +581,7 @@ impl FetchNode {
subgraph_schemas: &SubgraphSchemas,
) -> Result<(), ValidationErrors> {
let schema = &subgraph_schemas[self.service_name.as_ref()];
self.operation.init_parsed(schema)?;
self.operation.init_parsed(&schema.schema)?;
Ok(())
}

Expand All @@ -585,11 +591,12 @@ impl FetchNode {
supergraph_schema_hash: &str,
) -> Result<(), ValidationErrors> {
let schema = &subgraph_schemas[self.service_name.as_ref()];
let doc = self.operation.init_parsed(schema)?;
let doc = self.operation.init_parsed(&schema.schema)?;

if let Ok(hash) = QueryHashVisitor::hash_query(
schema,
&schema.schema,
supergraph_schema_hash,
&schema.implementers_map,
doc,
self.operation_name.as_deref(),
) {
Expand Down
Loading