Skip to content

Commit

Permalink
Size low hanging fruits
Browse files Browse the repository at this point in the history
Removes following functionality from WASM engine:
- GraphQL protocol
- DMMF
- SDL Schema

Neither of the features are used by the client runtimes and thrid party
clients don't and can not use WASM engine, so it is safe to remove.
  • Loading branch information
SevInf committed Nov 30, 2023
1 parent 77e1027 commit f63661b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 57 deletions.
5 changes: 4 additions & 1 deletion query-engine/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.1.0"

[features]
metrics = ["query-engine-metrics"]
graphql-protocol = []

[dependencies]
async-trait = "0.1"
Expand All @@ -19,7 +20,9 @@ indexmap = { version = "1.7", features = ["serde-1"] }
itertools = "0.10"
once_cell = "1"
petgraph = "0.4"
query-structure = { path = "../query-structure", features = ["default_generators"] }
query-structure = { path = "../query-structure", features = [
"default_generators",
] }
opentelemetry = { version = "0.17.0", features = ["rt-tokio", "serialize"] }
query-engine-metrics = { path = "../metrics", optional = true }
serde.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions query-engine/core/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::Deserialize;
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EngineProtocol {
#[cfg(feature = "graphql-protocol")]
Graphql,
Json,
}
Expand All @@ -14,6 +15,7 @@ impl EngineProtocol {
}

