Skip to content

Commit

Permalink
Add SupergraphConfig::merge_subgraphs method (#541)
Browse files Browse the repository at this point in the history
Adds a method to merge another `SupergraphConfig`'s subgraphs into
another.
  • Loading branch information
dotdat authored Jul 15, 2024
2 parents 2322112 + 5c5c87e commit 20ab21a
Showing 1 changed file with 81 additions and 1 deletion.
82 changes: 81 additions & 1 deletion apollo-federation-types/src/config/supergraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ impl SupergraphConfig {
pub fn get_federation_version(&self) -> Option<FederationVersion> {
self.federation_version.clone()
}

/// Merges the subgraphs of another [`SupergraphConfig`] into this one
pub fn merge_subgraphs(&mut self, other: &SupergraphConfig) {
for (key, value) in other.subgraphs.iter() {
self.subgraphs.insert(key.to_string(), value.clone());
}
}
}

impl From<Vec<SubgraphDefinition>> for SupergraphConfig {
Expand Down Expand Up @@ -147,13 +154,14 @@ impl IntoIterator for SupergraphConfig {

#[cfg(test)]
mod tests {
use crate::config::FederationVersion;
use crate::config::{FederationVersion, SchemaSource, SubgraphConfig};

use super::SupergraphConfig;

use assert_fs::TempDir;
use camino::Utf8PathBuf;
use semver::Version;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::fs;

Expand Down Expand Up @@ -611,4 +619,76 @@ subgraphs:

assert!(SupergraphConfig::new_from_yaml(raw_good_yaml).is_err())
}

#[test]
fn test_merge_subgraphs() {
let raw_base_config = r#"---
federation_version: 2
subgraphs:
films:
routing_url: https://films.example.com
schema:
file: ./good-films.graphql
people:
routing_url: https://people.example.com
schema:
file: ./good-people.graphql
"#;
let raw_override_config = r#"---
federation_version: 1
subgraphs:
films:
routing_url: https://films.example.com/graphql
schema:
file: ./good-films.graphql
books:
routing_url: https://books.example.com
schema:
file: ./good-books.graphql
"#;
let mut base_config = SupergraphConfig::new_from_yaml(raw_base_config)
.expect("Failed to parse supergraph config");

let override_config = SupergraphConfig::new_from_yaml(raw_override_config)
.expect("Failed to parse supergraph config");

base_config.merge_subgraphs(&override_config);

assert_eq!(
base_config.get_federation_version(),
Some(FederationVersion::LatestFedTwo)
);

let expected_subgraphs = BTreeMap::from([
(
"films".to_string(),
SubgraphConfig {
routing_url: Some("https://films.example.com/graphql".to_string()),
schema: SchemaSource::File {
file: "./good-films.graphql".into(),
},
},
),
(
"books".to_string(),
SubgraphConfig {
routing_url: Some("https://books.example.com".to_string()),
schema: SchemaSource::File {
file: "./good-books.graphql".into(),
},
},
),
(
"people".to_string(),
SubgraphConfig {
routing_url: Some("https://people.example.com".to_string()),
schema: SchemaSource::File {
file: "./good-people.graphql".into(),
},
},
),
]);

assert_eq!(base_config.subgraphs, expected_subgraphs);
}
}

0 comments on commit 20ab21a

Please sign in to comment.