-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rather than fetching each subgraph one at a time, make a single GraphQL request to get them all at once. This combines the work from #1985
- Loading branch information
1 parent
8650523
commit a1a37b3
Showing
9 changed files
with
532 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
crates/rover-client/src/operations/subgraph/fetch_all/fetch_all_query.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
136 changes: 136 additions & 0 deletions
136
crates/rover-client/src/operations/subgraph/fetch_all/runner.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use graphql_client::*; | ||
|
||
use crate::blocking::StudioClient; | ||
use crate::operations::config::is_federated::{self, IsFederatedInput}; | ||
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> { | ||
// This response is used to check whether 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 serde_json::json; | ||
|
||
use crate::shared::GraphRef; | ||
|
||
use super::*; | ||
|
||
#[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
31
crates/rover-client/src/operations/subgraph/fetch_all/types.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.