Django: Map Form to Mutation
See original GitHub issueCurrently it’s possible to map Django models to ObjectType
s using DjangoNode
. I think it would be nice to have a similar feature that allows mapping forms to Mutation
with a DjangoMutation
class.
This would provide several great improvements:
- No need to duplicate form fields in
Input
classes anymore. - There would be a simple and obvious way to handle validation.
- The code could map
form.errors
toinfo.context.errors
(It took a while until I figured out how to do that) on it’s own.
In the simple case you’d only have to define the outputs and a method that takes the validated data and returns an instance of the mutation class. I believe that even that would be unnecessary, if you’re working with ModelForm
s. In that case a default method could just save the form and return the model instance wrapped in the corresponding DjangoNode
.
Code that gets us some of the way to such a class already appears to exist, which was written for the django-filter integration.
I’m not that familiar with the code base itself but I have read quite a bit of it and I’ think it might be able to implement this. I’m interested to know what you think of it and whether this has a chance to be accepted.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:14
- Comments:12 (6 by maintainers)
Top GitHub Comments
Hi what is the status on this?
I need something like a UserInputType for a IntroduceUser mutation and i don’t want to manually map all the fields of the Django User model.
I would like if there would be a DjangoInputObjectType that could be created similar to the DjangoObjectType. Passing it through Django Forms is a brilliant solution to handling field validation, but being able to bypass validation would be appreciated for bulk data imports from a trusted system.
@stefanfoulis
ErrorType
is simple graphene object type for representing errors:However now I took different approach to error handling. Just raising ValidationError in mutation handler and adding error messages to
errors
object in overriddenGraphQLView.format_error
method: