-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup(compiler): make query compiler playground a binary crate
- Loading branch information
Showing
3 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
query-compiler/query-compiler-playground/src/schema.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |