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

chore: refactor the rest of rover-client #675

Merged
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ git-url-parse = "0.3.1"
git2 = { version = "0.13.20", default-features = false, features = ["vendored-openssl"] }
harmonizer = { version = "0.26.0", optional = true }
heck = "0.3.3"
humantime = "2.1.0"
opener = "0.5.0"
os_info = "3.0"
prettytable-rs = "0.8.0"
Expand Down
1 change: 1 addition & 0 deletions crates/rover-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ git-url-parse = "0.3.1"
git2 = { version = "0.13.20", default-features = false, features = ["vendored-openssl"] }
graphql_client = "0.9"
http = "0.2"
humantime = "2.1.0"
reqwest = { version = "0.11", default-features = false, features = ["blocking", "brotli", "gzip", "json", "native-tls-vendored"] }
regex = "1"
sdl-encoder = {path = "../sdl-encoder"}
Expand Down
37 changes: 22 additions & 15 deletions crates/rover-client/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use reqwest::Url;
use thiserror::Error;

use crate::shared::{CheckResponse, CompositionError};
use crate::shared::{CheckResponse, CompositionError, GraphRef};

/// RoverClientError represents all possible failures that can occur during a client request.
#[derive(Error, Debug)]
Expand Down Expand Up @@ -60,14 +60,11 @@ pub enum RoverClientError {

/// The Studio API could not find a variant for a graph
#[error(
"The graph registry does not contain variant \"{invalid_variant}\" for graph \"{graph}\""
"The graph registry does not contain variant \"{}\" for graph \"{}\"", graph_ref.variant, graph_ref.name
)]
NoSchemaForVariant {
/// The name of the graph.
graph: String,

/// The non-existent variant.
invalid_variant: String,
/// The graph ref.
graph_ref: GraphRef,

/// Valid variants.
valid_variants: Vec<String>,
Expand Down Expand Up @@ -95,20 +92,22 @@ pub enum RoverClientError {
/// when someone provides a bad graph/variant combination or isn't
/// validated properly, we don't know which reason is at fault for data.service
/// being empty, so this error tells them to check both.
#[error("Could not find graph with name \"{graph}\"")]
NoService { graph: String },
#[error("Could not find graph with name \"{graph_ref}\"")]
GraphNotFound { graph_ref: GraphRef },

/// if someone attempts to get a core schema from a supergraph that has
/// no composition results we return this error.
#[error("No supergraph SDL exists for \"{graph}\" because its subgraphs failed to compose.")]
#[error(
"No supergraph SDL exists for \"{graph_ref}\" because its subgraphs failed to compose."
)]
NoCompositionPublishes {
graph: String,
composition_errors: Vec<String>,
graph_ref: GraphRef,
composition_errors: Vec<CompositionError>,
},

#[error("{}", subgraph_composition_error_msg(.composition_errors))]
SubgraphCompositionErrors {
graph_name: String,
graph_ref: GraphRef,
composition_errors: Vec<CompositionError>,
},

