Skip to content

Commit

Permalink
fix: small fixes (#486)
Browse files Browse the repository at this point in the history
Co-authored-by: Natoandro <[email protected]>
  • Loading branch information
michael-0acf4 and Natoandro authored Nov 22, 2023
1 parent 5ad95df commit d3428fd
Show file tree
Hide file tree
Showing 15 changed files with 866 additions and 781 deletions.
22 changes: 13 additions & 9 deletions typegate/tests/runtimes/prisma/full_prisma_mapping_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,32 @@ Meta.test("prisma full mapping", async (t) => {
.on(e);
});

await t.should("find the first item", async () => {
await gql`
await t.should(
"find first item after a skip starting from a cursor",
async () => {
await gql`
query {
findFirstPost(
where: {
title: { contains: "Title" }
},
skip: 1,
cursor: { id: 10004 },
orderBy: [ {title: "desc"} ]
) {
id
title
}
}
`.expectData({
findFirstPost: {
id: 10004,
title: "Some Title 4",
},
})
.on(e);
});
findFirstPost: {
id: 10005,
title: "Some Title 4",
},
})
.on(e);
},
);

await t.should(
"work with reduce syntax and find the first item",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Skip;

impl TypeGen for Skip {
fn generate(&self, _context: &PrismaContext) -> Result<TypeId> {
t::integer().x_min(0).named(self.name()).build()
t::integer().min(0).named(self.name()).build()
}

fn name(&self) -> String {
Expand Down Expand Up @@ -57,3 +57,40 @@ impl TypeGen for Distinct {
format!("_KeysOf_{model_name}")
}
}

pub struct Cursor {
model_id: TypeId,
}

impl Cursor {
pub fn new(model_id: TypeId) -> Self {
Self { model_id }
}
}

impl TypeGen for Cursor {
fn generate(&self, context: &PrismaContext) -> Result<TypeId> {
let model = context.model(self.model_id)?;
let model = model.borrow();

let fields = model
.iter_props()
.filter_map(|(k, prop)| match prop {
Property::Scalar(t) => Some((k.to_string(), t.type_id)),
_ => None,
})
.collect::<Vec<(String, TypeId)>>();

let mut variants = vec![];
for (k, id) in fields {
let variant = t::struct_().prop(k, id).build()?;
variants.push(variant)
}
t::union(variants).named(self.name()).build()
}

fn name(&self) -> String {
let model_name = self.model_id.type_name().unwrap().unwrap();
format!("_{}_Cursor", model_name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

use super::{
additional_filters::{Distinct, Skip, Take},
additional_filters::{Cursor, Distinct, Skip, Take},
group_by::{GroupingFields, Having},
order_by::OrderBy,
query_where_expr::QueryWhereExpr,
Expand Down Expand Up @@ -49,7 +49,11 @@ impl TypeGen for QueryInputType {
)?
.propx("orderBy", t::optional(context.generate(&order_by)?))?
.propx("take", t::optional(context.generate(&Take)?))?
.propx("skip", t::optional(context.generate(&Skip)?))?;
.propx("skip", t::optional(context.generate(&Skip)?))?
.propx(
"cursor",
t::optional(context.generate(&Cursor::new(self.model_id))?),
)?;

if self.is_group_by {
builder.prop("by", context.generate(&GroupingFields::new(self.model_id))?);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
source: typegraph/core/src/runtimes/prisma/type_generation/mod.rs
expression: "tree::print(inp)"
---
root: struct '_Post_QueryInput' #146
├─ [where]: optional '_134_QueryPostWhereInput?' #134
│ └─ item: struct 'QueryPostWhereInput' #133
│ ├─ [id]: optional '_113__integer_filter_c?' #113
root: struct '_Post_QueryInput' #159
├─ [where]: optional '_143_QueryPostWhereInput?' #143
│ └─ item: struct 'QueryPostWhereInput' #142
│ ├─ [id]: optional '_122__integer_filter_c?' #122
│ │ └─ item: optional '_integer_filter_c' #34
│ │ └─ item: union #33
│ │ ├─ variant_0: either '_integer_filter' #31
Expand Down Expand Up @@ -51,7 +51,7 @@ root: struct '_Post_QueryInput' #146
│ │ └─ variant_5: struct #30
│ │ └─ [notIn]: list #25
│ │ └─ item: integer #23
│ ├─ [title]: optional '_114__string_filter_c?' #114
│ ├─ [title]: optional '_123__string_filter_c?' #123
│ │ └─ item: optional '_string_filter_c' #20
│ │ └─ item: union #19
│ │ ├─ variant_0: union '_string_filter' #17
Expand Down Expand Up @@ -101,9 +101,9 @@ root: struct '_Post_QueryInput' #146
│ │ │ └─ item: string #5
│ │ └─ [endsWith]: optional #6
│ │ └─ item: string #5
│ ├─ [author]: optional '_127_UserWhere__skip_72_PostWhere?' #127
│ │ └─ item: struct 'UserWhere__skip_72_PostWhere' #126
│ │ ├─ [id]: optional '_115__integer_filter_c?' #115
│ ├─ [author]: optional '_136_UserWhere__skip_77_PostWhere?' #136
│ │ └─ item: struct 'UserWhere__skip_77_PostWhere' #135
│ │ ├─ [id]: optional '_124__integer_filter_c?' #124
│ │ │ └─ item: optional '_integer_filter_c' #34
│ │ │ └─ item: union #33
│ │ │ ├─ variant_0: either '_integer_filter' #31
Expand Down Expand Up @@ -149,7 +149,7 @@ root: struct '_Post_QueryInput' #146
│ │ │ └─ variant_5: struct #30
│ │ │ └─ [notIn]: list #25
│ │ │ └─ item: integer #23
│ │ ├─ [name]: optional '_116__string_filter_c?' #116
│ │ ├─ [name]: optional '_125__string_filter_c?' #125
│ │ │ └─ item: optional '_string_filter_c' #20
│ │ │ └─ item: union #19
│ │ │ ├─ variant_0: union '_string_filter' #17
Expand Down Expand Up @@ -199,28 +199,28 @@ root: struct '_Post_QueryInput' #146
│ │ │ │ └─ item: string #5
│ │ │ └─ [endsWith]: optional #6
│ │ │ └─ item: string #5
│ │ └─ [posts]: optional #125
│ │ └─ item: union #124
│ │ ├─ variant_0: struct #119
│ │ │ └─ [every]: optional '_118_PostWhere?' #118
│ │ │ └─ item: &PostWhere #117
│ │ ├─ variant_1: struct #121
│ │ │ └─ [some]: optional '_120_PostWhere?' #120
│ │ │ └─ item: &PostWhere #117
│ │ └─ variant_2: struct #123
│ │ └─ [none]: optional '_122_PostWhere?' #122
│ │ └─ item: &PostWhere #117
│ ├─ [AND]: optional '_131__130_QueryPostWhereInput[]?' #131
│ │ └─ item: list '_130_QueryPostWhereInput[]' #130
│ │ └─ item: &QueryPostWhereInput #129
│ ├─ [OR]: optional '_131__130_QueryPostWhereInput[]?' #131
│ │ └─ item: list '_130_QueryPostWhereInput[]' #130
│ │ └─ item: &QueryPostWhereInput #129
│ └─ [NOT]: optional '_132_QueryPostWhereInput?' #132
│ └─ item: &QueryPostWhereInput #129
├─ [orderBy]: optional '_140__Post_OrderBy?' #140
│ └─ item: list '_Post_OrderBy' #139
│ └─ item: struct #138
│ │ └─ [posts]: optional #134
│ │ └─ item: union #133
│ │ ├─ variant_0: struct #128
│ │ │ └─ [every]: optional '_127_PostWhere?' #127
│ │ │ └─ item: &PostWhere #126
│ │ ├─ variant_1: struct #130
│ │ │ └─ [some]: optional '_129_PostWhere?' #129
│ │ │ └─ item: &PostWhere #126
│ │ └─ variant_2: struct #132
│ │ └─ [none]: optional '_131_PostWhere?' #131
│ │ └─ item: &PostWhere #126
│ ├─ [AND]: optional '_140__139_QueryPostWhereInput[]?' #140
│ │ └─ item: list '_139_QueryPostWhereInput[]' #139
│ │ └─ item: &QueryPostWhereInput #138
│ ├─ [OR]: optional '_140__139_QueryPostWhereInput[]?' #140
│ │ └─ item: list '_139_QueryPostWhereInput[]' #139
│ │ └─ item: &QueryPostWhereInput #138
│ └─ [NOT]: optional '_141_QueryPostWhereInput?' #141
│ └─ item: &QueryPostWhereInput #138
├─ [orderBy]: optional '_149__Post_OrderBy?' #149
│ └─ item: list '_Post_OrderBy' #148
│ └─ item: struct #147
│ ├─ [id]: optional '_Sort' #47
│ │ └─ item: union #46
│ │ ├─ variant_0: struct #45
Expand All @@ -231,9 +231,9 @@ root: struct '_Post_QueryInput' #146
│ │ ├─ variant_0: struct #45
│ │ │ └─ [sort]: string '_SortOrder' #43 enum{ '"asc"', '"desc"' }
│ │ └─ variant_1: string '_SortOrder' #43 enum{ '"asc"', '"desc"' }
│ └─ [author]: optional '_137__User_OrderBy_excluding___rel_Post_User_1?' #137
│ └─ item: list '_User_OrderBy_excluding___rel_Post_User_1' #136
│ └─ item: struct #135
│ └─ [author]: optional '_146__User_OrderBy_excluding___rel_Post_User_1?' #146
│ └─ item: list '_User_OrderBy_excluding___rel_Post_User_1' #145
│ └─ item: struct #144
│ ├─ [id]: optional '_Sort' #47
│ │ └─ item: union #46
│ │ ├─ variant_0: struct #45
Expand All @@ -244,11 +244,17 @@ root: struct '_Post_QueryInput' #146
│ ├─ variant_0: struct #45
│ │ └─ [sort]: string '_SortOrder' #43 enum{ '"asc"', '"desc"' }
│ └─ variant_1: string '_SortOrder' #43 enum{ '"asc"', '"desc"' }
├─ [take]: optional '_141__Take?' #141
├─ [take]: optional '_150__Take?' #150
│ └─ item: integer '_Take' #54
├─ [skip]: optional '_142__Skip?' #142
├─ [skip]: optional '_151__Skip?' #151
│ └─ item: integer '_Skip' #56
└─ [distinct]: optional '_145__KeysOf_Post?' #145
└─ item: list '_KeysOf_Post' #144
└─ item: string #143 enum{ '"id"', '"title"', '"author"' }
├─ [cursor]: optional '_155__Post_Cursor?' #155
│ └─ item: union '_Post_Cursor' #154
│ ├─ variant_0: struct #152
│ │ └─ [id]: integer #74
│ └─ variant_1: struct #153
│ └─ [title]: string #75
└─ [distinct]: optional '_158__KeysOf_Post?' #158
└─ item: list '_KeysOf_Post' #157
└─ item: string #156 enum{ '"id"', '"title"', '"author"' }

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
source: typegraph/core/src/runtimes/prisma/type_generation/mod.rs
expression: "tree::print(out)"
---
root: list '_148_PostWithNestedCount[]' #148
└─ item: struct 'PostWithNestedCount' #147
├─ [id]: integer #69
├─ [title]: string #70
└─ [author]: struct 'User' #68
├─ [id]: integer #64
├─ [name]: string #65
└─ [posts]: list '_67_Post[]' #67
└─ item: &Post #66
root: list '_161_PostWithNestedCount[]' #161
└─ item: struct 'PostWithNestedCount' #160
├─ [id]: integer #74
├─ [title]: string #75
└─ [author]: struct 'User' #73
├─ [id]: integer #69
├─ [name]: string #70
└─ [posts]: list '_72_Post[]' #72
└─ item: &Post #71

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: typegraph/core/src/runtimes/prisma/type_generation/mod.rs
expression: "tree::print(inp)"
---
root: struct '_Record_QueryInput' #61
root: struct '_Record_QueryInput' #66
├─ [where]: optional '_42_QueryRecordWhereInput?' #42
│ └─ item: struct 'QueryRecordWhereInput' #41
│ ├─ [id]: optional '_21__string_filter_c?' #21
Expand Down Expand Up @@ -182,7 +182,15 @@ root: struct '_Record_QueryInput' #61
│ └─ item: integer '_Take' #54
├─ [skip]: optional '_57__Skip?' #57
│ └─ item: integer '_Skip' #56
└─ [distinct]: optional '_60__KeysOf_Record?' #60
└─ item: list '_KeysOf_Record' #59
└─ item: string #58 enum{ '"id"', '"name"', '"age"' }
├─ [cursor]: optional '_62__Record_Cursor?' #62
│ └─ item: union '_Record_Cursor' #61
│ ├─ variant_0: struct #58
│ │ └─ [id]: string #0
│ ├─ variant_1: struct #59
│ │ └─ [name]: string #1
│ └─ variant_2: struct #60
│ └─ [age]: integer #2
└─ [distinct]: optional '_65__KeysOf_Record?' #65
└─ item: list '_KeysOf_Record' #64
└─ item: string #63 enum{ '"id"', '"name"', '"age"' }

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
source: typegraph/core/src/runtimes/prisma/type_generation/mod.rs
expression: "tree::print(out)"
---
root: list '_63_RecordWithNestedCount[]' #63
└─ item: struct 'RecordWithNestedCount' #62
root: list '_68_RecordWithNestedCount[]' #68
└─ item: struct 'RecordWithNestedCount' #67
├─ [id]: string #0
├─ [name]: string #1
└─ [age]: optional #3
Expand Down
Loading

0 comments on commit d3428fd

Please sign in to comment.