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 to supergraph compose #2001

Merged
merged 8 commits into from
Aug 1, 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
60 changes: 60 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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 @@ -84,6 +85,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 Expand Up @@ -203,6 +205,7 @@ duct = "0.13.7"
git2 = { workspace = true, features = ["https"]}
graphql-schema-diff = "0.2.0"
httpmock = { workspace = true }
indoc = { workspace = true }
mime = "0.3.17"
portpicker = "0.1.1"
predicates = { workspace = true }
Expand Down
3 changes: 3 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 Expand Up @@ -55,3 +57,4 @@ indoc = { workspace = true}
httpmock = { workspace = true }
pretty_assertions = { workspace = true }
strip-ansi-escapes = { workspace = true }
rstest = { workspace = true }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the heart of the change, adding this new query into the rover client

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
}
}
}
}
}
Comment on lines +1 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

does this query support pagination? I'm a hair worried about getting the sdl for, say, hundreds of subgraphs in one go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had a look in studio and I can't see that it does, I think the only way we're going to know though is to try it with a big number of graphs and see how it performs. Do we have the power to point it at Indeed's subgraphs, since all we're doing is composing and we have the schemas I don't see why not right?

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;
121 changes: 121 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,121 @@
use graphql_client::*;

use crate::blocking::StudioClient;
use crate::RoverClientError;

use super::types::*;

#[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;

/// For a given graph return all of its subgraphs as a list
pub fn run(
input: SubgraphFetchAllInput,
client: &StudioClient,
) -> Result<Vec<Subgraph>, RoverClientError> {
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> {
match response_data.variant {
None => Err(RoverClientError::GraphNotFound {
graph_ref: input.graph_ref,
}),
Some(SubgraphFetchAllGraphVariant::GraphVariant(variant)) => variant.subgraphs.map_or_else(
|| {
Err(RoverClientError::ExpectedFederatedGraph {
graph_ref: input.graph_ref,
can_operation_convert: true,
})
},
|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())
},
),
_ => Err(RoverClientError::InvalidGraphRef),
}
}

#[cfg(test)]
mod tests {
use rstest::{fixture, rstest};
use serde_json::json;

use crate::shared::GraphRef;

use super::*;

#[rstest]
fn get_services_from_response_data_works(#[from(mock_input)] input: SubgraphFetchAllInput) {
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 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]);
}

#[rstest]
fn get_services_from_response_data_errs_with_no_variant(mock_input: SubgraphFetchAllInput) {
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());
}

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

SubgraphFetchAllInput { graph_ref }
}
jonathanrainer marked this conversation as resolved.
Show resolved Hide resolved
}
41 changes: 41 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,41 @@
use apollo_federation_types::config::{SchemaSource, SubgraphConfig};
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,
}

impl From<Subgraph> for SubgraphConfig {
fn from(value: Subgraph) -> Self {
Self {
routing_url: value.url,
schema: SchemaSource::Sdl { sdl: value.sdl },
}
}
}
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
6 changes: 4 additions & 2 deletions crates/rover-std/src/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Emoji {
Hourglass,
Listen,
Memo,
Merge,
New,
Note,
Person,
Expand All @@ -30,9 +31,10 @@ impl Emoji {
match self {
Action => "🎬 ",
Compose => "🎶 ",
Hourglass => "⌛ ",
Hourglass => "⌛ ",
Listen => "👂 ",
Memo => "📝 ",
Merge => "⛙ ",
New => "🐤 ",
Note => "🗒️ ",
Person => "🧑 ",
Expand All @@ -42,7 +44,7 @@ impl Emoji {
Sparkle => "✨ ",
Start => "🛫 ",
Stop => "✋ ",
Success => "✅ ",
Success => "✅ ",
Warn => "⚠️ ",
Watch => "👀 ",
Web => "🕸️ ",
Expand Down
Loading
Loading