Can the ErrorsPlugin
be applied to validation of scalars used as input args?
#495
-
If I use the However, if I use scalars for my arguments this no longer seems to hold true. Here's an example. For brevity I have not reproduced, here, the code to flatten Zod errors and create the const max5Schema = z.number().int().max(5);
builder.queryType({
fields: (t) => ({
list: t.boolean({
errors: { types: [ZodError] },
nullable: true,
args: {
max5: t.arg.int({
validate: {
schema: max5Schema,
},
}),
},
resolve: () => true,
}),
}),
}); If I then query, passing in {
"data": {
"list": {
"fieldErrors": [
{
"message": "Number must be less than or equal to 5",
"path": [
"max5"
]
}
]
}
}
} This is great, and exactly what I want from the response 🎉 However, if I wanted to present the builder.scalarType('Max5', {
serialize: (n) => n,
parseValue: max5Schema.parse,
});
builder.queryType({
fields: (t) => ({
list: t.boolean({
errors: { types: [ZodError] },
nullable: true,
args: {
max5: t.arg({
type: 'Max5',
}),
},
resolve: () => true,
}),
}),
}); The validation is still performed, and the Zod error is still thrown if the supplied value is not valid, but I no longer have the Zod error handled and my response is the {
"errors": [
{
"message": "Variable \"$max5\" got invalid value 6; Expected type \"Max5\". [\n {\n \"code\": \"too_big\",\n \"maximum\": 5,\n \"type\": \"number\",\n \"inclusive\": true,\n \"message\": \"Number must be less than or equal to 5\",\n \"path\": []\n }\n]",
"extensions": {
"code": "BAD_USER_INPUT",
"exception": {
"stacktrace": [
"ZodError: [",
" {",
" \"code\": \"too_big\",",
" \"maximum\": 5,",
" \"type\": \"number\",",
" \"inclusive\": true,",
" \"message\": \"Number must be less than or equal to 5\",",
" \"path\": []",
" }",
"]",
" at handleResult ... full stack trace blah, blah, blah..."
]
}
}
}
]
} Is there any way around this? There doesn't seem to be anywhere else that I'm able to place the Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Unfortunately I don't know of a good way to do this. Scalar validation is handle by graphql, and this happens at a completely different phase of execution, which means we can't catch the errors in the resolver. Pothos doesn't have a way to hook in or intercept this. One possible option might be to allow a custom validation option on scalar definitions that the does run during the resolver like other validation functions, and have the default parseValue function be more permissive. |
Beta Was this translation helpful? Give feedback.
Unfortunately I don't know of a good way to do this. Scalar validation is handle by graphql, and this happens at a completely different phase of execution, which means we can't catch the errors in the resolver. Pothos doesn't have a way to hook in or intercept this. One possible option might be to allow a custom validation option on scalar definitions that the does run during the resolver like other validation functions, and have the default parseValue function be more permissive.