Skip to content

Commit 999bca8

Browse files
committed
Fixed mutation with unbound mutate method. Fixed #311
1 parent 02a6c1c commit 999bca8

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

graphene/types/mutation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import six
44

55
from ..utils.is_base_type import is_base_type
6+
from ..utils.get_unbound_function import get_unbound_function
67
from ..utils.props import props
78
from .field import Field
89
from .objecttype import ObjectType, ObjectTypeMeta
@@ -22,6 +23,7 @@ def __new__(cls, name, bases, attrs):
2223
field_args = props(input_class) if input_class else {}
2324
resolver = getattr(cls, 'mutate', None)
2425
assert resolver, 'All mutations must define a mutate method in it'
26+
resolver = get_unbound_function(resolver)
2527
cls.Field = partial(Field, cls, args=field_args, resolver=resolver)
2628
return cls
2729

graphene/types/tests/test_mutation.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from ..mutation import Mutation
44
from ..objecttype import ObjectType
5+
from ..schema import Schema
6+
from ..scalars import String
57

68

79
def test_generate_mutation_no_args():
@@ -17,26 +19,6 @@ def mutate(cls, *args, **kwargs):
1719
assert MyMutation.Field().resolver == MyMutation.mutate
1820

1921

20-
# def test_generate_mutation_with_args():
21-
# class MyMutation(Mutation):
22-
# '''Documentation'''
23-
# class Input:
24-
# s = String()
25-
26-
# @classmethod
27-
# def mutate(cls, *args, **kwargs):
28-
# pass
29-
30-
# graphql_type = MyMutation._meta.graphql_type
31-
# field = MyMutation.Field()
32-
# assert graphql_type.name == "MyMutation"
33-
# assert graphql_type.description == "Documentation"
34-
# assert isinstance(field, Field)
35-
# assert field.type == MyMutation._meta.graphql_type
36-
# assert 's' in field.args
37-
# assert field.args['s'].type == String
38-
39-
4022
def test_generate_mutation_with_meta():
4123
class MyMutation(Mutation):
4224

@@ -59,3 +41,35 @@ class MyMutation(Mutation):
5941
pass
6042

6143
assert "All mutations must define a mutate method in it" == str(excinfo.value)
44+
45+
46+
def test_mutation_execution():
47+
class CreateUser(Mutation):
48+
class Input:
49+
name = String()
50+
51+
name = String()
52+
53+
def mutate(self, args, context, info):
54+
name = args.get('name')
55+
return CreateUser(name=name)
56+
57+
class Query(ObjectType):
58+
a = String()
59+
60+
class MyMutation(ObjectType):
61+
create_user = CreateUser.Field()
62+
63+
schema = Schema(query=Query, mutation=MyMutation)
64+
result = schema.execute(''' mutation mymutation {
65+
createUser(name:"Peter") {
66+
name
67+
}
68+
}
69+
''')
70+
assert not result.errors
71+
assert result.data == {
72+
'createUser': {
73+
'name': "Peter"
74+
}
75+
}

0 commit comments

Comments
 (0)