1
crates/graphql-schema/.gitignore
vendored
Normal file
1
crates/graphql-schema/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
20
crates/graphql-schema/Cargo.toml
Normal file
20
crates/graphql-schema/Cargo.toml
Normal file
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "graphql-schema"
|
||||
edition = "2021"
|
||||
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
||||
dotenv.workspace = true
|
||||
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
uuid = { version = "1.7.0", features = ["v4"] }
|
||||
graphql_client = { version = "0.14.0" }
|
||||
reqwest = { version = "0.12.10", features = ["json"] }
|
||||
cynic-introspection = "3.9.1"
|
||||
cynic = { version = "3.9.1", features = ["http-reqwest", "reqwest", "serde_json"] }
|
90
crates/graphql-schema/src/main.rs
Normal file
90
crates/graphql-schema/src/main.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
use std::{io::Write, path::PathBuf};
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
||||
struct Command {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Download {
|
||||
#[arg(long, env = "ENDPOINT")]
|
||||
endpoint: String,
|
||||
|
||||
#[arg(long, env = "AUTHORIZATION")]
|
||||
authorization: Option<String>,
|
||||
|
||||
#[arg(long = "output-file", env = "OUTPUT_FILE")]
|
||||
output_file: Option<PathBuf>,
|
||||
|
||||
#[arg(long = "output-stdout", env = "OUTPUT_STDOUT", default_value = "false")]
|
||||
output_stdout: bool,
|
||||
},
|
||||
}
|
||||
|
||||
use cynic::{http::ReqwestExt, QueryBuilder};
|
||||
use cynic_introspection::IntrospectionQuery;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
tracing_subscriber::fmt()
|
||||
.with_writer(std::io::stderr)
|
||||
.finish()
|
||||
.init();
|
||||
|
||||
let cli = Command::parse();
|
||||
tracing::debug!("Starting cli");
|
||||
|
||||
if let Some(Commands::Download {
|
||||
endpoint,
|
||||
authorization,
|
||||
output_file,
|
||||
output_stdout,
|
||||
}) = cli.command
|
||||
{
|
||||
tracing::info!("making request to: {}", endpoint);
|
||||
let client = reqwest::Client::new();
|
||||
let mut res = client.post(endpoint);
|
||||
if let Some(token) = authorization {
|
||||
res = res.bearer_auth(token);
|
||||
}
|
||||
|
||||
let schema = res
|
||||
.run_graphql(IntrospectionQuery::build(()))
|
||||
.await?
|
||||
.data
|
||||
.ok_or(anyhow::anyhow!("failed to get data"))?
|
||||
.into_schema()?;
|
||||
|
||||
let mut output = String::new();
|
||||
schema.write_sdl(&mut output)?;
|
||||
tracing::info!("formatted graphql file");
|
||||
|
||||
if let Some(output_file) = output_file {
|
||||
tracing::info!("outputting to file: {}", output_file.display());
|
||||
|
||||
let mut file = tokio::fs::File::create(output_file).await?;
|
||||
file.write_all(output.as_bytes()).await?;
|
||||
file.flush().await?;
|
||||
}
|
||||
|
||||
if output_stdout {
|
||||
tracing::info!("outputting to stdout");
|
||||
|
||||
let mut stdout = tokio::io::stdout();
|
||||
stdout.write_all(output.as_bytes()).await?;
|
||||
stdout.flush().await?;
|
||||
}
|
||||
}
|
||||
|
||||
std::io::stderr().flush()?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user