Skip to content

Commit

Permalink
Add new FromIterator method (#579)
Browse files Browse the repository at this point in the history
Adds a simple method in order to construct a SuperGraph config out of an
Iterator of String and Subgraph Configs. Essentially the reverse of the
IntoIterator method implemented above.
  • Loading branch information
loshz authored Sep 18, 2024
2 parents f5d6295 + 3c381c1 commit d304b54
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions apollo-federation-types/src/config/supergraph.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -164,18 +166,26 @@ impl IntoIterator for SupergraphConfig {
}
}

impl FromIterator<(String, SubgraphConfig)> for SupergraphConfig {
fn from_iter<T: IntoIterator<Item = (String, SubgraphConfig)>>(iter: T) -> Self {
Self {
subgraphs: iter.into_iter().collect::<BTreeMap<_, _>>(),
federation_version: None,
}
}
}

#[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() {
Expand Down Expand Up @@ -719,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());
}
}

0 comments on commit d304b54

Please sign in to comment.