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 subgraph introspect #641

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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query IntrospectionQuery{
query SubgraphIntrospectQuery{
_service {
sdl
}
Expand Down
5 changes: 5 additions & 0 deletions crates/rover-client/src/query/subgraph/introspect/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub(crate) mod query_runner;
pub(crate) mod types;

pub use query_runner::run;
pub use types::{SubgraphIntrospectInput, SubgraphIntrospectResponse};
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
use crate::blocking::GraphQLClient;
use crate::query::subgraph::introspect::types::*;
use crate::RoverClientError;

use graphql_client::*;
use std::collections::HashMap;

#[derive(GraphQLQuery)]
#[graphql(
query_path = "src/query/subgraph/introspect_query.graphql",
schema_path = "src/query/subgraph/introspect_schema.graphql",
query_path = "src/query/subgraph/introspect/introspect_query.graphql",
schema_path = "src/query/subgraph/introspect/introspect_schema.graphql",
response_derives = "PartialEq, Debug, Serialize, Deserialize",
deprecated = "warn"
)]

pub struct IntrospectionQuery;

#[derive(Debug, PartialEq)]
pub struct IntrospectionResponse {
pub result: String,
}
pub(crate) struct SubgraphIntrospectQuery;

pub fn run(
input: SubgraphIntrospectInput,
client: &GraphQLClient,
headers: &HashMap<String, String>,
) -> Result<IntrospectionResponse, RoverClientError> {
let variables = introspection_query::Variables {};
let response_data = client.post::<IntrospectionQuery>(variables, headers);
) -> Result<SubgraphIntrospectResponse, RoverClientError> {
let response_data =
client.post::<SubgraphIntrospectQuery>(input.clone().into(), &input.headers);

match response_data {
Ok(data) => build_response(data),
Expand All @@ -39,14 +35,12 @@ pub fn run(
}
}

fn build_response(
data: introspection_query::ResponseData,
) -> Result<IntrospectionResponse, RoverClientError> {
fn build_response(data: QueryResponseData) -> Result<SubgraphIntrospectResponse, RoverClientError> {
let service_data = data.service.ok_or(RoverClientError::IntrospectionError {
msg: "No introspection response available.".to_string(),
})?;

Ok(IntrospectionResponse {
Ok(SubgraphIntrospectResponse {
result: service_data.sdl,
})
}
22 changes: 22 additions & 0 deletions crates/rover-client/src/query/subgraph/introspect/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::query::subgraph::introspect::query_runner::subgraph_introspect_query;

pub(crate) type QueryVariables = subgraph_introspect_query::Variables;
pub(crate) type QueryResponseData = subgraph_introspect_query::ResponseData;

use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq)]
pub struct SubgraphIntrospectInput {
pub headers: HashMap<String, String>,
}

impl From<SubgraphIntrospectInput> for QueryVariables {
fn from(_input: SubgraphIntrospectInput) -> Self {
Self {}
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct SubgraphIntrospectResponse {
pub result: String,
}
7 changes: 5 additions & 2 deletions src/command/subgraph/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::collections::HashMap;
use structopt::StructOpt;
use url::Url;

use rover_client::{blocking::GraphQLClient, query::subgraph::introspect};
use rover_client::{
blocking::GraphQLClient,
query::subgraph::introspect::{self, SubgraphIntrospectInput},
};

use crate::command::RoverStdout;
use crate::utils::parsers::parse_header;
Expand Down Expand Up @@ -43,7 +46,7 @@ impl Introspect {
}
}

let introspection_response = introspect::run(&client, &headers)?;
let introspection_response = introspect::run(SubgraphIntrospectInput { headers }, &client)?;

Ok(RoverStdout::Introspection(introspection_response.result))
}
Expand Down
8 changes: 7 additions & 1 deletion src/command/supergraph/compose/do_compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ansi_term::Colour::Red;
use camino::Utf8PathBuf;

use rover_client::query::subgraph::fetch::SubgraphFetchInput;
use rover_client::query::subgraph::introspect::SubgraphIntrospectInput;
use rover_client::{
blocking::GraphQLClient,
query::subgraph::{fetch, introspect},
Expand Down Expand Up @@ -105,7 +106,12 @@ pub(crate) fn get_subgraph_definitions(
// obtain SDL and add it to subgraph_definition.
let client = GraphQLClient::new(&subgraph_url.to_string())?;

let introspection_response = introspect::run(&client, &HashMap::new())?;
let introspection_response = introspect::run(
SubgraphIntrospectInput {
headers: HashMap::new(),
},
&client,
)?;
let schema = introspection_response.result;

// We don't require a routing_url for this variant of a schema,
Expand Down