Skip to content

Commit

Permalink
refactor(typegraph_core): Simplify private rust SDK (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
Natoandro authored Oct 5, 2023
1 parent 819976b commit c8e742d
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 208 deletions.
1 change: 1 addition & 0 deletions typegraph/core/src/runtimes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::runtimes::prisma::migration::{
};
use crate::runtimes::prisma::with_prisma_runtime;
use crate::runtimes::typegraph::TypegraphOperation;
use crate::t::TypeBuilder;
use crate::validation::types::validate_value;
use crate::wit::aws::S3RuntimeData;
use crate::wit::core::{FuncParams, MaterializerId, RuntimeId, TypeId as CoreTypeId};
Expand Down
111 changes: 57 additions & 54 deletions typegraph/core/src/runtimes/prisma/relationship/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Relationship {
}
}

#[derive(Default)]
#[derive(Default, Clone)]
pub struct PrismaLink {
type_name: String,
rel_name: Option<String>,
Expand Down Expand Up @@ -145,15 +145,15 @@ impl PrismaLink {
self
}

pub fn build(mut self) -> Result<TypeId> {
let mut proxy = t::proxy(self.type_name);
if let Some(rel_name) = self.rel_name.take() {
fn build_link(&self) -> Result<TypeId> {
let mut proxy = t::proxy(&self.type_name);
if let Some(rel_name) = self.rel_name.clone() {
proxy.set("rel_name", rel_name);
}
if let Some(fkey) = self.fkey {
proxy.set("fkey", format!("{fkey}"));
}
if let Some(target_field) = self.target_field.take() {
if let Some(target_field) = self.target_field.clone() {
proxy.set("target_field", target_field);
}
let res = proxy.build()?;
Expand All @@ -162,6 +162,17 @@ impl PrismaLink {
}
}

impl TypeBuilder for PrismaLink {
fn build(&self) -> Result<TypeId> {
self.build_link()
}
}

#[allow(dead_code)]
pub fn prisma_linkx(typ: impl TypeBuilder) -> Result<PrismaLink> {
prisma_link(typ.build()?)
}

pub fn prisma_link(type_id: TypeId) -> Result<PrismaLink> {
let name = type_id
.type_name()?
Expand All @@ -180,11 +191,10 @@ use registry::RelationshipRegistry;

#[cfg(test)]
mod test {
use super::prisma_linkn;
use super::{prisma_linkn, prisma_linkx};
use crate::errors::Result;
use crate::global_store::Store;
use crate::runtimes::prisma::errors;
use crate::runtimes::prisma::relationship::prisma_link;
use crate::runtimes::prisma::relationship::registry::RelationshipRegistry;
use crate::t::{self, ConcreteTypeBuilder, TypeBuilder};
use crate::test_utils::*;
Expand All @@ -205,16 +215,16 @@ mod test {
fn test_explicit_relationship_name() -> Result<(), String> {
Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("name", t::string().build()?)
.prop("posts", t::array(t::proxy("Post").build()?).build()?)
.propx("id", t::integer().as_id(true))?
.propx("name", t::string())?
.propx("posts", t::arrayx(t::proxy("Post"))?)?
.named("User")
.build()?;

let post = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("title", t::string().build()?)
.prop("author", prisma_linkn("User").name("PostAuthor").build()?)
.propx("id", t::integer().as_id(true))?
.propx("title", t::string())?
.propx("author", prisma_linkn("User").name("PostAuthor"))?
.named("Post")
.build()?;

Expand All @@ -231,19 +241,17 @@ mod test {
fn test_fkey_attribute() -> Result<(), String> {
Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop(
.propx("id", t::integer().as_id(true))?
.propx(
"profile",
prisma_link(t::optional(t::proxy("Profile").build()?).build()?)?
.fkey(true)
.build()?,
)
prisma_linkx(t::optionalx(t::proxy("Profile"))?)?.fkey(true),
)?
.named("User")
.build()?;

let profile = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("user", t::optional(t::proxy("User").build()?).build()?)
.propx("id", t::integer().as_id(true))?
.propx("user", t::optionalx(t::proxy("User"))?)?
.named("Profile")
.build()?;

Expand All @@ -260,19 +268,17 @@ mod test {
fn test_unique_attribute() -> Result<(), String> {
Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop(
.propx("id", t::integer().as_id(true))?
.propx(
"profile",
t::optional(t::proxy("Profile").build()?)
.config("unique", "true")
.build()?,
)
t::optionalx(t::proxy("Profile"))?.config("unique", "true"),
)?
.named("User")
.build()?;

let profile = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("user", t::optional(t::proxy("User").build()?).build()?)
.propx("id", t::integer().as_id(true))?
.propx("user", t::optionalx(t::proxy("User"))?)?
.named("Profile")
.build()?;

Expand All @@ -289,9 +295,9 @@ mod test {
fn test_self_relationship() -> Result<(), String> {
Store::reset();
let node = t::struct_()
.prop("id", t::string().as_id(true).build()?)
.prop("children", t::array(t::proxy("Node").build()?).build()?)
.prop("parent", t::proxy("Node").build()?)
.propx("id", t::string().as_id(true))?
.propx("children", t::arrayx(t::proxy("Node"))?)?
.propx("parent", t::proxy("Node"))?
.named("Node")
.build()?;

Expand All @@ -307,14 +313,14 @@ mod test {
fn test_ambiguous_side() -> Result<(), String> {
Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("profile", t::proxy("Profile").build()?)
.propx("id", t::integer().as_id(true))?
.propx("profile", t::proxy("Profile"))?
.named("User")
.build()?;

let profile = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("user", t::proxy("User").build()?)
.propx("id", t::integer().as_id(true))?
.propx("user", t::proxy("User"))?
.named("Profile")
.build()?;

Expand All @@ -332,17 +338,14 @@ mod test {

Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop(
"profile",
t::optional(t::proxy("Profile").build()?).build()?,
)
.propx("id", t::integer().as_id(true))?
.propx("profile", t::optionalx(t::proxy("Profile"))?)?
.named("User")
.build()?;

let profile = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("user", t::optional(t::proxy("User").build()?).build()?)
.propx("id", t::integer().as_id(true))?
.propx("user", t::optionalx(t::proxy("User"))?)?
.named("Profile")
.build()?;

Expand All @@ -365,14 +368,14 @@ mod test {
fn test_conflicting_attributes() -> Result<(), String> {
Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("profile", prisma_linkn("Profile").fkey(true).build()?)
.propx("id", t::integer().as_id(true))?
.propx("profile", prisma_linkn("Profile").fkey(true))?
.named("User")
.build()?;

let profile = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("user", prisma_linkn("User").fkey(true).build()?)
.propx("id", t::integer().as_id(true))?
.propx("user", prisma_linkn("User").fkey(true))?
.named("Profile")
.build()?;

Expand All @@ -394,14 +397,14 @@ mod test {

Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("profile", prisma_linkn("Profile").fkey(false).build()?)
.propx("id", t::integer().as_id(true))?
.propx("profile", prisma_linkn("Profile").fkey(false))?
.named("User")
.build()?;

let profile = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("user", prisma_linkn("User").fkey(false).build()?)
.propx("id", t::integer().as_id(true))?
.propx("user", prisma_linkn("User").fkey(false))?
.named("Profile")
.build()?;

Expand All @@ -428,13 +431,13 @@ mod test {
fn test_missing_target() -> Result<(), String> {
Store::reset();
let user = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.prop("profile", prisma_linkn("Profile").fkey(true).build()?)
.propx("id", t::integer().as_id(true))?
.propx("profile", prisma_linkn("Profile").fkey(true))?
.named("User")
.build()?;

let _profile = t::struct_()
.prop("id", t::integer().as_id(true).build()?)
.propx("id", t::integer().as_id(true))?
.named("Profile")
.build()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl TypeGen for Distinct {
.map(|(k, _)| k.to_string())
.collect::<Vec<_>>();

t::array(t::string().enum_(cols).build()?)
t::arrayx(t::string().enum_(cols))?
.named(self.name())
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl CountOutput {
impl TypeGen for CountOutput {
fn generate(&self, _context: &mut TypeGenContext) -> Result<TypeId> {
let mut builder = t::struct_();
let opt_int = t::optional(t::integer().build()?).build()?;
let opt_int = t::optionalx(t::integer())?.build()?;
builder.prop("_all", opt_int);

for (k, _) in self.model_id.as_struct()?.iter_props() {
Expand Down Expand Up @@ -54,11 +54,11 @@ impl TypeGen for NumberAggregateOutput {
fn generate(&self, _context: &mut TypeGenContext) -> Result<TypeId> {
let mut builder = t::struct_();

let opt_float = t::optional(t::float().build()?).build()?;
let opt_float = t::optionalx(t::float())?.build()?;
let for_int = if self.avg {
opt_float
} else {
t::optional(t::integer().build()?).build()?
t::optionalx(t::integer())?.build()?
};

for (k, type_id) in self.model_id.as_struct()?.iter_props() {
Expand Down
4 changes: 1 addition & 3 deletions typegraph/core/src/runtimes/prisma/type_generation/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ pub struct Count;

impl TypeGen for Count {
fn generate(&self, _context: &mut TypeGenContext) -> Result<TypeId> {
t::optional(t::integer().build()?)
.named(self.name())
.build()
t::optionalx(t::integer())?.named(self.name()).build()
}

fn name(&self) -> String {
Expand Down
16 changes: 6 additions & 10 deletions typegraph/core/src/runtimes/prisma/type_generation/group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl TypeGen for GroupingFields {
}
}

t::array(t::string().enum_(fields).build()?)
t::arrayx(t::string().enum_(fields))?
.named(self.name())
.build()
}
Expand Down Expand Up @@ -73,16 +73,12 @@ impl TypeGen for Having {
let name = self.name();
let self_ref = t::proxy(&name).build()?;

t::union([
t::unionx![
extended_type,
t::struct_()
.prop("AND", t::array(self_ref).build()?)
.build()?,
t::struct_()
.prop("OR", t::array(self_ref).build()?)
.build()?,
t::struct_().prop("NOT", self_ref).build()?,
])
t::struct_().propx("AND", t::array(self_ref))?,
t::struct_().propx("OR", t::array(self_ref))?,
t::struct_().prop("NOT", self_ref)
]
.named(name)
.build()
}
Expand Down
14 changes: 7 additions & 7 deletions typegraph/core/src/runtimes/prisma/type_generation/input_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ impl TypeGen for InputType {
let connect = context.generate(&Where::new(entry.model_type, false))?;

let mut inner = t::struct_();
inner.prop("create", t::optional(create).build()?);
inner.prop("connect", t::optional(connect).build()?);
inner.propx("create", t::optional(create))?;
inner.propx("connect", t::optional(connect))?;

if let Cardinality::Many = entry.cardinality {
let create_many = t::struct_()
.prop("data", t::array(create).build()?)
.build()?;
inner.prop("createMany", t::optional(create_many).build()?);
inner.propx(
"createMany",
t::optionalx(t::struct_().propx("data", t::array(create))?)?,
)?;
}

// TODO what if cardinality is Cardinality::One ??
builder.prop(k, t::optional(inner.min(1).max(1).build()?).build()?);
builder.propx(k, t::optionalx(inner.min(1).max(1))?)?;
} else {
let attrs = type_id.attrs()?;
match attrs.concrete_type.as_type()? {
Expand Down
Loading

0 comments on commit c8e742d

Please sign in to comment.