Skip to content

Commit

Permalink
cleanup(compiler): make query compiler playground a binary crate
Browse files Browse the repository at this point in the history
  • Loading branch information
FGoessler committed Jan 20, 2025
1 parent e5f4ae7 commit d6f8a09
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
1 change: 0 additions & 1 deletion query-compiler/query-compiler-playground/src/lib.rs

This file was deleted.

88 changes: 88 additions & 0 deletions query-compiler/query-compiler-playground/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::sync::Arc;

use quaint::{
prelude::{ConnectionInfo, ExternalConnectionInfo, SqlFamily},
visitor::Postgres,
};
use query_core::{query_graph_builder::QueryGraphBuilder, QueryDocument};
use request_handlers::{JsonBody, JsonSingleQuery, RequestBody};
use serde_json::json;
use sql_query_builder::{Context, SqlQueryBuilder};

pub fn main() -> anyhow::Result<()> {
let schema_string = include_str!("./schema.prisma");
let schema = psl::validate(schema_string.into());

if schema.diagnostics.has_errors() {
anyhow::bail!("invalid schema");
}

let schema = Arc::new(schema);
let query_schema = Arc::new(query_core::schema::build(schema, true));

let connection_info = ConnectionInfo::External(ExternalConnectionInfo::new(
SqlFamily::Postgres,
"public".to_owned(),
None,
));

// prisma.user.findUnique({
// where: {
// email: Prisma.Param("userEmail")
// },
// select: {
// val: true,
// posts: true,
// profile: true,
// }
// })
let query: JsonSingleQuery = serde_json::from_value(json!({
"modelName": "User",
"action": "findMany",
"query": {
"arguments": {
"where": {
"email": {
"$type": "Param",
"value": "userEmail"
}
}
},
"selection": {
"val": true,
"posts": {
"arguments": {},
"selection": {
"$scalars": true
}
},
"profile": {
"arguments": {},
"selection": {
"$scalars": true
}
}
}
}
}))?;

let request = RequestBody::Json(JsonBody::Single(query));
let doc = request.into_doc(&query_schema)?;

let QueryDocument::Single(query) = doc else {
anyhow::bail!("expected single query");
};

let (graph, _serializer) = QueryGraphBuilder::new(&query_schema).build(query)?;

println!("{graph}");

let ctx = Context::new(&connection_info, None);
let builder = SqlQueryBuilder::<Postgres<'_>>::new(ctx);

let expr = query_compiler::translate(graph, &builder)?;

println!("{}", expr.pretty_print(true, 80)?);

Ok(())
}
33 changes: 33 additions & 0 deletions query-compiler/query-compiler-playground/src/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = "postgresql://postgres:prisma@localhost:5438"
}

model User {
id String @id @default(cuid())
email String @unique
name String?
posts Post[]
val Int?
profile Profile?
}

model Profile {
userId String @id
user User @relation(fields: [userId], references: [id])
}

model Post {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean
title String
content String?
authorId String?
author User? @relation(fields: [authorId], references: [id])
}

0 comments on commit d6f8a09

Please sign in to comment.