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

Optimize rover dev --graph-ref by fetching all subgraph data at once #1985

Closed
wants to merge 3 commits into from
Closed
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
62 changes: 60 additions & 2 deletions Cargo.lock

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

4 changes: 3 additions & 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 All @@ -71,6 +71,7 @@ backtrace = "0.3"
backoff = "0.4"
base64 = "0.22"
billboard = "0.2"
buildstructor = "0.5.4"
cargo_metadata = "0.18"
calm_io = "0.1"
camino = "1"
Expand All @@ -80,6 +81,7 @@ ci_info = "0.14"
console = "0.15"
crossbeam-channel = "0.5"
ctrlc = "3"
derive-getters = "0.4.0"
dialoguer = "0.11"
directories-next = "2.0"
flate2 = "1"
Expand Down
2 changes: 2 additions & 0 deletions crates/rover-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ apollo-federation-types = { workspace = true }
apollo-parser = { workspace = true }
apollo-encoder = { workspace = true }
backoff = { workspace = true }
buildstructor = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
derive-getters = { workspace = true }
git-url-parse = { workspace = true }
git2 = { workspace = true, features = [
"vendored-openssl",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
query SubgraphFetchAllQuery($graph_ref: ID!) {
variant(ref: $graph_ref) {
__typename
... on GraphVariant {
subgraphs {
name
url
activePartialSchema {
sdl
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod runner;
mod types;

pub use runner::run;
pub use types::SubgraphFetchAllInput;
133 changes: 133 additions & 0 deletions crates/rover-client/src/operations/subgraph/fetch_all/runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use super::types::*;
use crate::blocking::StudioClient;
use crate::operations::config::is_federated::{self, IsFederatedInput};
use crate::RoverClientError;

use graphql_client::*;

#[derive(GraphQLQuery)]
// The paths are relative to the directory where your `Cargo.toml` is located.
// Both json and the GraphQL schema language are supported as sources for the schema
#[graphql(
query_path = "src/operations/subgraph/fetch_all/fetch_all_query.graphql",
schema_path = ".schema/schema.graphql",
response_derives = "Eq, PartialEq, Debug, Serialize, Deserialize",
deprecated = "warn"
)]
/// This struct is used to generate the module containing `Variables` and
/// `ResponseData` structs.
/// Snake case of this name is the mod name. i.e. subgraph_fetch_all_query
pub(crate) struct SubgraphFetchAllQuery;

/// Fetches a schema from apollo studio and returns its SDL (String)
pub fn run(
input: SubgraphFetchAllInput,
client: &StudioClient,
) -> Result<Vec<Subgraph>, RoverClientError> {
// This response is used to check whether or not the current graph is federated.
let is_federated = is_federated::run(
IsFederatedInput {
graph_ref: input.graph_ref.clone(),
},
client,
)?;
if !is_federated {
return Err(RoverClientError::ExpectedFederatedGraph {
graph_ref: input.graph_ref,
can_operation_convert: false,
});
}
let variables = input.clone().into();
let response_data = client.post::<SubgraphFetchAllQuery>(variables)?;
get_subgraphs_from_response_data(input, response_data)
}

fn get_subgraphs_from_response_data(
input: SubgraphFetchAllInput,
response_data: SubgraphFetchAllResponseData,
) -> Result<Vec<Subgraph>, RoverClientError> {
if let Some(maybe_variant) = response_data.variant {
match maybe_variant {
SubgraphFetchAllGraphVariant::GraphVariant(variant) => {
if let Some(subgraphs) = variant.subgraphs {
Ok(subgraphs
.into_iter()
.map(|subgraph| {
Subgraph::builder()
.name(subgraph.name.clone())
.and_url(subgraph.url)
.sdl(subgraph.active_partial_schema.sdl)
.build()
})
.collect())
} else {
Err(RoverClientError::ExpectedFederatedGraph {
graph_ref: input.graph_ref,
can_operation_convert: true,
})
}
}
_ => Err(RoverClientError::InvalidGraphRef),
}
} else {
Err(RoverClientError::GraphNotFound {
graph_ref: input.graph_ref,
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::shared::GraphRef;
use serde_json::json;

#[test]
fn get_services_from_response_data_works() {
let sdl = "extend type User @key(fields: \"id\") {\n id: ID! @external\n age: Int\n}\n"
.to_string();
let url = "http://my.subgraph.com".to_string();
let input = mock_input();
let json_response = json!({
"variant": {
"__typename": "GraphVariant",
"subgraphs": [
{
"name": "accounts",
"url": &url,
"activePartialSchema": {
"sdl": &sdl
}
},
]
}
});
let data: SubgraphFetchAllResponseData = serde_json::from_value(json_response).unwrap();
let expected_subgraph = Subgraph::builder()
.url(url)
.sdl(sdl)
.name("accounts".to_string())
.build();
let output = get_subgraphs_from_response_data(input, data);

assert!(output.is_ok());
assert_eq!(output.unwrap(), vec![expected_subgraph]);
}

#[test]
fn get_services_from_response_data_errs_with_no_variant() {
let json_response = json!({ "variant": null });
let data: SubgraphFetchAllResponseData = serde_json::from_value(json_response).unwrap();
let output = get_subgraphs_from_response_data(mock_input(), data);
assert!(output.is_err());
}

fn mock_input() -> SubgraphFetchAllInput {
let graph_ref = GraphRef {
name: "mygraph".to_string(),
variant: "current".to_string(),
};

SubgraphFetchAllInput { graph_ref }
}
}
31 changes: 31 additions & 0 deletions crates/rover-client/src/operations/subgraph/fetch_all/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use buildstructor::Builder;
use derive_getters::Getters;

use crate::shared::GraphRef;

use super::runner::subgraph_fetch_all_query;

pub(crate) type SubgraphFetchAllResponseData = subgraph_fetch_all_query::ResponseData;
pub(crate) type SubgraphFetchAllGraphVariant =
subgraph_fetch_all_query::SubgraphFetchAllQueryVariant;
pub(crate) type QueryVariables = subgraph_fetch_all_query::Variables;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SubgraphFetchAllInput {
pub graph_ref: GraphRef,
}

impl From<SubgraphFetchAllInput> for QueryVariables {
fn from(input: SubgraphFetchAllInput) -> Self {
Self {
graph_ref: input.graph_ref.to_string(),
}
}
}

#[derive(Clone, Builder, Debug, Eq, Getters, PartialEq)]
pub struct Subgraph {
name: String,
url: Option<String>,
sdl: String,
}
3 changes: 3 additions & 0 deletions crates/rover-client/src/operations/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub mod check;
/// "subgraph fetch" command execution
pub mod fetch;

/// "subgraph fetch_all" command execution
pub mod fetch_all;

/// "subgraph publish" command execution
pub mod publish;

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
Loading
Loading