Skip to content

Commit 2350963

Browse files
protasovsejkimbo
andauthored
Update mutation.py (#811)
* Update mutation.py * Add tests Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
1 parent a12fc92 commit 2350963

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

graphene_django/forms/mutation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def mutate_and_get_payload(cls, root, info, **input):
4747
else:
4848
errors = ErrorType.from_errors(form.errors)
4949

50-
return cls(errors=errors)
50+
return cls(errors=errors, **form.data)
5151

5252
@classmethod
5353
def get_form(cls, root, info, **input):
@@ -100,7 +100,7 @@ def __init_subclass_with_meta__(
100100
@classmethod
101101
def perform_mutate(cls, form, info):
102102
form.save()
103-
return cls(errors=[])
103+
return cls(errors=[], **form.cleaned_data)
104104

105105

106106
class DjangoModelDjangoFormMutationOptions(DjangoFormMutationOptions):

graphene_django/forms/tests/test_mutation.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django import forms
22
from django.test import TestCase
3+
from django.core.exceptions import ValidationError
34
from py.test import raises
45

56
from graphene import ObjectType, Schema, String, Field
@@ -13,6 +14,15 @@
1314
class MyForm(forms.Form):
1415
text = forms.CharField()
1516

17+
def clean_text(self):
18+
text = self.cleaned_data["text"]
19+
if text == "INVALID_INPUT":
20+
raise ValidationError("Invalid input")
21+
return text
22+
23+
def save(self):
24+
pass
25+
1626

1727
class PetForm(forms.ModelForm):
1828
class Meta:
@@ -83,6 +93,64 @@ class MockQuery(ObjectType):
8393
a = String()
8494

8595

96+
class FormMutationTests(TestCase):
97+
def test_form_invalid_form(self):
98+
class MyMutation(DjangoFormMutation):
99+
class Meta:
100+
form_class = MyForm
101+
102+
class Mutation(ObjectType):
103+
my_mutation = MyMutation.Field()
104+
105+
schema = Schema(query=MockQuery, mutation=Mutation)
106+
107+
result = schema.execute(
108+
""" mutation MyMutation {
109+
myMutation(input: { text: "INVALID_INPUT" }) {
110+
errors {
111+
field
112+
messages
113+
}
114+
text
115+
}
116+
}
117+
"""
118+
)
119+
120+
self.assertIs(result.errors, None)
121+
self.assertEqual(
122+
result.data["myMutation"]["errors"],
123+
[{"field": "text", "messages": ["Invalid input"]}],
124+
)
125+
126+
def test_form_valid_input(self):
127+
class MyMutation(DjangoFormMutation):
128+
class Meta:
129+
form_class = MyForm
130+
131+
class Mutation(ObjectType):
132+
my_mutation = MyMutation.Field()
133+
134+
schema = Schema(query=MockQuery, mutation=Mutation)
135+
136+
result = schema.execute(
137+
""" mutation MyMutation {
138+
myMutation(input: { text: "VALID_INPUT" }) {
139+
errors {
140+
field
141+
messages
142+
}
143+
text
144+
}
145+
}
146+
"""
147+
)
148+
149+
self.assertIs(result.errors, None)
150+
self.assertEqual(result.data["myMutation"]["errors"], [])
151+
self.assertEqual(result.data["myMutation"]["text"], "VALID_INPUT")
152+
153+
86154
class ModelFormMutationTests(TestCase):
87155
def test_default_meta_fields(self):
88156
class PetMutation(DjangoModelFormMutation):

0 commit comments

Comments
 (0)