From a08d9e17f991ef7232cb77abce426c305486a03f Mon Sep 17 00:00:00 2001 From: jonathanrainer Date: Wed, 4 Sep 2024 13:56:40 +0100 Subject: [PATCH 1/3] Add new FromIterator method --- apollo-federation-types/src/config/supergraph.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/apollo-federation-types/src/config/supergraph.rs b/apollo-federation-types/src/config/supergraph.rs index dd309149..b45a55bf 100644 --- a/apollo-federation-types/src/config/supergraph.rs +++ b/apollo-federation-types/src/config/supergraph.rs @@ -164,6 +164,19 @@ impl IntoIterator for SupergraphConfig { } } +impl FromIterator<(String, SubgraphConfig)> for SupergraphConfig { + fn from_iter>(iter: T) -> Self { + let mut subgraphs = BTreeMap::new(); + for (name, subgraph_config) in iter { + subgraphs.insert(name, subgraph_config); + } + Self { + subgraphs, + federation_version: None, + } + } +} + #[cfg(test)] mod tests { use crate::config::{FederationVersion, SchemaSource, SubgraphConfig}; From 4400c86a0594a19c9d15fc594d09812343590946 Mon Sep 17 00:00:00 2001 From: jonathanrainer Date: Thu, 12 Sep 2024 08:34:35 +0100 Subject: [PATCH 2/3] Adds linting changes --- apollo-federation-types/src/config/supergraph.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apollo-federation-types/src/config/supergraph.rs b/apollo-federation-types/src/config/supergraph.rs index b45a55bf..808786af 100644 --- a/apollo-federation-types/src/config/supergraph.rs +++ b/apollo-federation-types/src/config/supergraph.rs @@ -165,7 +165,7 @@ impl IntoIterator for SupergraphConfig { } impl FromIterator<(String, SubgraphConfig)> for SupergraphConfig { - fn from_iter>(iter: T) -> Self { + fn from_iter>(iter: T) -> Self { let mut subgraphs = BTreeMap::new(); for (name, subgraph_config) in iter { subgraphs.insert(name, subgraph_config); From 3c381c1bae30925c4674da6251ee222a84ed0ad3 Mon Sep 17 00:00:00 2001 From: Dan Bond Date: Wed, 18 Sep 2024 11:41:57 +0100 Subject: [PATCH 3/3] add SupergraphConfig FromIterator test --- .../src/config/supergraph.rs | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/apollo-federation-types/src/config/supergraph.rs b/apollo-federation-types/src/config/supergraph.rs index 808786af..28bbbd21 100644 --- a/apollo-federation-types/src/config/supergraph.rs +++ b/apollo-federation-types/src/config/supergraph.rs @@ -1,10 +1,12 @@ -use crate::config::{ConfigError, ConfigResult, FederationVersion, SubgraphConfig}; +use std::{collections::BTreeMap, fs}; use camino::Utf8PathBuf; use serde::{Deserialize, Serialize}; -use crate::javascript::SubgraphDefinition; -use std::{collections::BTreeMap, fs}; +use crate::{ + config::{ConfigError, ConfigResult, FederationVersion, SubgraphConfig}, + javascript::SubgraphDefinition, +}; /// The configuration for a single supergraph /// composed of multiple subgraphs. @@ -166,12 +168,8 @@ impl IntoIterator for SupergraphConfig { impl FromIterator<(String, SubgraphConfig)> for SupergraphConfig { fn from_iter>(iter: T) -> Self { - let mut subgraphs = BTreeMap::new(); - for (name, subgraph_config) in iter { - subgraphs.insert(name, subgraph_config); - } Self { - subgraphs, + subgraphs: iter.into_iter().collect::>(), federation_version: None, } } @@ -179,16 +177,15 @@ impl FromIterator<(String, SubgraphConfig)> for SupergraphConfig { #[cfg(test)] mod tests { - use crate::config::{FederationVersion, SchemaSource, SubgraphConfig}; - - use super::SupergraphConfig; + use std::{collections::BTreeMap, convert::TryFrom, fs}; use assert_fs::TempDir; use camino::Utf8PathBuf; use semver::Version; - use std::collections::BTreeMap; - use std::convert::TryFrom; - use std::fs; + + use crate::config::{FederationVersion, SchemaSource, SubgraphConfig}; + + use super::SupergraphConfig; #[test] fn it_can_parse_valid_config_without_version() { @@ -732,4 +729,23 @@ subgraphs: assert_eq!(base_config.subgraphs, expected_subgraphs); } + + #[test] + fn test_supergraph_config_from_iterator() { + let iter = [( + "subgraph_tmp".to_string(), + SubgraphConfig { + routing_url: Some("url".to_string()), + schema: SchemaSource::Sdl { + sdl: "subgraph_tmp".to_string(), + }, + }, + )] + .into_iter(); + + let s: SupergraphConfig = iter.collect(); + assert_eq!(None, s.get_federation_version()); + assert!(s.get_subgraph_definitions().is_ok()); + assert_eq!(1, s.get_subgraph_definitions().unwrap().len()); + } }