Replies: 2 comments 11 replies
-
+1 on modeling the errors explicitly. I'm not sure the error type needs to be tied to the type PydanticInputValidationError {
fieldName: String!
errorMessage: String!
}
type UserInputErrors {
nonFieldErrors: [String!]!
fieldErrors: [PydanticInputValidationError!]!
} (names are TBD) It's possible that it would be valuable to be able to define custom input error types per model but I can't think of a valid use case for that at the moment. Also it is always possible to just create the types by hand if they are needed. Did you have a specific use case in mind @patrick91 ? One thing to note is that Pydantic has support for quite complex validation on things like Lists so having just a plain [
{
"loc": [
"friends",
2
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
] https://pydantic-docs.helpmanual.io/ Minor thing but in your python example @patrick91 I think you've referred to the wrong type in the return |
Beta Was this translation helpful? Give feedback.
-
From #453 (comment)
@AndrewIngram we discussed this a bit on discord, and we were thinking of doing something like this: user = UserType(instance=User(name="Jonathan Kim"), additional_attributes="Not my name") Similar to django forms. I'm not 100% sure on this yet, because there's the additional complexity of input types. For example if we wan't to access a user attribute I guess can do this:
for input types it is different I guess, let's say that we do this:
how would I access the input data?
The other option is to just register pydantic types, ie making some sort of copies (for inputs and types). And then use them as if the were pydantic types, but I don't like that as you can't use strawberry.field for changing a fields name for example. Do you have any suggestion? |
Beta Was this translation helpful? Give feedback.
-
Hi folks!
I'm thinking of starting using Pydantic for data validation in our work project and I'm consindering adding support for it into Strawberry[1]. I don't know yet how big this project will be, as I haven't seen much of Pydantic's source code.
The reason why I'd like to have support for Pydantic is to prevent duplication with strawberry input types and pydantic types.
In terms of API I was thinking at something like this:
let's go through each of these one by one, first we have the
UserModel
class, this is the model that we will be using to auto generate type, input type and an error type.Our
User
class is a GraphQL object type,UserInput
is a GraphQL input type, and, finally,UserInputError
is another GraphQL object type that we can use to wrap validation errors.Let's do a small digression on errors, why do we need a new type when GraphQL already supports errors?
Basically the idea is to not use GraphQL errors for expected errors (ie. validation errors, item not found, etc) but instead to return a new type that has information about the error (for example a string for each input field, some metadata, etc), this is all based from this blog post: https://medium.com/@sachee/200-ok-error-handling-in-graphql-7ec869aec9bc
So it would be nice to generate types to encapsulate errors. This is the GraphQL schema we'd get from the classes above:
The
UserInputError
has errors wrapped inside afieldErrors
to prevent name clashing with the output field (when returingUnion[User, UserInputErrors]
).We might also have
nonFieldErrors
for errors that don't belong to a particular field.This is how you'd use it:
we are hiding the data validation a bit, but maybe that's fine, any thoughts?
[1] I think we will do the same thing with Django in future.
Beta Was this translation helpful? Give feedback.
All reactions