-
Notifications
You must be signed in to change notification settings - Fork 85
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
Changes from all commits
9d9b6d0
e8f58b2
59d6f44
7d888f5
ae69a95
2a277b5
3b2b205
e08de0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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
|
||
} |
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 }, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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