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

Add --graph-ref flag to rover dev #1984

Merged
merged 3 commits into from
Jul 16, 2024
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ apollo-parser = "0.7"
apollo-encoder = "0.8"

# https://github.com/apollographql/federation-rs
apollo-federation-types = "0.13.0"
apollo-federation-types = "0.13.1"

# crates.io dependencies
ariadne = "0.4"
Expand Down
32 changes: 31 additions & 1 deletion src/command/dev/do_dev.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Context};
use apollo_federation_types::config::FederationVersion;
use camino::Utf8PathBuf;
use crossbeam_channel::bounded as sync_channel;

Expand All @@ -10,6 +11,7 @@ use crate::utils::client::StudioClientConfig;
use crate::{RoverError, RoverOutput, RoverResult};

use super::protocol::{FollowerChannel, FollowerMessenger, LeaderChannel, LeaderSession};
use super::remote_subgraphs::RemoteSubgraphs;
use super::router::RouterConfigHandler;
use super::Dev;

Expand All @@ -34,7 +36,22 @@ impl Dev {
let leader_channel = LeaderChannel::new();
let follower_channel = FollowerChannel::new();

// Read in Supergraph Config
// Read in Remote subgraphs
let remote_subgraphs = match &self.opts.supergraph_opts.graph_ref {
Some(graph_ref) => Some(RemoteSubgraphs::fetch(
&client_config.get_authenticated_client(&self.opts.plugin_opts.profile)?,
&self
.opts
.supergraph_opts
.federation_version
.clone()
.unwrap_or(FederationVersion::LatestFedTwo),
graph_ref,
)?),
None => None,
};

// Read in Local Supergraph Config
let supergraph_config =
if let Some(config_path) = &self.opts.supergraph_opts.supergraph_config_path {
let config_content = Fs::read_file(config_path)?;
Expand All @@ -43,6 +60,19 @@ impl Dev {
None
};

// Merge Remote and Local Supergraph Configs
let supergraph_config = match remote_subgraphs {
Some(remote_subgraphs) => match supergraph_config {
Some(supergraph_config) => {
let mut merged_supergraph_config = remote_subgraphs.inner().clone();
merged_supergraph_config.merge_subgraphs(&supergraph_config);
Some(merged_supergraph_config)
}
None => Some(remote_subgraphs.inner().clone()),
},
None => supergraph_config,
};

// Build a Rayon Thread pool
let tp = rayon::ThreadPoolBuilder::new()
.num_threads(1)
Expand Down
45 changes: 32 additions & 13 deletions src/command/dev/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
use std::net::IpAddr;

use apollo_federation_types::config::FederationVersion;
use camino::Utf8PathBuf;
use clap::Parser;
use rover_client::shared::GraphRef;
use serde::Serialize;

use crate::options::{OptionalSubgraphOpts, PluginOpts};

#[cfg(feature = "composition-js")]
mod compose;

#[cfg(feature = "composition-js")]
mod introspect;
mod do_dev;

#[cfg(feature = "composition-js")]
mod router;
mod introspect;

#[cfg(feature = "composition-js")]
mod schema;
mod protocol;

#[cfg(feature = "composition-js")]
mod protocol;
mod remote_subgraphs;

#[cfg(feature = "composition-js")]
mod netstat;
mod router;

#[cfg(feature = "composition-js")]
mod watcher;
mod schema;

#[cfg(feature = "composition-js")]
mod do_dev;
mod netstat;

#[cfg(not(feature = "composition-js"))]
mod no_dev;

use crate::options::{OptionalSubgraphOpts, PluginOpts};
use std::net::IpAddr;

use camino::Utf8PathBuf;
use clap::Parser;
use serde::Serialize;
#[cfg(feature = "composition-js")]
mod watcher;

#[derive(Debug, Serialize, Parser)]
pub struct Dev {
Expand Down Expand Up @@ -86,6 +92,19 @@ pub struct SupergraphOpts {
conflicts_with_all = ["subgraph_name", "subgraph_url", "subgraph_schema_path"]
)]
supergraph_config_path: Option<Utf8PathBuf>,

/// A [`GraphRef`] that is accessible in Apollo Studio.
/// This is used to initialize your supergraph with the values contained in this variant.
///
/// This is analogous to providing a supergraph.yaml file with references to your graph variant in studio.
///
/// If used in conjunction with `--supergraph-config`, the values presented in the supergraph.yaml will take precedence over these values.
#[arg(long = "graph-ref")]
graph_ref: Option<GraphRef>,

/// The version of Apollo Federation to use for composition
#[arg(long = "federation-version")]
federation_version: Option<FederationVersion>,
}

lazy_static::lazy_static! {
Expand Down
3 changes: 2 additions & 1 deletion src/command/dev/protocol/leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl LeaderSession {
let mut router_runner = RouterRunner::new(
router_config_handler.get_supergraph_schema_path(),
router_config_handler.get_router_config_path(),
plugin_opts,
plugin_opts.clone(),
router_socket_addr,
router_config_handler.get_router_listen_path(),
override_install_path,
Expand Down Expand Up @@ -403,6 +403,7 @@ impl LeaderSession {
.map(|((name, url), sdl)| SubgraphDefinition::new(name, url.to_string(), sdl))
.collect::<Vec<SubgraphDefinition>>()
.into();

supergraph_config.set_federation_version(self.federation_version.clone());
supergraph_config
}
Expand Down
55 changes: 55 additions & 0 deletions src/command/dev/remote_subgraphs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use apollo_federation_types::config::{
FederationVersion, SchemaSource, SubgraphConfig, SupergraphConfig,
};
use rover_client::{
blocking::StudioClient,
operations::subgraph::{self, list::SubgraphListInput},
shared::GraphRef,
};

use crate::RoverResult;

/// Nominal type that captures the behavior of collecting remote subgraphs into a
/// [`SupergraphConfig`] representation
#[derive(Clone, Debug)]
pub struct RemoteSubgraphs(SupergraphConfig);

impl RemoteSubgraphs {
/// Fetches [`RemoteSubgraphs`] from Studio
pub fn fetch(
client: &StudioClient,
federation_version: &FederationVersion,
graph_ref: &GraphRef,
) -> RoverResult<RemoteSubgraphs> {
let subgraphs = subgraph::list::run(
SubgraphListInput {
graph_ref: graph_ref.clone(),
},
client,
)?;
let subgraphs = subgraphs
.subgraphs
.iter()
.map(|subgraph| {
(
subgraph.name.clone(),
SubgraphConfig {
routing_url: subgraph.url.clone(),
schema: SchemaSource::Subgraph {
graphref: graph_ref.clone().to_string(),
subgraph: subgraph.name.clone(),
},
},
)
})
.collect();
let supergraph_config = SupergraphConfig::new(subgraphs, Some(federation_version.clone()));
let remote_subgraphs = RemoteSubgraphs(supergraph_config);
Ok(remote_subgraphs)
}

/// Provides a reference to the inner value of this representation
pub fn inner(&self) -> &SupergraphConfig {
&self.0
}
}
Loading