/// Returns `true` if the engine protocol is [`Graphql`].
#[cfg(feature = "graphql-protocol")]
pub fn is_graphql(&self) -> bool {
matches!(self, Self::Graphql)
}
Expand All @@ -22,6 +24,7 @@ impl EngineProtocol {
impl From<&String> for EngineProtocol {
fn from(s: &String) -> Self {
match s.as_str() {
#[cfg(feature = "graphql-protocol")]
"graphql" => EngineProtocol::Graphql,
"json" => EngineProtocol::Json,
x => panic!("Unknown engine protocol '{x}'. Must be 'graphql' or 'json'."),
Expand Down
2 changes: 2 additions & 0 deletions query-engine/core/src/response_ir/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,13 @@ fn serialize_scalar(field: &OutputField<'_>, value: PrismaValue) -> crate::Resul

fn convert_prisma_value(field: &OutputField<'_>, value: PrismaValue, st: &ScalarType) -> crate::Result<PrismaValue> {
match crate::executor::get_engine_protocol() {
#[cfg(feature = "graphql-protocol")]
EngineProtocol::Graphql => convert_prisma_value_graphql_protocol(field, value, st),
EngineProtocol::Json => convert_prisma_value_json_protocol(field, value, st),
}
}

#[cfg(feature = "graphql-protocol")]
fn convert_prisma_value_graphql_protocol(
field: &OutputField<'_>,
value: PrismaValue,
Expand Down
1 change: 0 additions & 1 deletion query-engine/query-engine-node-api/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::ApiError;
use napi_derive::napi;
use request_handlers::dmmf;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion query-engine/query-engine-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[lib]
doc = false
crate-type = ["cdylib"]
name = "query_engine"
name = "query_engine_wasm"

[dependencies]

Expand Down
33 changes: 1 addition & 32 deletions query-engine/query-engine-wasm/src/wasm/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use query_core::{
telemetry, QueryExecutor, TransactionOptions, TxId,
};
use request_handlers::ConnectorMode;
use request_handlers::{dmmf, load_executor, render_graphql_schema, RequestBody, RequestHandler};
use request_handlers::{load_executor, RequestBody, RequestHandler};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
Expand Down Expand Up @@ -385,28 +385,6 @@ impl QueryEngine {
.await
}

#[wasm_bindgen]
pub async fn dmmf(&self, trace: String) -> Result<String, wasm_bindgen::JsError> {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;

let dispatcher = self.logger.dispatcher();

tracing::dispatcher::with_default(&dispatcher, || {
let span = tracing::info_span!("prisma:engine:dmmf");
let _ = telemetry::helpers::set_parent_context_from_json_str(&span, &trace);
let _guard = span.enter();
let dmmf = dmmf::render_dmmf(&engine.query_schema);

let json = {
let _span = tracing::info_span!("prisma:engine:dmmf_to_json").entered();
serde_json::to_string(&dmmf)?
};

Ok(json)
})
}

/// If connected, attempts to roll back a transaction with id `tx_id` in the core.
#[wasm_bindgen(js_name = rollbackTransaction)]
pub async fn rollback_transaction(&self, tx_id: String, trace: String) -> Result<String, wasm_bindgen::JsError> {
Expand All @@ -425,15 +403,6 @@ impl QueryEngine {
.await
}

/// Loads the query schema. Only available when connected.
#[wasm_bindgen(js_name = sdlSchema)]
pub async fn sdl_schema(&self) -> Result<String, wasm_bindgen::JsError> {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;

Ok(render_graphql_schema(engine.query_schema()))
}

#[wasm_bindgen]
pub async fn metrics(&self, json_options: String) -> Result<(), wasm_bindgen::JsError> {
log::info!("Called `QueryEngine::metrics()`");
Expand Down
18 changes: 0 additions & 18 deletions query-engine/query-engine-wasm/src/wasm/functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::error::ApiError;
use request_handlers::dmmf;
use serde::Serialize;
use std::sync::Arc;
use tsify::Tsify;
use wasm_bindgen::prelude::wasm_bindgen;

Expand All @@ -21,21 +18,6 @@ pub fn version() -> Version {
}
}

#[wasm_bindgen]
pub fn dmmf(datamodel_string: String) -> Result<String, wasm_bindgen::JsError> {
let mut schema = psl::validate(datamodel_string.into());

schema
.diagnostics
.to_result()
.map_err(|errors| ApiError::conversion(errors, schema.db.source()))?;

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

Ok(serde_json::to_string(&dmmf)?)
}

#[wasm_bindgen]
pub fn debug_panic(panic_message: Option<String>) -> Result<(), wasm_bindgen::JsError> {
let user_facing = user_facing_errors::Error::from_panic_payload(Box::new(
Expand Down
12 changes: 9 additions & 3 deletions query-engine/request-handlers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bigdecimal = "0.3"
thiserror = "1"
tracing = "0.1"
url = "2"
connection-string.workspace = true
connection-string.workspace = true
once_cell = "1.15"

mongodb-query-connector = { path = "../connectors/mongodb-query-connector", optional = true }
Expand All @@ -32,11 +32,17 @@ schema = { path = "../schema" }
codspeed-criterion-compat = "1.1.0"

[features]
default = ["sql", "mongodb", "native"]
default = ["sql", "mongodb", "native", "graphql-protocol"]
mongodb = ["mongodb-query-connector"]
sql = ["sql-query-connector"]
driver-adapters = ["sql-query-connector/driver-adapters"]
native = ["mongodb", "sql-query-connector", "quaint/native", "query-core/metrics"]
native = [
"mongodb",
"sql-query-connector",
"quaint/native",
"query-core/metrics",
]
graphql-protocol = ["query-core/graphql-protocol"]

[[bench]]
name = "query_planning_bench"
Expand Down
4 changes: 3 additions & 1 deletion query-engine/request-handlers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ mod response;
pub use self::{error::HandlerError, load_executor::load as load_executor};
pub use connector_mode::ConnectorMode;
pub use handler::*;
pub use protocols::{graphql::*, json::*, RequestBody};
#[cfg(feature = "graphql-protocol")]
pub use protocols::graphql::*;
pub use protocols::{json::*, RequestBody};
pub use response::*;

pub type Result<T> = std::result::Result<T, HandlerError>;
Expand Down
6 changes: 6 additions & 0 deletions query-engine/request-handlers/src/protocols/mod.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
#[cfg(feature = "graphql-protocol")]
pub mod graphql;
pub mod json;

use query_core::{protocol::EngineProtocol, schema::QuerySchemaRef, QueryDocument};

#[derive(Debug)]
pub enum RequestBody {
#[cfg(feature = "graphql-protocol")]
Graphql(graphql::GraphqlBody),
Json(json::JsonBody),
}

impl RequestBody {
pub fn into_doc(self, query_schema: &QuerySchemaRef) -> crate::Result<QueryDocument> {
match self {
#[cfg(feature = "graphql-protocol")]
RequestBody::Graphql(body) => body.into_doc(),
RequestBody::Json(body) => body.into_doc(query_schema),
}
}

pub fn try_from_str(val: &str, engine_protocol: EngineProtocol) -> Result<RequestBody, serde_json::Error> {
match engine_protocol {
#[cfg(feature = "graphql-protocol")]
EngineProtocol::Graphql => serde_json::from_str::<graphql::GraphqlBody>(val).map(Self::from),
EngineProtocol::Json => serde_json::from_str::<json::JsonBody>(val).map(Self::from),
}
}

pub fn try_from_slice(val: &[u8], engine_protocol: EngineProtocol) -> Result<RequestBody, serde_json::Error> {
match engine_protocol {
#[cfg(feature = "graphql-protocol")]
EngineProtocol::Graphql => serde_json::from_slice::<graphql::GraphqlBody>(val).map(Self::from),
EngineProtocol::Json => serde_json::from_slice::<json::JsonBody>(val).map(Self::from),
}
}
}

#[cfg(feature = "graphql-protocol")]
impl From<graphql::GraphqlBody> for RequestBody {
fn from(body: graphql::GraphqlBody) -> Self {
Self::Graphql(body)
Expand Down

0 comments on commit f63661b

Please sign in to comment.