-
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.
- Loading branch information
1 parent
309a336
commit 0159bf2
Showing
11 changed files
with
232 additions
and
2 deletions.
There are no files selected for viewing
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
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,17 @@ | ||
query FetchSupergraphQuery($variant: String!, $graphId: ID!) { | ||
frontendUrlRoot | ||
service(id: $graphId) { | ||
schema(tag: $variant) { | ||
hash | ||
} | ||
mostRecentCompositionPublish(graphVariant: $variant) { | ||
supergraphSdl | ||
errors { | ||
message | ||
} | ||
} | ||
variants { | ||
name | ||
} | ||
} | ||
} |
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,139 @@ | ||
use crate::blocking::StudioClient; | ||
use crate::RoverClientError; | ||
use graphql_client::*; | ||
|
||
// I'm not sure where this should live long-term | ||
/// this is because of the custom GraphQLDocument scalar in the schema | ||
type GraphQLDocument = String; | ||
|
||
#[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/query/supergraph/fetch.graphql", | ||
schema_path = ".schema/schema.graphql", | ||
response_derives = "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. fetch_supergraph_query | ||
pub struct FetchSupergraphQuery; | ||
|
||
/// The main function to be used from this module. This function fetches a | ||
/// core schema from apollo studio | ||
pub fn run( | ||
variables: fetch_supergraph_query::Variables, | ||
client: &StudioClient, | ||
) -> Result<String, RoverClientError> { | ||
let graph = variables.graph_id.clone(); | ||
let invalid_variant = variables.variant.clone(); | ||
let response_data = client.post::<FetchSupergraphQuery>(variables)?; | ||
get_supergraph_sdl_from_response_data(response_data, graph, invalid_variant) | ||
} | ||
|
||
fn get_supergraph_sdl_from_response_data( | ||
response_data: fetch_supergraph_query::ResponseData, | ||
graph: String, | ||
invalid_variant: String, | ||
) -> Result<String, RoverClientError> { | ||
let service_data = match response_data.service { | ||
Some(data) => Ok(data), | ||
None => Err(RoverClientError::NoService { | ||
graph: graph.clone(), | ||
}), | ||
}?; | ||
|
||
let mut valid_variants = Vec::new(); | ||
|
||
for variant in service_data.variants { | ||
valid_variants.push(variant.name) | ||
} | ||
|
||
if service_data.schema.is_none() { | ||
return Err(RoverClientError::NoSchemaForVariant { | ||
graph, | ||
invalid_variant, | ||
valid_variants, | ||
frontend_url_root: response_data.frontend_url_root, | ||
}); | ||
} | ||
|
||
if let Some(composition_publish) = service_data.most_recent_composition_publish { | ||
if let Some(supergraph_sdl) = composition_publish.supergraph_sdl { | ||
Ok(supergraph_sdl) | ||
} else { | ||
let mut error_msg = "".to_string(); | ||
// TODO: make this less hacky and let folks know these are composition errors. | ||
for error in composition_publish.errors { | ||
error_msg.push_str(&error.message); | ||
error_msg.push_str("\n") | ||
} | ||
Err(RoverClientError::AdhocError { msg: error_msg }) | ||
} | ||
} else { | ||
Err(RoverClientError::NoCompositionPublishes { graph }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use serde_json::json; | ||
#[test] | ||
fn get_supergraph_sdl_from_response_data_works() { | ||
let json_response = json!({ | ||
"frontendUrlRoot": "https://studio.apollographql.com", | ||
"service": { | ||
"schema": { | ||
"hash": "123" | ||
}, | ||
"mostRecentCompositionPublish": { | ||
"supergraphSdl": "type Query { hello: String }", | ||
"errors": [] | ||
}, | ||
"variants": [] | ||
} | ||
}); | ||
let data: fetch_supergraph_query::ResponseData = | ||
serde_json::from_value(json_response).unwrap(); | ||
let (graph, invalid_variant) = mock_vars(); | ||
let output = get_supergraph_sdl_from_response_data(data, graph, invalid_variant); | ||
|
||
assert!(output.is_ok()); | ||
assert_eq!(output.unwrap(), "type Query { hello: String }".to_string()); | ||
} | ||
|
||
#[test] | ||
fn get_schema_from_response_data_errs_on_no_service() { | ||
let json_response = | ||
json!({ "service": null, "frontendUrlRoot": "https://studio.apollographql.com" }); | ||
let data: fetch_supergraph_query::ResponseData = | ||
serde_json::from_value(json_response).unwrap(); | ||
let (graph, invalid_variant) = mock_vars(); | ||
let output = get_supergraph_sdl_from_response_data(data, graph, invalid_variant); | ||
|
||
assert!(output.is_err()); | ||
} | ||
|
||
#[test] | ||
fn get_schema_from_response_data_errs_on_no_schema() { | ||
let json_response = json!({ | ||
"frontendUrlRoot": "https://studio.apollographql.com/", | ||
"service": { | ||
"schema": null, | ||
"variants": [], | ||
}, | ||
}); | ||
let data: fetch_supergraph_query::ResponseData = | ||
serde_json::from_value(json_response).unwrap(); | ||
let (graph, invalid_variant) = mock_vars(); | ||
let output = get_supergraph_sdl_from_response_data(data, graph, invalid_variant); | ||
|
||
assert!(output.is_err()); | ||
} | ||
|
||
fn mock_vars() -> (String, String) { | ||
("mygraph".to_string(), "current".to_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/// "supergraph fetch" | ||
pub mod fetch; |
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
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,45 @@ | ||
use crate::utils::client::StudioClientConfig; | ||
use crate::utils::parsers::{parse_graph_ref, GraphRef}; | ||
use crate::{command::RoverStdout, Result}; | ||
|
||
use rover_client::query::supergraph::fetch; | ||
|
||
use ansi_term::Colour::{Cyan, Yellow}; | ||
use serde::Serialize; | ||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, Serialize, StructOpt)] | ||
pub struct Fetch { | ||
/// <NAME>@<VARIANT> of graph in Apollo Studio to fetch from. | ||
/// @<VARIANT> may be left off, defaulting to @current | ||
#[structopt(name = "GRAPH_REF", parse(try_from_str = parse_graph_ref))] | ||
#[serde(skip_serializing)] | ||
graph: GraphRef, | ||
|
||
/// Name of configuration profile to use | ||
#[structopt(long = "profile", default_value = "default")] | ||
#[serde(skip_serializing)] | ||
profile_name: String, | ||
} | ||
|
||
impl Fetch { | ||
pub fn run(&self, client_config: StudioClientConfig) -> Result<RoverStdout> { | ||
let client = client_config.get_client(&self.profile_name)?; | ||
let graph_ref = self.graph.to_string(); | ||
eprintln!( | ||
"Fetching supergraph SDL from {} using credentials from the {} profile.", | ||
Cyan.normal().paint(&graph_ref), | ||
Yellow.normal().paint(&self.profile_name) | ||
); | ||
|
||
let supergraph_sdl = fetch::run( | ||
fetch::fetch_supergraph_query::Variables { | ||
graph_id: self.graph.name.clone(), | ||
variant: self.graph.variant.clone(), | ||
}, | ||
&client, | ||
)?; | ||
|
||
Ok(RoverStdout::SupergraphSdl(supergraph_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
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