1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! The `graphql` subcommand: manage a project's GraphQL admin surface — the
//! persisted-operation **safelist** (register/list/remove trusted operations) and the
//! federation **subgraphs** (publish an SDL / SQL / function subgraph, remove one, and
//! read the composed supergraph). Project-scoped over the control-plane API — the same
//! endpoints `graphql.md` previously documented with `curl`.
use std::path::PathBuf;
use clap::Subcommand;
use crate::client;
use crate::config::ProjectConfig;
/// A failure in the `graphql` subcommand.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Resolving the target or talking to the control plane failed.
#[error(transparent)]
Client(#[from] crate::client::ClientError),
/// A request to the GraphQL admin API failed.
#[error(transparent)]
Http(#[from] reqwest::Error),
/// Reading a `--sdl`/`--file`/`--query-file` input failed.
#[error("reading {0}: {1}")]
Io(String, #[source] std::io::Error),
}
type Result<T> = std::result::Result<T, Error>;
/// Arguments for `boatramp graphql`.
#[derive(Debug, clap::Args)]
pub struct GraphqlArgs {
/// boatramp server base URL (overrides [deploy].server).
#[arg(long, env = "BOATRAMP_SERVER", global = true)]
server: Option<String>,
#[command(subcommand)]
command: GraphqlCommand,
}
#[derive(Debug, Subcommand)]
enum GraphqlCommand {
/// Manage the persisted-operation safelist (trusted-operation allowlist).
Safelist {
#[command(subcommand)]
command: SafelistCommand,
},
/// Manage federation subgraphs.
Subgraph {
#[command(subcommand)]
command: SubgraphCommand,
},
/// Print the composed supergraph SDL.
Supergraph,
}
#[derive(Debug, Subcommand)]
enum SafelistCommand {
/// Register a trusted operation. Give the query inline or with `--file`.
Add {
/// The GraphQL operation text (omit to read from `--file`).
query: Option<String>,
/// Read the operation from a file instead of an argument.
#[arg(long)]
file: Option<PathBuf>,
},
/// List the registered trusted operations.
List,
/// Remove a trusted operation by its hash.
Rm {
/// The operation hash (as returned by `add`/`list`).
hash: String,
},
}
#[derive(Debug, Subcommand)]
enum SubgraphCommand {
/// Publish (or replace) a Wasm subgraph from its SDL file.
Put {
/// Subgraph name.
name: String,
/// Path to the subgraph's SDL.
#[arg(long)]
sdl: PathBuf,
},
/// Publish (or replace) a SQL subgraph from a request JSON file (`{site, config}`).
Sql {
/// Subgraph name.
name: String,
/// Path to the JSON request body.
#[arg(long)]
file: PathBuf,
},
/// Register (or refresh) a function subgraph by introspecting the deployed
/// function of the same name — no SDL, no body.
Function {
/// Subgraph / function name.
name: String,
},
/// Remove a subgraph.
Rm {
/// Subgraph name.
name: String,
},
}
/// Entry point for `boatramp graphql`.
pub async fn run(args: GraphqlArgs, config: &ProjectConfig) -> Result<()> {
let server = client::resolve_server(args.server, config)?;
let project = client::resolve_project(config);
let seg = client::project_seg(&project, "graphql");
let http = client::http_client(client::token(config).as_deref());
let base = format!("{server}/api/{seg}");
match args.command {
GraphqlCommand::Safelist { command } => match command {
SafelistCommand::Add { query, file } => {
let query = read_input(query, file)?;
let resp = http
.post(format!("{base}/safelist"))
.json(&serde_json::json!({ "query": query }))
.send()
.await?
.error_for_status()?;
let body: serde_json::Value = resp.json().await.unwrap_or_default();
let hash = body
.get("hash")
.and_then(|h| h.as_str())
.unwrap_or("(registered)");
println!("safelisted operation: {hash}");
}
SafelistCommand::List => {
let body: serde_json::Value = http
.get(format!("{base}/safelist"))
.send()
.await?
.error_for_status()?
.json()
.await
.unwrap_or_default();
println!(
"{}",
serde_json::to_string_pretty(&body).unwrap_or_default()
);
}
SafelistCommand::Rm { hash } => {
http.delete(format!("{base}/safelist/{hash}"))
.send()
.await?
.error_for_status()?;
println!("removed safelisted operation {hash}");
}
},
GraphqlCommand::Subgraph { command } => match command {
SubgraphCommand::Put { name, sdl } => {
let body = read_file(&sdl)?;
http.put(format!("{base}/subgraphs/{name}"))
.header("content-type", "text/plain")
.body(body)
.send()
.await?
.error_for_status()?;
println!("published subgraph {name}");
}
SubgraphCommand::Sql { name, file } => {
put_json_subgraph(&http, &base, &name, "sql", &file).await?;
}
SubgraphCommand::Function { name } => {
http.put(format!("{base}/subgraphs/{name}/function"))
.send()
.await?
.error_for_status()?;
println!("registered function subgraph {name}");
}
SubgraphCommand::Rm { name } => {
http.delete(format!("{base}/subgraphs/{name}"))
.send()
.await?
.error_for_status()?;
println!("removed subgraph {name}");
}
},
GraphqlCommand::Supergraph => {
let text = http
.get(format!("{base}/supergraph"))
.send()
.await?
.error_for_status()?
.text()
.await?;
println!("{text}");
}
}
Ok(())
}
/// PUT a JSON-bodied subgraph (`sql` / `function`) from a request file.
async fn put_json_subgraph(
http: &client::ApiClient,
base: &str,
name: &str,
kind: &str,
file: &std::path::Path,
) -> Result<()> {
let raw = read_file(file)?;
let value: serde_json::Value = serde_json::from_str(&raw).map_err(|e| {
Error::Io(
file.display().to_string(),
std::io::Error::new(std::io::ErrorKind::InvalidData, e),
)
})?;
http.put(format!("{base}/subgraphs/{name}/{kind}"))
.json(&value)
.send()
.await?
.error_for_status()?;
println!("published {kind} subgraph {name}");
Ok(())
}
/// Resolve a query given inline text or a `--file`.
fn read_input(inline: Option<String>, file: Option<PathBuf>) -> Result<String> {
match (inline, file) {
(Some(q), _) => Ok(q),
(None, Some(path)) => read_file(&path),
(None, None) => Ok(String::new()),
}
}
/// Read a file, tagging the path into the error.
fn read_file(path: &std::path::Path) -> Result<String> {
std::fs::read_to_string(path).map_err(|e| Error::Io(path.display().to_string(), e))
}