Skip to content

Commit

Permalink
fix: prisma where string types
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Feb 5, 2025
1 parent 47d1005 commit 0f71084
Show file tree
Hide file tree
Showing 31 changed files with 2,560 additions and 1,954 deletions.
25 changes: 11 additions & 14 deletions src/typegate/src/engine/typecheck/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import {
export function generateValidator(tg: TypeGraph, typeIdx: number): Validator {
const validatorName = (typeIdx: number) =>
`validate_${tg.type(typeIdx).title.replace(/[^1-9a-zA-Z_$]/g, "_")}`;
const validatorCode = new InputValidationCompiler(tg, validatorName)
.generate(typeIdx);
const validatorCode = new InputValidationCompiler(tg, validatorName).generate(
typeIdx,
);

const validator = new Function(validatorCode)() as ValidatorFn;
return (value: unknown) => {
const errors: ErrorEntry[] = [];
validator(value, "<value>", errors, validationContext);
if (errors.length > 0) {
const messages = errors.map(([path, msg]) => ` - at ${path}: ${msg}\n`)
const messages = errors
.map(([path, msg]) => ` - at ${path}: ${msg}\n`)
.join("");
throw new Error(
`Validation errors on ${tg.type(typeIdx).title}:\n${messages}`,
Expand Down Expand Up @@ -86,23 +88,15 @@ function filterDeclaredFields(
const result = {} as Record<string, unknown>;
for (const [field, idx] of explicitlyDeclared) {
const nextNode = tg.type(idx);
result[field] = filterDeclaredFields(
tg,
value?.[field],
nextNode,
);
result[field] = filterDeclaredFields(tg, value?.[field], nextNode);
}
return result;
}
case "optional":
if (value === undefined || value === null) {
return null;
}
return filterDeclaredFields(
tg,
value,
tg.type(node.item),
);
return filterDeclaredFields(tg, value, tg.type(node.item));
default:
return value;
}
Expand All @@ -112,7 +106,10 @@ export class InputValidationCompiler {
codes: Map<number, string> = new Map();
#getFunctionName: (idx: number) => string;

constructor(private tg: TypeGraph, getFunctionName: (idx: number) => string) {
constructor(
private tg: TypeGraph,
getFunctionName: (idx: number) => string,
) {
this.#getFunctionName = getFunctionName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ impl TypeGen for NumberFilter {
}
}

pub(super) struct StringFilter;
pub(super) struct StringFilter {
pub type_id: TypeId,
}

impl TypeGen for StringFilter {
fn generate(&self, context: &PrismaContext) -> Result<TypeId> {
let type_id = t::string().build()?;
let type_id = self.type_id;
let opt_type_id = t::optional(type_id).build()?;
let list_type_id = t::list(type_id).build()?;

Expand All @@ -141,7 +143,7 @@ impl TypeGen for StringFilter {
}

fn name(&self, _context: &PrismaContext) -> Result<String> {
Ok("_prisma_string_filter".to_string())
Ok(format!("_prisma_string_filter{}", self.type_id.0))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@
source: src/typegraph/core/src/runtimes/prisma/type_generation/mod.rs
expression: tp.print(inp)
---
root: struct 'Post_query_input' #243
├─ [cursor]: optional #236
│ └─ item: union 'Post_cursor' #235
│ ├─ variant_0: struct #232
│ │ └─ [id]: integer #119
│ └─ variant_1: struct #233
│ └─ [title]: string #120
├─ [distinct]: optional #241
│ └─ item: list 'Post_keys_union' #240
│ └─ item: string #238 enum{ '"id"', '"title"', '"author"' }
├─ [orderBy]: optional #228
│ └─ item: list 'Post_order_by' #227
│ └─ item: struct #225
│ ├─ [author]: optional #224
│ │ └─ item: list 'User_order_by_withoutrel_Post_User' #223
│ │ └─ item: struct #221
│ │ ├─ [id]: &_prisma_sort #63
│ │ └─ [name]: &_prisma_sort #63
│ ├─ [id]: &_prisma_sort #63
│ └─ [title]: &_prisma_sort #63
├─ [skip]: optional #230
│ └─ item: &_skip #87
├─ [take]: optional #229
│ └─ item: &_take #83
└─ [where]: optional #218
└─ item: struct 'Post_query_where_input' #217
├─ [AND]: optional #214
│ └─ item: list #213
│ └─ item: &Post_query_where_input #212
├─ [NOT]: optional #215
│ └─ item: &Post_query_where_input #212
├─ [OR]: optional #214
│ └─ item: list #213
│ └─ item: &Post_query_where_input #212
├─ [author]: optional #209
│ └─ item: struct 'User_where_excluding_Post' #208
│ ├─ [id]: optional #196
│ │ └─ item: &_prisma_integer_filter_ex #35
│ ├─ [name]: optional #197
│ │ └─ item: &_prisma_string_filter_ex #13
│ └─ [posts]: optional #206
│ └─ item: union #205
│ ├─ variant_0: struct #200
│ │ └─ [every]: optional #199
│ │ └─ item: &Post_where #198
│ ├─ variant_1: struct #202
│ │ └─ [some]: optional #201
│ │ └─ item: &Post_where #198
│ └─ variant_2: struct #204
│ └─ [none]: optional #203
│ └─ item: &Post_where #198
├─ [id]: optional #193
│ └─ item: &_prisma_integer_filter_ex #35
└─ [title]: optional #194
└─ item: &_prisma_string_filter_ex #13
root: struct 'Post_query_input' #283
├─ [cursor]: optional #276
│ └─ item: union 'Post_cursor' #275
│ ├─ variant_0: struct #272
│ │ └─ [id]: integer #159
│ └─ variant_1: struct #273
│ └─ [title]: string #160
├─ [distinct]: optional #281
│ └─ item: list 'Post_keys_union' #280
│ └─ item: string #278 enum{ '"id"', '"title"', '"author"' }
├─ [orderBy]: optional #268
│ └─ item: list 'Post_order_by' #267
│ └─ item: struct #265
│ ├─ [author]: optional #264
│ │ └─ item: list 'User_order_by_withoutrel_Post_User' #263
│ │ └─ item: struct #261
│ │ ├─ [id]: &_prisma_sort #103
│ │ └─ [name]: &_prisma_sort #103
│ ├─ [id]: &_prisma_sort #103
│ └─ [title]: &_prisma_sort #103
├─ [skip]: optional #270
│ └─ item: &_skip #127
├─ [take]: optional #269
│ └─ item: &_take #123
└─ [where]: optional #258
└─ item: struct 'Post_query_where_input' #257
├─ [AND]: optional #254
│ └─ item: list #253
│ └─ item: &Post_query_where_input #252
├─ [NOT]: optional #255
│ └─ item: &Post_query_where_input #252
├─ [OR]: optional #254
│ └─ item: list #253
│ └─ item: &Post_query_where_input #252
├─ [author]: optional #249
│ └─ item: struct 'User_where_excluding_Post' #248
│ ├─ [id]: optional #236
│ │ └─ item: &_prisma_integer_filter_ex #55
│ ├─ [name]: optional #237
│ │ └─ item: &_prisma_string_filter_ex #34
│ └─ [posts]: optional #246
│ └─ item: union #245
│ ├─ variant_0: struct #240
│ │ └─ [every]: optional #239
│ │ └─ item: &Post_where #238
│ ├─ variant_1: struct #242
│ │ └─ [some]: optional #241
│ │ └─ item: &Post_where #238
│ └─ variant_2: struct #244
│ └─ [none]: optional #243
│ └─ item: &Post_where #238
├─ [id]: optional #233
│ └─ item: &_prisma_integer_filter_ex #55
└─ [title]: optional #234
└─ item: &_prisma_string_filter_ex #34
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
source: src/typegraph/core/src/runtimes/prisma/type_generation/mod.rs
expression: tp.print(out)
---
root: list #247
└─ item: struct 'Post_with_nested_count' #246
├─ [author]: &User #121
├─ [id]: integer #117
└─ [title]: string #120
root: list #287
└─ item: struct 'Post_with_nested_count' #286
├─ [author]: &User #161
├─ [id]: integer #157
└─ [title]: string #160
Loading

0 comments on commit 0f71084

Please sign in to comment.