Skip to content

Commit

Permalink
feat(query-compiler-playground): render query graphs to png (#5150)
Browse files Browse the repository at this point in the history
Render the query graph for the query in playground using Graphviz. This
is useful because we can't use the usual mechanism with the
`PRISMA_RENDER_DOT_FILE` environment variable when running the
TypeScript playground with WASM query compiler.
  • Loading branch information
aqrln authored Feb 5, 2025
1 parent 13fef94 commit 8f5c6ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions query-compiler/query-compiler-playground/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
graph.dot
graph.png
33 changes: 31 additions & 2 deletions query-compiler/query-compiler-playground/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::sync::Arc;
use std::{fs, path::PathBuf, process::Command, sync::Arc};

use quaint::{
prelude::{ConnectionInfo, ExternalConnectionInfo, SqlFamily},
visitor::Postgres,
};
use query_core::{query_graph_builder::QueryGraphBuilder, QueryDocument};
use query_core::{query_graph_builder::QueryGraphBuilder, QueryDocument, QueryGraph, ToGraphviz};
use request_handlers::{JsonBody, JsonSingleQuery, RequestBody};
use serde_json::json;
use sql_query_builder::{Context, SqlQueryBuilder};
Expand Down Expand Up @@ -76,6 +76,7 @@ pub fn main() -> anyhow::Result<()> {
let (graph, _serializer) = QueryGraphBuilder::new(&query_schema).build(query)?;

println!("{graph}");
render_query_graph(&graph)?;

let ctx = Context::new(&connection_info, None);
let builder = SqlQueryBuilder::<Postgres<'_>>::new(ctx);
Expand All @@ -86,3 +87,31 @@ pub fn main() -> anyhow::Result<()> {

Ok(())
}

fn render_query_graph(graph: &QueryGraph) -> anyhow::Result<()> {
let package_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let dot_path = package_root.join("graph.dot");
let png_path = package_root.join("graph.png");

fs::write(&dot_path, graph.to_graphviz())?;

match Command::new("dot")
.arg("-Tpng")
.arg(&dot_path)
.arg("-o")
.arg(&png_path)
.status()
{
Ok(status) if !status.success() => {
anyhow::bail!("graphviz exited with status {status}")
}
Ok(_) => Ok(()),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
eprintln!("`dot` command not found, please install graphviz to render query graphs");
Ok(())
}
Err(err) => {
anyhow::bail!("failed to execute graphviz: {err}")
}
}
}

0 comments on commit 8f5c6ec

Please sign in to comment.