Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

composition: check env var when getting fed version #2184

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/command/dev/protocol/leader.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::str::FromStr;
use std::{
collections::{hash_map::Entry::Vacant, HashMap},
fmt::Debug,
io::BufReader,
net::TcpListener,
str::FromStr,
};

use anyhow::{anyhow, Context};
Expand Down
73 changes: 67 additions & 6 deletions src/composition/supergraph/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::sync::Arc;
use std::{str::FromStr, sync::Arc};

use apollo_federation_types::config::{ConfigError, FederationVersion, SupergraphConfig};
use camino::Utf8PathBuf;
use derive_getters::Getters;
use rover_client::shared::GraphRef;
use rover_std::{Fs, RoverStdError};
use rover_std::{warnln, Fs, RoverStdError};
use tokio::sync::{Mutex, MutexGuard};

use crate::{
Expand Down Expand Up @@ -298,10 +298,33 @@ impl FinalSupergraphConfig {
Ok(())
}

pub fn federation_version(&self) -> FederationVersion {
self.config
.get_federation_version()
.unwrap_or(FederationVersion::LatestFedTwo)
/// Calculates what the correct version of Federation should be, based on the
/// value on the given environment variable or the supergraph config.
///
/// The order of precedence is:
/// Environment Variable -> Supergraph Schema -> Default (Latest)
pub fn federation_version(&self, env_var: Option<String>) -> FederationVersion {
let env_var_version = if let Some(version) = env_var {
match FederationVersion::from_str(&version) {
Ok(v) => Some(v),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this from here, but I removed the exact version constraint.

Err(e) => {
warnln!(
"could not parse federation version from environment variable: {:?}",
e
);
None
}
}
} else {
None
};

env_var_version.unwrap_or_else(|| {
self.config.get_federation_version().unwrap_or_else(|| {
warnln!("federation version not found in supergraph schema, defaulting to latest version");
FederationVersion::LatestFedTwo
})
})
}
}

Expand All @@ -310,3 +333,41 @@ impl From<FinalSupergraphConfig> for SupergraphConfig {
value.config
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;

use apollo_federation_types::config::{FederationVersion, SupergraphConfig};
use rstest::rstest;

use super::FinalSupergraphConfig;

#[rstest]
#[case::env_var_set(Some("2.9".to_string()), None, FederationVersion::LatestFedTwo)]
#[case::env_var_unset_config_set(
None,
Some(FederationVersion::LatestFedTwo),
FederationVersion::LatestFedTwo
)]
#[case::env_var_unset_config_unset(None, None, FederationVersion::LatestFedTwo)]
#[case::env_var_set_non_default(Some("1".to_string()), None, FederationVersion::LatestFedOne)]
#[case::config_set_non_default(
None,
Some(FederationVersion::LatestFedOne),
FederationVersion::LatestFedOne
)]
fn test_final_supergraph_config_federation_version(
#[case] env_var: Option<String>,
#[case] fed_version: Option<FederationVersion>,
#[case] expected: FederationVersion,
) {
let supergraph_config = SupergraphConfig::new(BTreeMap::new(), fed_version.clone());

let final_config =
FinalSupergraphConfig::new(None, "/path/to/file".into(), supergraph_config);
let fed_version = final_config.federation_version(env_var);

assert_eq!(expected, fed_version);
}
}
Loading