Expand All @@ -122,16 +121,24 @@ pub enum RoverClientError {
/// `can_operation_convert` is only set to true when a non-federated graph
/// was encountered during an operation that could potentially convert a non-federated graph
/// to a federated graph.
#[error("The graph `{graph}` is a non-federated graph. This operation is only possible for federated graphs.")]
#[error("The graph `{graph_ref}` is a non-federated graph. This operation is only possible for federated graphs.")]
ExpectedFederatedGraph {
graph: String,
graph_ref: GraphRef,
can_operation_convert: bool,
},

/// The API returned an invalid ChangeSeverity value
#[error("Invalid ChangeSeverity.")]
InvalidSeverity,

/// The user supplied an invalid validation period
#[error("You can only specify a duration as granular as seconds.")]
ValidationPeriodTooGranular,

/// The user supplied an invalid validation period duration
#[error(transparent)]
InvalidValidationPeriodDuration(#[from] humantime::DurationError),

/// While checking the proposed schema, we encountered changes that would break existing operations
// we nest the CheckResponse here because we want to print the entire response even
// if there were failures
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query IsFederatedGraph($graph_id: ID!, $variant: String!) {
service(id: $graphId) {
implementingServices(graphVariant: $variant) {
__typename
}
}
}
5 changes: 5 additions & 0 deletions crates/rover-client/src/operations/config/is_federated/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod runner;
mod types;

pub(crate) use runner::run;
pub(crate) use types::IsFederatedInput;
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// PublishPartialSchemaMutation
use crate::blocking::StudioClient;
use crate::operations::config::is_federated::IsFederatedInput;
use crate::shared::GraphRef;
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/config/is_federated.graphql",
query_path = "src/operations/config/is_federated/is_federated_query.graphql",
schema_path = ".schema/schema.graphql",
response_derives = "PartialEq, Debug, Serialize, Deserialize",
deprecated = "warn"
Expand All @@ -18,21 +20,23 @@ use graphql_client::*;
pub(crate) struct IsFederatedGraph;

pub(crate) fn run(
variables: is_federated_graph::Variables,
input: IsFederatedInput,
client: &StudioClient,
) -> Result<bool, RoverClientError> {
let graph = variables.graph_id.clone();
let data = client.post::<IsFederatedGraph>(variables)?;
build_response(data, graph)
let graph_ref = input.graph_ref.clone();
let data = client.post::<IsFederatedGraph>(input.into())?;
build_response(data, graph_ref)
}

type ImplementingServices = is_federated_graph::IsFederatedGraphServiceImplementingServices;

fn build_response(
data: is_federated_graph::ResponseData,
graph: String,
graph_ref: GraphRef,
) -> Result<bool, RoverClientError> {
let service = data.service.ok_or(RoverClientError::NoService { graph })?;
let service = data
.service
.ok_or(RoverClientError::GraphNotFound { graph_ref })?;
Ok(match service.implementing_services {
Some(typename) => match typename {
ImplementingServices::FederatedImplementingServices => true,
Expand Down
18 changes: 18 additions & 0 deletions crates/rover-client/src/operations/config/is_federated/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::operations::config::is_federated::runner::is_federated_graph;
use crate::shared::GraphRef;

type QueryVariables = is_federated_graph::Variables;

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

impl From<IsFederatedInput> for QueryVariables {
fn from(input: IsFederatedInput) -> Self {
Self {
graph_id: input.graph_ref.name,
variant: input.graph_ref.variant,
}
}
}
12 changes: 7 additions & 5 deletions crates/rover-client/src/operations/graph/check/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::blocking::StudioClient;
use crate::operations::graph::check::types::{
GraphCheckInput, MutationChangeSeverity, MutationResponseData,
};
use crate::shared::CheckResponse;
use crate::shared::{CheckResponse, GraphRef};
use crate::RoverClientError;

use graphql_client::*;
Expand All @@ -29,16 +29,18 @@ pub fn run(
input: GraphCheckInput,
client: &StudioClient,
) -> Result<CheckResponse, RoverClientError> {
let graph = input.graph_ref.name.clone();
let graph_ref = input.graph_ref.clone();
let data = client.post::<GraphCheckMutation>(input.into())?;
get_check_response_from_data(data, graph)
get_check_response_from_data(data, graph_ref)
}

fn get_check_response_from_data(
data: MutationResponseData,
graph: String,
graph_ref: GraphRef,
) -> Result<CheckResponse, RoverClientError> {
let service = data.service.ok_or(RoverClientError::NoService { graph })?;
let service = data
.service
.ok_or(RoverClientError::GraphNotFound { graph_ref })?;
let target_url = service.check_schema.target_url;

let diff_to_previous = service.check_schema.diff_to_previous;
Expand Down
11 changes: 9 additions & 2 deletions crates/rover-client/src/operations/graph/check/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ impl From<CheckConfig> for MutationConfig {
Self {
query_count_threshold: input.query_count_threshold,
query_count_threshold_percentage: input.query_count_threshold_percentage,
from: input.validation_period_from,
to: input.validation_period_to,
from: Some(
input
.validation_period
.clone()
.unwrap_or_default()
.from
.to_string(),
),
to: Some(input.validation_period.unwrap_or_default().to.to_string()),
// we don't support configuring these, but we can't leave them out
excluded_clients: None,
ignored_operations: None,
Expand Down
11 changes: 0 additions & 11 deletions crates/rover-client/src/operations/graph/fetch.graphql

This file was deleted.

11 changes: 11 additions & 0 deletions crates/rover-client/src/operations/graph/fetch/fetch_query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query GraphFetchQuery($graph_id: ID!, $variant: String) {
frontendUrlRoot,
service(id: $graph_id) {
schema(tag: $variant) {
document
}
variants {
name
}
}
}
5 changes: 5 additions & 0 deletions crates/rover-client/src/operations/graph/fetch/mod.rs
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::GraphFetchInput;
Loading