@@ -2,16 +2,19 @@ use ansi_term::Colour::{Cyan, Red, Yellow};
2
2
use serde:: Serialize ;
3
3
use structopt:: StructOpt ;
4
4
5
- use crate :: command:: RoverStdout ;
6
5
use crate :: utils:: {
7
6
client:: StudioClientConfig ,
8
7
git:: GitContext ,
9
8
loaders:: load_schema_from_flag,
10
9
parsers:: { parse_graph_ref, parse_schema_source, GraphRef , SchemaSource } ,
11
10
} ;
12
- use crate :: Result ;
11
+ use crate :: { anyhow, Result } ;
12
+ use crate :: { command:: RoverStdout , error:: RoverError } ;
13
13
14
- use rover_client:: query:: subgraph:: publish:: { self , PublishPartialSchemaResponse } ;
14
+ use rover_client:: query:: {
15
+ config:: is_federated,
16
+ subgraph:: publish:: { self , PublishPartialSchemaResponse } ,
17
+ } ;
15
18
16
19
#[ derive( Debug , Serialize , StructOpt ) ]
17
20
pub struct Publish {
@@ -37,6 +40,10 @@ pub struct Publish {
37
40
#[ serde( skip_serializing) ]
38
41
subgraph : String ,
39
42
43
+ /// Indicate whether to convert a non-federated graph into a subgraph
44
+ #[ structopt( short, long) ]
45
+ convert : bool ,
46
+
40
47
/// Url of a running subgraph that a gateway can route operations to
41
48
/// (often a deployed subgraph). May be left empty ("") or a placeholder url
42
49
/// if not running a gateway in managed federation mode
@@ -64,6 +71,24 @@ impl Publish {
64
71
65
72
tracing:: debug!( "Schema Document to publish:\n {}" , & schema_document) ;
66
73
74
+ // This response is used to check whether or not the current graph is federated.
75
+ let federated_response = is_federated:: run (
76
+ is_federated:: is_federated_graph:: Variables {
77
+ graph_id : self . graph . name . clone ( ) ,
78
+ graph_variant : self . graph . variant . clone ( ) ,
79
+ } ,
80
+ & client,
81
+ ) ?;
82
+
83
+ // We don't want to implicitly convert non-federated graph to subgraphs.
84
+ // Error here if no --convert flag is passed _and_ the current context
85
+ // is non-federated. Add a suggestion to require a --convert flag.
86
+ if !federated_response. result && !self . convert {
87
+ return Err ( RoverError :: new ( anyhow ! (
88
+ "Could not publish a subgraph to a non-federated graph."
89
+ ) ) ) ;
90
+ }
91
+
67
92
let publish_response = publish:: run (
68
93
publish:: publish_partial_schema_mutation:: Variables {
69
94
graph_id : self . graph . name . clone ( ) ,
@@ -81,12 +106,12 @@ impl Publish {
81
106
& client,
82
107
) ?;
83
108
84
- handle_response ( publish_response, & self . subgraph , & self . graph . name ) ;
109
+ handle_publish_response ( publish_response, & self . subgraph , & self . graph . name ) ;
85
110
Ok ( RoverStdout :: None )
86
111
}
87
112
}
88
113
89
- fn handle_response ( response : PublishPartialSchemaResponse , subgraph : & str , graph : & str ) {
114
+ fn handle_publish_response ( response : PublishPartialSchemaResponse , subgraph : & str , graph : & str ) {
90
115
if response. service_was_created {
91
116
eprintln ! (
92
117
"A new subgraph called '{}' for the '{}' graph was created" ,
@@ -120,7 +145,7 @@ fn handle_response(response: PublishPartialSchemaResponse, subgraph: &str, graph
120
145
121
146
#[ cfg( test) ]
122
147
mod tests {
123
- use super :: { handle_response , PublishPartialSchemaResponse } ;
148
+ use super :: { handle_publish_response , PublishPartialSchemaResponse } ;
124
149
125
150
// this test is a bit weird, since we can't test the output. We just verify it
126
151
// doesn't error
@@ -133,7 +158,7 @@ mod tests {
133
158
composition_errors : None ,
134
159
} ;
135
160
136
- handle_response ( response, "accounts" , "my-graph" ) ;
161
+ handle_publish_response ( response, "accounts" , "my-graph" ) ;
137
162
}
138
163
139
164
#[ test]
@@ -148,7 +173,7 @@ mod tests {
148
173
] ) ,
149
174
} ;
150
175
151
- handle_response ( response, "accounts" , "my-graph" ) ;
176
+ handle_publish_response ( response, "accounts" , "my-graph" ) ;
152
177
}
153
178
154
179
// TODO: test the actual output of the logs whenever we do design work
0 commit comments