Skip to content

Commit e124da5

Browse files
chore: refactor subgraph publish (#630)
1 parent 24f9608 commit e124da5

File tree

7 files changed

+114
-84
lines changed

7 files changed

+114
-84
lines changed

crates/rover-client/src/query/subgraph/check/types.rs

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type QueryVariables = subgraph_check_query::Variables;
99
type QueryChangeSeverity = subgraph_check_query::ChangeSeverity;
1010
type QuerySchema = subgraph_check_query::PartialSchemaInput;
1111
type QueryConfig = subgraph_check_query::HistoricQueryParameters;
12+
type GitContextInput = subgraph_check_query::GitContextInput;
1213

1314
#[derive(Debug, Clone, PartialEq)]
1415
pub struct SubgraphCheckInput {
@@ -99,3 +100,15 @@ pub struct CompositionError {
99100
pub message: String,
100101
pub code: Option<String>,
101102
}
103+
104+
impl From<GitContext> for GitContextInput {
105+
fn from(git_context: GitContext) -> GitContextInput {
106+
GitContextInput {
107+
branch: git_context.branch,
108+
commit: git_context.commit,
109+
committer: git_context.author,
110+
remote_url: git_context.remote_url,
111+
message: None,
112+
}
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod mutation_runner;
2+
pub(crate) mod types;
3+
pub use types::{SubgraphPublishInput, SubgraphPublishResponse};

crates/rover-client/src/query/subgraph/publish.rs crates/rover-client/src/query/subgraph/publish/mutation_runner.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// PublishPartialSchemaMutation
1+
use super::types::*;
22
use crate::blocking::StudioClient;
33
use crate::query::config::is_federated;
44
use crate::RoverClientError;
@@ -8,38 +8,30 @@ use graphql_client::*;
88
// The paths are relative to the directory where your `Cargo.toml` is located.
99
// Both json and the GraphQL schema language are supported as sources for the schema
1010
#[graphql(
11-
query_path = "src/query/subgraph/publish.graphql",
11+
query_path = "src/query/subgraph/publish/publish_mutation.graphql",
1212
schema_path = ".schema/schema.graphql",
1313
response_derives = "PartialEq, Debug, Serialize, Deserialize",
1414
deprecated = "warn"
1515
)]
1616
/// This struct is used to generate the module containing `Variables` and
1717
/// `ResponseData` structs.
18-
/// Snake case of this name is the mod name. i.e. publish_partial_schema_mutation
19-
pub struct PublishPartialSchemaMutation;
20-
21-
#[derive(Debug, PartialEq)]
22-
pub struct PublishPartialSchemaResponse {
23-
pub schema_hash: Option<String>,
24-
pub did_update_gateway: bool,
25-
pub service_was_created: bool,
26-
pub composition_errors: Option<Vec<String>>,
27-
}
18+
/// Snake case of this name is the mod name. i.e. subgraph_publish_mutation
19+
pub struct SubgraphPublishMutation;
2820

2921
pub fn run(
30-
variables: publish_partial_schema_mutation::Variables,
22+
input: SubgraphPublishInput,
3123
client: &StudioClient,
32-
convert_to_federated_graph: bool,
33-
) -> Result<PublishPartialSchemaResponse, RoverClientError> {
34-
let graph = variables.graph_id.clone();
24+
) -> Result<SubgraphPublishResponse, RoverClientError> {
25+
let variables: MutationVariables = input.clone().into();
26+
let graph = input.graph_id.clone();
3527
// We don't want to implicitly convert non-federated graph to supergraphs.
3628
// Error here if no --convert flag is passed _and_ the current context
3729
// is non-federated. Add a suggestion to require a --convert flag.
38-
if !convert_to_federated_graph {
30+
if !input.convert_to_federated_graph {
3931
let is_federated = is_federated::run(
4032
is_federated::is_federated_graph::Variables {
41-
graph_id: variables.graph_id.clone(),
42-
graph_variant: variables.graph_variant.clone(),
33+
graph_id: input.graph_id.clone(),
34+
graph_variant: input.variant,
4335
},
4436
&client,
4537
)?;
@@ -51,24 +43,21 @@ pub fn run(
5143
});
5244
}
5345
}
54-
let data = client.post::<PublishPartialSchemaMutation>(variables)?;
46+
let data = client.post::<SubgraphPublishMutation>(variables)?;
5547
let publish_response = get_publish_response_from_data(data, graph)?;
5648
Ok(build_response(publish_response))
5749
}
5850

59-
// alias this return type since it's disgusting
60-
type UpdateResponse = publish_partial_schema_mutation::PublishPartialSchemaMutationServiceUpsertImplementingServiceAndTriggerComposition;
61-
6251
fn get_publish_response_from_data(
63-
data: publish_partial_schema_mutation::ResponseData,
52+
data: ResponseData,
6453
graph: String,
6554
) -> Result<UpdateResponse, RoverClientError> {
6655
let service_data = data.service.ok_or(RoverClientError::NoService { graph })?;
6756

6857
Ok(service_data.upsert_implementing_service_and_trigger_composition)
6958
}
7059

71-
fn build_response(publish_response: UpdateResponse) -> PublishPartialSchemaResponse {
60+
fn build_response(publish_response: UpdateResponse) -> SubgraphPublishResponse {
7261
let composition_errors: Vec<String> = publish_response
7362
.errors
7463
.iter()
@@ -82,7 +71,7 @@ fn build_response(publish_response: UpdateResponse) -> PublishPartialSchemaRespo
8271
None
8372
};
8473

85-
PublishPartialSchemaResponse {
74+
SubgraphPublishResponse {
8675
schema_hash: match publish_response.composition_config {
8776
Some(config) => Some(config.schema_hash),
8877
None => None,
@@ -114,7 +103,7 @@ mod tests {
114103

115104
assert_eq!(
116105
output,
117-
PublishPartialSchemaResponse {
106+
SubgraphPublishResponse {
118107
schema_hash: Some("5gf564".to_string()),
119108
composition_errors: Some(vec![
120109
"[Accounts] User -> composition error".to_string(),
@@ -139,7 +128,7 @@ mod tests {
139128

140129
assert_eq!(
141130
output,
142-
PublishPartialSchemaResponse {
131+
SubgraphPublishResponse {
143132
schema_hash: Some("5gf564".to_string()),
144133
composition_errors: None,
145134
did_update_gateway: true,
@@ -163,7 +152,7 @@ mod tests {
163152

164153
assert_eq!(
165154
output,
166-
PublishPartialSchemaResponse {
155+
SubgraphPublishResponse {
167156
schema_hash: None,
168157
composition_errors: Some(
169158
vec!["[Accounts] -> Things went really wrong".to_string()]

crates/rover-client/src/query/subgraph/publish.graphql crates/rover-client/src/query/subgraph/publish/publish_mutation.graphql

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
mutation PublishPartialSchemaMutation(
1+
mutation SubgraphPublishMutation(
22
$graphId: ID!
3-
$graphVariant: String!
4-
$name: String!
3+
$variant: String!
4+
$subgraph: String!
55
$url: String
66
$revision: String!
7-
$activePartialSchema: PartialSchemaInput!
7+
$schema: PartialSchemaInput!
88
$gitContext: GitContextInput!
99
) {
1010
service(id: $graphId) {
1111
upsertImplementingServiceAndTriggerComposition(
12-
name: $name
12+
name: $subgraph
1313
url: $url
1414
revision: $revision
15-
activePartialSchema: $activePartialSchema
16-
graphVariant: $graphVariant
15+
activePartialSchema: $schema
16+
graphVariant: $variant
1717
gitContext: $gitContext
1818
) {
1919
compositionConfig {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use super::mutation_runner::subgraph_publish_mutation;
2+
3+
use crate::utils::GitContext;
4+
5+
pub(crate) type ResponseData = subgraph_publish_mutation::ResponseData;
6+
pub(crate) type MutationVariables = subgraph_publish_mutation::Variables;
7+
pub(crate) type UpdateResponse = subgraph_publish_mutation::SubgraphPublishMutationServiceUpsertImplementingServiceAndTriggerComposition;
8+
9+
type SchemaInput = subgraph_publish_mutation::PartialSchemaInput;
10+
type GitContextInput = subgraph_publish_mutation::GitContextInput;
11+
12+
#[derive(Debug, Clone, PartialEq)]
13+
pub struct SubgraphPublishInput {
14+
pub graph_id: String,
15+
pub variant: String,
16+
pub subgraph: String,
17+
pub url: Option<String>,
18+
pub schema: String,
19+
pub git_context: GitContext,
20+
pub convert_to_federated_graph: bool,
21+
}
22+
23+
#[derive(Debug, PartialEq)]
24+
pub struct SubgraphPublishResponse {
25+
pub schema_hash: Option<String>,
26+
pub did_update_gateway: bool,
27+
pub service_was_created: bool,
28+
pub composition_errors: Option<Vec<String>>,
29+
}
30+
31+
impl From<SubgraphPublishInput> for MutationVariables {
32+
fn from(publish_input: SubgraphPublishInput) -> Self {
33+
Self {
34+
graph_id: publish_input.graph_id,
35+
variant: publish_input.variant,
36+
subgraph: publish_input.subgraph,
37+
url: publish_input.url,
38+
schema: SchemaInput {
39+
sdl: Some(publish_input.schema),
40+
hash: None,
41+
},
42+
git_context: publish_input.git_context.into(),
43+
revision: "".to_string(),
44+
}
45+
}
46+
}
47+
48+
impl From<GitContext> for GitContextInput {
49+
fn from(git_context: GitContext) -> GitContextInput {
50+
GitContextInput {
51+
branch: git_context.branch,
52+
commit: git_context.commit,
53+
committer: git_context.author,
54+
remote_url: git_context.remote_url,
55+
message: None,
56+
}
57+
}
58+
}

crates/rover-client/src/utils/git.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::query::{graph, subgraph};
1+
use crate::query::graph;
22

33
use std::env;
44

@@ -163,34 +163,6 @@ impl From<GitContext> for GraphCheckContextInput {
163163
}
164164
}
165165

166-
type SubgraphPublishContextInput =
167-
subgraph::publish::publish_partial_schema_mutation::GitContextInput;
168-
impl From<GitContext> for SubgraphPublishContextInput {
169-
fn from(git_context: GitContext) -> SubgraphPublishContextInput {
170-
SubgraphPublishContextInput {
171-
branch: git_context.branch,
172-
commit: git_context.commit,
173-
committer: git_context.author,
174-
remote_url: git_context.remote_url,
175-
message: None,
176-
}
177-
}
178-
}
179-
180-
type SubgraphCheckContextInput =
181-
subgraph::check::query_runner::subgraph_check_query::GitContextInput;
182-
impl From<GitContext> for SubgraphCheckContextInput {
183-
fn from(git_context: GitContext) -> SubgraphCheckContextInput {
184-
SubgraphCheckContextInput {
185-
branch: git_context.branch,
186-
commit: git_context.commit,
187-
committer: git_context.author,
188-
remote_url: git_context.remote_url,
189-
message: None,
190-
}
191-
}
192-
}
193-
194166
#[cfg(test)]
195167
mod tests {
196168
use super::*;

src/command/subgraph/publish.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::utils::{
1010
};
1111
use crate::Result;
1212

13-
use rover_client::query::subgraph::publish::{self, PublishPartialSchemaResponse};
13+
use rover_client::query::subgraph::publish::{self, SubgraphPublishInput, SubgraphPublishResponse};
1414
use rover_client::utils::GitContext;
1515

1616
#[derive(Debug, Serialize, StructOpt)]
@@ -64,34 +64,29 @@ impl Publish {
6464
Yellow.normal().paint(&self.profile_name)
6565
);
6666

67-
let schema_document = load_schema_from_flag(&self.schema, std::io::stdin())?;
67+
let schema = load_schema_from_flag(&self.schema, std::io::stdin())?;
6868

69-
tracing::debug!("Publishing \n{}", &schema_document);
69+
tracing::debug!("Publishing \n{}", &schema);
7070

71-
let publish_response = publish::run(
72-
publish::publish_partial_schema_mutation::Variables {
71+
let publish_response = publish::mutation_runner::run(
72+
SubgraphPublishInput {
7373
graph_id: self.graph.name.clone(),
74-
graph_variant: self.graph.variant.clone(),
75-
name: self.subgraph.clone(),
76-
active_partial_schema:
77-
publish::publish_partial_schema_mutation::PartialSchemaInput {
78-
sdl: Some(schema_document),
79-
hash: None,
80-
},
81-
revision: "".to_string(),
74+
variant: self.graph.variant.clone(),
75+
subgraph: self.subgraph.clone(),
8276
url: self.routing_url.clone(),
83-
git_context: git_context.into(),
77+
schema,
78+
git_context,
79+
convert_to_federated_graph: self.convert,
8480
},
8581
&client,
86-
self.convert,
8782
)?;
8883

8984
handle_publish_response(publish_response, &self.subgraph, &self.graph.name);
9085
Ok(RoverStdout::None)
9186
}
9287
}
9388

94-
fn handle_publish_response(response: PublishPartialSchemaResponse, subgraph: &str, graph: &str) {
89+
fn handle_publish_response(response: SubgraphPublishResponse, subgraph: &str, graph: &str) {
9590
if response.service_was_created {
9691
eprintln!(
9792
"A new subgraph called '{}' for the '{}' graph was created",
@@ -125,13 +120,13 @@ fn handle_publish_response(response: PublishPartialSchemaResponse, subgraph: &st
125120

126121
#[cfg(test)]
127122
mod tests {
128-
use super::{handle_publish_response, PublishPartialSchemaResponse};
123+
use super::{handle_publish_response, SubgraphPublishResponse};
129124

130125
// this test is a bit weird, since we can't test the output. We just verify it
131126
// doesn't error
132127
#[test]
133128
fn handle_response_doesnt_error_with_all_successes() {
134-
let response = PublishPartialSchemaResponse {
129+
let response = SubgraphPublishResponse {
135130
schema_hash: Some("123456".to_string()),
136131
did_update_gateway: true,
137132
service_was_created: true,
@@ -143,7 +138,7 @@ mod tests {
143138

144139
#[test]
145140
fn handle_response_doesnt_error_with_all_failures() {
146-
let response = PublishPartialSchemaResponse {
141+
let response = SubgraphPublishResponse {
147142
schema_hash: None,
148143
did_update_gateway: false,
149144
service_was_created: false,

0 commit comments

Comments
 (0)