Skip to content

Commit

Permalink
feat(rover): subgraph checks
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Dec 15, 2020
1 parent 0b9ee37 commit 46d2fff
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 2 deletions.
125 changes: 123 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ binstall = { path = "./installers/binstall" }
anyhow = "1.0.31"
atty = "0.2.14"
console = "0.13.0"
prettytable-rs = "0.8.0"
serde = "1.0"
serde_json = "1.0"
structopt = "0.3.15"
Expand Down
36 changes: 36 additions & 0 deletions crates/rover-client/src/query/subgraph/check.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mutation CheckPartialSchemaQuery (
$graph_id: ID!
$variant: String!
$implementingServiceName: String!
$partialSchema: PartialSchemaInput!
) {
service(id: $graph_id) {
checkPartialSchema(
graphVariant: $variant
implementingServiceName: $implementingServiceName
partialSchema: $partialSchema
) {
compositionValidationResult {
compositionValidationDetails {
schemaHash
}
graphCompositionID
errors {
message
}
}
checkSchemaResult {
diffToPrevious {
severity
numberOfCheckedOperations
changes {
severity
code
description
}
}
targetUrl
}
}
}
}
79 changes: 79 additions & 0 deletions crates/rover-client/src/query/subgraph/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::blocking::StudioClient;
use crate::RoverClientError;
use graphql_client::*;

use reqwest::Url;

#[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/subgraph/check.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. check_partial_schema_query
pub struct CheckPartialSchemaQuery;

/// The main function to be used from this module.
/// This function takes a proposed schema and validates it against a pushed
/// schema.
pub fn run(
variables: check_partial_schema_query::Variables,
client: &StudioClient,
) -> Result<CheckResponse, RoverClientError> {
let data = client.post::<CheckPartialSchemaQuery>(variables)?;
get_check_response_from_data(data)
}

#[derive(Debug)]
pub struct CheckResponse {
pub target_url: Option<Url>,
pub number_of_checked_operations: i64,
pub change_severity: check_partial_schema_query::ChangeSeverity,
pub changes: Vec<check_partial_schema_query::CheckPartialSchemaQueryServiceCheckPartialSchemaCheckSchemaResultDiffToPreviousChanges>
}

fn get_check_response_from_data(
data: check_partial_schema_query::ResponseData,
) -> Result<CheckResponse, RoverClientError> {
let service = data.service.ok_or(RoverClientError::NoService)?;

// TODO: fix this error
let check_schema_result = service
.check_partial_schema
.check_schema_result
.ok_or(RoverClientError::NoData)?;
let target_url = get_url(check_schema_result.target_url);

let diff_to_previous = check_schema_result.diff_to_previous;

let number_of_checked_operations = diff_to_previous.number_of_checked_operations.unwrap_or(0);

let change_severity = diff_to_previous.severity;
let changes = diff_to_previous.changes;

Ok(CheckResponse {
target_url,
number_of_checked_operations,
change_severity,
changes,
})
}

fn get_url(url: Option<String>) -> Option<Url> {
match url {
Some(url) => {
let url = Url::parse(&url);
match url {
Ok(url) => Some(url),
// if the API returns an invalid URL, don't put it in the response
Err(_) => None,
}
}
None => None,
}
}
3 changes: 3 additions & 0 deletions crates/rover-client/src/query/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub mod push;

/// "subgraph delete" command execution
pub mod delete;

/// "subgraph check" command execution
pub mod check;
Loading

0 comments on commit 46d2fff

Please sign in to comment.