Skip to content

Commit

Permalink
fix: Fix .apply serialization error with optional structs (#886)
Browse files Browse the repository at this point in the history
<!--
Pull requests are squashed and merged using:
- their title as the commit message
- their description as the commit body

Having a good title and description is important for the users to get
readable changelog.
-->

<!-- 1. Explain WHAT the change is about -->

- Fix the error on `.apply` when the apply tree goes beyond an optional
struct, mostly changing the implementation of `resolve_optional` method.
- Improve the errors when we encounter an exception raise by
`Result::Err` from typegraph_core.

<!-- 2. Explain WHY the change cannot be made simpler -->



<!-- 3. Explain HOW users should update their code -->

#### Migration notes

...

- [x] The change comes with new or modified tests
- [ ] Hard-to-understand functions have explanatory comments
- [ ] End-user documentation is updated to reflect the change
  • Loading branch information
Natoandro authored Oct 24, 2024
1 parent 7c52fce commit 042ae80
Show file tree
Hide file tree
Showing 25 changed files with 262 additions and 297 deletions.
80 changes: 0 additions & 80 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions src/typegraph/core/src/global_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::runtimes::{
DenoMaterializer, Materializer, MaterializerData, MaterializerDenoModule, Runtime,
};
use crate::types::type_ref::TypeRef;
use crate::types::{NamedTypeRef, Type, TypeDef, TypeDefExt, TypeId, TypeRefBuilder};
use crate::types::{
AsTypeDefEx as _, NamedTypeRef, Type, TypeDef, TypeDefExt, TypeId, TypeRefBuilder,
};
use crate::wit::core::{Policy as CorePolicy, PolicyId, RuntimeId};
use crate::wit::utils::Auth as WitAuth;

Expand Down Expand Up @@ -489,22 +491,22 @@ impl TypeId {
as_variant!(List);

pub fn is_func(&self) -> Result<bool> {
Ok(matches!(self.as_type_def()?, Some(TypeDef::Func(_))))
Ok(matches!(self.as_xdef()?.type_def, TypeDef::Func(_)))
}

pub fn resolve_quant(&self) -> Result<TypeId> {
let type_id = *self;
match type_id.as_type_def()? {
Some(TypeDef::List(a)) => Ok(a.data.of.into()),
Some(TypeDef::Optional(o)) => Ok(o.data.of.into()),
match type_id.as_xdef()?.type_def {
TypeDef::List(a) => Ok(a.data.of.into()),
TypeDef::Optional(o) => Ok(o.data.of.into()),
_ => Ok(type_id),
}
}

pub fn resolve_optional(&self) -> Result<TypeId> {
let type_id = *self;
match type_id.as_type_def()? {
Some(TypeDef::Optional(o)) => Ok(o.data.of.into()),
match type_id.as_xdef()?.type_def {
TypeDef::Optional(o) => Ok(o.data.of.into()),
_ => Ok(type_id),
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/typegraph/core/src/params/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ pub fn build_transform_data(
root_fields: &HashMap<String, raw_tree::ParameterTransformNode>,
) -> Result<TransformData> {
let mut context = TransformDataBuildContext::default();
let (new_tree, type_id) = context.check_object_node(resolver_input, root_fields)?;
let (new_tree, type_id) = context
.check_object_node(resolver_input, root_fields)
.context("building apply transform tree")?;

let mut query_input_type = t::struct_();
for (name, param) in context.query_params.into_iter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub(super) struct ScalarListFilter(pub TypeId);

impl TypeGen for ScalarListFilter {
fn generate(&self, _context: &PrismaContext) -> Result<TypeId> {
if let Some(TypeDef::Optional(_)) = self.0.as_type_def()? {
if let TypeDef::Optional(_) = self.0.as_xdef()?.type_def {
return Err("array of optional not supported".into());
}

Expand Down Expand Up @@ -251,7 +251,7 @@ impl TypeGen for AvgFilter {
.filter_map(|(k, type_id)| {
let type_def = type_id.as_xdef().unwrap().type_def;
let non_opt_type = match type_def {
TypeDef::Optional(inner) => inner.item().as_type_def().unwrap().unwrap(),
TypeDef::Optional(inner) => inner.item().as_xdef().unwrap().type_def,
_ => type_def,
};
match non_opt_type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ root: struct 'Post_query_input' #306
│ │ ├─ [id]: optional '_prisma_sort' #79
│ │ │ └─ item: union #76
│ │ │ ├─ variant_0: struct #75
│ │ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ [name]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ ├─ [id]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ └─ [title]: optional '_prisma_sort' #79
│ └─ item: union #76
│ ├─ variant_0: struct #75
│ │ └─ [sort]: string '_prisma_sort_order' #72
│ └─ variant_1: string '_prisma_sort_order' #72
│ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
├─ [skip]: optional 'optional_291' #292
│ └─ item: integer '_skip' #96
├─ [take]: optional 'optional_289' #290
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ root: struct 'Record_query_input' #114
│ ├─ [age]: optional '_prisma_sort_nullable' #84
│ │ └─ item: union #81
│ │ ├─ variant_0: struct #80
│ │ │ ├─ [nulls]: string '_prisma_nulls_order' #74
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ ├─ [nulls]: string '_prisma_nulls_order' #74 enum{ '"first"', '"last"' }
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ ├─ [created_at]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ ├─ [id]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ └─ [name]: optional '_prisma_sort' #79
│ └─ item: union #76
│ ├─ variant_0: struct #75
│ │ └─ [sort]: string '_prisma_sort_order' #72
│ └─ variant_1: string '_prisma_sort_order' #72
│ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
├─ [skip]: optional 'optional_97' #98
│ └─ item: integer '_skip' #96
├─ [take]: optional 'optional_93' #94
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,40 @@ root: struct 'User_query_input' #217
│ ├─ [id]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ ├─ [name]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ └─ [posts]: optional '_prisma_sort_by_aggregates' #193
│ └─ item: struct #190
│ ├─ [_avg]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ ├─ [_count]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ ├─ [_max]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ ├─ [_min]: optional '_prisma_sort' #79
│ │ └─ item: union #76
│ │ ├─ variant_0: struct #75
│ │ │ └─ [sort]: string '_prisma_sort_order' #72
│ │ └─ variant_1: string '_prisma_sort_order' #72
│ │ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ └─ [_sum]: optional '_prisma_sort' #79
│ └─ item: union #76
│ ├─ variant_0: struct #75
│ │ └─ [sort]: string '_prisma_sort_order' #72
│ └─ variant_1: string '_prisma_sort_order' #72
│ │ └─ [sort]: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
│ └─ variant_1: string '_prisma_sort_order' #72 enum{ '"asc"', '"desc"' }
├─ [skip]: optional 'optional_202' #203
│ └─ item: integer '_skip' #96
├─ [take]: optional 'optional_200' #201
Expand Down
Loading

0 comments on commit 042ae80

Please sign in to comment.