feat: make introspection

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-12-27 15:39:55 +01:00
commit 6622e327a1
11 changed files with 2349 additions and 0 deletions

1
crates/graphql-schema/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

View 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"] }

View 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(())
}