From 104446124d27187e74544da4cc6ceea3cab7cc19 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Wed, 1 Apr 2020 15:35:13 +0100 Subject: [PATCH 1/3] Fix DjangoModelFormMutation --- graphene_django/forms/mutation.py | 11 +++++ graphene_django/forms/tests/test_mutation.py | 46 ++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/graphene_django/forms/mutation.py b/graphene_django/forms/mutation.py index b7bf0493f..692f8d566 100644 --- a/graphene_django/forms/mutation.py +++ b/graphene_django/forms/mutation.py @@ -162,6 +162,17 @@ def __init_subclass_with_meta__( _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() diff --git a/graphene_django/forms/tests/test_mutation.py b/graphene_django/forms/tests/test_mutation.py index e7dbbdfb4..093f398ce 100644 --- a/graphene_django/forms/tests/test_mutation.py +++ b/graphene_django/forms/tests/test_mutation.py @@ -29,6 +29,12 @@ class Meta: 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 Mutation(ObjectType): name age } + errors { + field + messages + } } } """ @@ -255,6 +265,42 @@ class Mutation(ObjectType): 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: From 5905aae10cc5d516559a488062dbfe7e3ca48a56 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Wed, 1 Apr 2020 16:36:00 +0100 Subject: [PATCH 2/3] Try and fix tests --- graphene_django/forms/tests/test_mutation.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/graphene_django/forms/tests/test_mutation.py b/graphene_django/forms/tests/test_mutation.py index 093f398ce..846a37c89 100644 --- a/graphene_django/forms/tests/test_mutation.py +++ b/graphene_django/forms/tests/test_mutation.py @@ -36,6 +36,12 @@ def clean_age(self): return age +class FilmDetailsForm(forms.ModelForm): + class Meta: + model = FilmDetails + fields = "__all__" + + class PetType(DjangoObjectType): class Meta: model = Pet From 772aa33ae7abfee34018e2a0d5a101d1a9037107 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Sun, 12 Apr 2020 16:27:25 +0100 Subject: [PATCH 3/3] Remove unused form --- graphene_django/forms/tests/test_mutation.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/graphene_django/forms/tests/test_mutation.py b/graphene_django/forms/tests/test_mutation.py index 846a37c89..093f398ce 100644 --- a/graphene_django/forms/tests/test_mutation.py +++ b/graphene_django/forms/tests/test_mutation.py @@ -36,12 +36,6 @@ def clean_age(self): return age -class FilmDetailsForm(forms.ModelForm): - class Meta: - model = FilmDetails - fields = "__all__" - - class PetType(DjangoObjectType): class Meta: model = Pet