Skip to content

Commit 966ff1a

Browse files
authored
feat: Remove obsolete restrictions on prisma (#592)
#### Motivation and context Since v5, where on unique queries exposes all the fields, not just unique fields. Reference: https://www.prisma.io/docs/orm/reference/prisma-client-reference#filter-on-non-unique-fields-with-userwhereuniqueinput #### Migration notes <!-- Explain HOW users should update their code when required --> ### Checklist - [x] The change come with new or modified tests - [x] Hard-to-understand functions have explanatory comments - [ ] End-user documentation is updated to reflect the change
1 parent 0461df1 commit 966ff1a

16 files changed

+4251
-2720
lines changed

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,5 +2081,3 @@ Make `with_store` and `with_store_mut` private in `global_store` module (<a href
20812081

20822082

20832083
</details>
2084-
2085-

typegate/tests/runtimes/prisma/prisma_edgecases_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Meta.test("prisma critical edgecases", async (t) => {
7575
id
7676
}
7777
}
78-
`.expectErrorContains("Unexpected property 'firstname'")
78+
`.expectErrorContains("Expected a minimum of 1 fields of (id, pseudo)")
7979
.on(e);
8080
});
8181
});

typegate/tests/runtimes/prisma/prisma_test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ Meta.test("prisma", async (t) => {
3030
.on(e);
3131
});
3232

33+
await t.should("accept non unique fields", async () => {
34+
await gql`
35+
query {
36+
findRecord(where: {
37+
id: "b7831fd1-799d-4b20-9a84-830588f750a1"
38+
age: { gte: 18 }
39+
}) {
40+
id
41+
}
42+
}
43+
`
44+
.expectData({
45+
findRecord: null,
46+
})
47+
.on(e);
48+
});
49+
3350
await t.should("return no data when empty", async () => {
3451
await gql`
3552
query {

typegate/tests/runtimes/typegate/__snapshots__/typegate_runtime_test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ snapshot[`typegate: find available operations 1`] = `
1616
format: null,
1717
optional: true,
1818
runtime: "prisma",
19-
title: "QueryUniquerecordWhereInput",
19+
title: "QueryrecordWhereUniqueInput",
2020
type: "object",
2121
},
2222
},
@@ -198,7 +198,7 @@ snapshot[`typegate: find available operations 1`] = `
198198
format: null,
199199
optional: false,
200200
runtime: "prisma",
201-
title: "_100_recordWithNestedCount[]",
201+
title: "_103_recordWithNestedCount[]",
202202
type: "list",
203203
},
204204
outputItem: {
@@ -291,7 +291,7 @@ snapshot[`typegate: find available operations 1`] = `
291291
format: null,
292292
optional: true,
293293
runtime: "prisma",
294-
title: "QueryUniqueusersWhereInput",
294+
title: "QueryusersWhereUniqueInput",
295295
type: "object",
296296
},
297297
},
@@ -331,7 +331,7 @@ snapshot[`typegate: find available operations 1`] = `
331331
format: null,
332332
optional: false,
333333
runtime: "prisma",
334-
title: "_241_user_identityWithNestedCount_excluding___rel_user_identity_users_1[]",
334+
title: "_279_user_identityWithNestedCount_excluding___rel_user_identity_users_1[]",
335335
type: "list",
336336
},
337337
},
@@ -379,7 +379,7 @@ snapshot[`typegate: find available operations 1`] = `
379379
format: null,
380380
optional: false,
381381
runtime: "prisma",
382-
title: "_243_messagesWithNestedCount_excluding_messageSender[]",
382+
title: "_281_messagesWithNestedCount_excluding_messageSender[]",
383383
type: "list",
384384
},
385385
},
@@ -396,7 +396,7 @@ snapshot[`typegate: find available operations 1`] = `
396396
format: null,
397397
optional: true,
398398
runtime: "prisma",
399-
title: "integer_244",
399+
title: "integer_282",
400400
type: "integer",
401401
},
402402
},
@@ -413,7 +413,7 @@ snapshot[`typegate: find available operations 1`] = `
413413
format: null,
414414
optional: true,
415415
runtime: "prisma",
416-
title: "integer_244",
416+
title: "integer_282",
417417
type: "integer",
418418
},
419419
},
@@ -523,7 +523,7 @@ snapshot[`typegate: find available operations 1`] = `
523523
format: null,
524524
optional: false,
525525
runtime: "prisma",
526-
title: "_276_messagesWithNestedCount[]",
526+
title: "_314_messagesWithNestedCount[]",
527527
type: "list",
528528
},
529529
outputItem: {

typegraph/core/src/runtimes/prisma/type_generation/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use self::group_by::GroupByResult;
1717
use self::input_type::InputType;
1818
use self::out_type::OutType;
1919
use self::query_input_type::QueryInputType;
20-
use self::query_unique_where_expr::QueryUniqueWhereExpr;
2120
use self::query_where_expr::QueryWhereExpr;
2221
use self::with_nested_count::WithNestedCount;
2322

@@ -37,7 +36,6 @@ mod input_type;
3736
mod order_by;
3837
mod out_type;
3938
pub mod query_input_type;
40-
mod query_unique_where_expr;
4139
mod query_where_expr;
4240
mod where_;
4341
mod with_nested_count;
@@ -173,7 +171,7 @@ impl PrismaOperation for FindUnique {
173171
t::struct_()
174172
.propx(
175173
"where",
176-
t::optional(context.generate(&QueryUniqueWhereExpr::new(model_id))?),
174+
t::optional(context.generate(&QueryWhereExpr::new(model_id).unique())?),
177175
)?
178176
.build()
179177
}
@@ -281,7 +279,7 @@ impl PrismaOperation for UpdateOne {
281279
.prop("data", context.generate(&InputType::for_update(model_id))?)
282280
.prop(
283281
"where",
284-
context.generate(&QueryUniqueWhereExpr::new(model_id))?,
282+
context.generate(&QueryWhereExpr::new(model_id).unique())?,
285283
)
286284
.build()
287285
}
@@ -310,7 +308,7 @@ impl PrismaOperation for UpsertOne {
310308
t::struct_()
311309
.prop(
312310
"where",
313-
context.generate(&QueryUniqueWhereExpr::new(model_id))?,
311+
context.generate(&QueryWhereExpr::new(model_id).unique())?,
314312
)
315313
.prop(
316314
"create",
@@ -332,7 +330,7 @@ impl PrismaOperation for DeleteOne {
332330
t::struct_()
333331
.prop(
334332
"where",
335-
context.generate(&QueryUniqueWhereExpr::new(model_id))?,
333+
context.generate(&QueryWhereExpr::new(model_id).unique())?,
336334
)
337335
.build()
338336
}

typegraph/core/src/runtimes/prisma/type_generation/query_unique_where_expr.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

typegraph/core/src/runtimes/prisma/type_generation/query_where_expr.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,35 @@ use super::{where_::Where, TypeGen};
99

1010
pub struct QueryWhereExpr {
1111
model_id: TypeId,
12+
unique: bool,
1213
}
1314

1415
impl QueryWhereExpr {
1516
pub fn new(model_id: TypeId) -> Self {
16-
Self { model_id }
17+
Self {
18+
model_id,
19+
unique: false,
20+
}
21+
}
22+
23+
#[allow(dead_code)]
24+
pub fn unique(mut self) -> Self {
25+
self.unique = true;
26+
self
1727
}
1828
}
1929

30+
// TODO require at least one unique field for unique queries
31+
// Currently, this is not supported by `t.struct()`
32+
//
33+
// https://www.prisma.io/docs/orm/reference/prisma-client-reference#filter-on-non-unique-fields-with-userwhereuniqueinput
34+
//
35+
// ```
36+
// if model.id_fields.contains(k) || prop.unique {
37+
// // ...
38+
// }
39+
// ```
40+
2041
impl TypeGen for QueryWhereExpr {
2142
fn generate(&self, context: &PrismaContext) -> Result<TypeId> {
2243
let where_type = context.generate(&Where::new(self.model_id))?;
@@ -40,7 +61,11 @@ impl TypeGen for QueryWhereExpr {
4061
}
4162

4263
fn name(&self) -> String {
43-
format!("Query{}WhereInput", self.model_id.name().unwrap().unwrap())
64+
let unique = if self.unique { "Unique" } else { "" };
65+
format!(
66+
"Query{}Where{unique}Input",
67+
self.model_id.name().unwrap().unwrap()
68+
)
4469
}
4570
}
4671

0 commit comments

Comments
 (0)