Fix DjangoModelFormMutation (#915)

* Fix DjangoModelFormMutation

* Try and fix tests

* Remove unused form
This commit is contained in:
Jonathan Kim 2020-04-12 20:01:30 +01:00 committed by GitHub
parent 9d9a14c36d
commit 481d3ff35d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -162,6 +162,17 @@ class DjangoModelFormMutation(BaseDjangoFormMutation):
_meta=_meta, input_fields=input_fields, **options
)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
form = cls.get_form(root, info, **input)
if form.is_valid():
return cls.perform_mutate(form, info)
else:
errors = ErrorType.from_errors(form.errors)
return cls(errors=errors)
@classmethod
def perform_mutate(cls, form, info):
obj = form.save()

View File

@ -29,6 +29,12 @@ class PetForm(forms.ModelForm):
model = Pet
fields = "__all__"
def clean_age(self):
age = self.cleaned_data["age"]
if age >= 99:
raise ValidationError("Too old")
return age
class PetType(DjangoObjectType):
class Meta:
@ -243,6 +249,10 @@ class ModelFormMutationTests(TestCase):
name
age
}
errors {
field
messages
}
}
}
"""
@ -255,6 +265,42 @@ class ModelFormMutationTests(TestCase):
self.assertEqual(pet.name, "Mia")
self.assertEqual(pet.age, 10)
def test_model_form_mutation_invalid_input(self):
class PetMutation(DjangoModelFormMutation):
pet = Field(PetType)
class Meta:
form_class = PetForm
class Mutation(ObjectType):
pet_mutation = PetMutation.Field()
schema = Schema(query=MockQuery, mutation=Mutation)
result = schema.execute(
""" mutation PetMutation {
petMutation(input: { name: "Mia", age: 99 }) {
pet {
name
age
}
errors {
field
messages
}
}
}
"""
)
self.assertIs(result.errors, None)
self.assertEqual(result.data["petMutation"]["pet"], None)
self.assertEqual(
result.data["petMutation"]["errors"],
[{"field": "age", "messages": ["Too old"],}],
)
self.assertEqual(Pet.objects.count(), 0)
def test_model_form_mutation_mutate_invalid_form(self):
class PetMutation(DjangoModelFormMutation):
class Meta: