Skip to content

Commit

Permalink
Fixed mutation with unbound mutate method. Fixed #311
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Oct 4, 2016
1 parent 02a6c1c commit 999bca8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
2 changes: 2 additions & 0 deletions graphene/types/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import six

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

Expand Down
54 changes: 34 additions & 20 deletions graphene/types/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from ..mutation import Mutation
from ..objecttype import ObjectType
from ..schema import Schema
from ..scalars import String


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


# def test_generate_mutation_with_args():
# class MyMutation(Mutation):
# '''Documentation'''
# class Input:
# s = String()

# @classmethod
# def mutate(cls, *args, **kwargs):
# pass

# graphql_type = MyMutation._meta.graphql_type
# field = MyMutation.Field()
# assert graphql_type.name == "MyMutation"
# assert graphql_type.description == "Documentation"
# assert isinstance(field, Field)
# assert field.type == MyMutation._meta.graphql_type
# assert 's' in field.args
# assert field.args['s'].type == String


def test_generate_mutation_with_meta():
class MyMutation(Mutation):

Expand All @@ -59,3 +41,35 @@ class MyMutation(Mutation):
pass

assert "All mutations must define a mutate method in it" == str(excinfo.value)


def test_mutation_execution():
class CreateUser(Mutation):
class Input:
name = String()

name = String()

def mutate(self, args, context, info):
name = args.get('name')
return CreateUser(name=name)

class Query(ObjectType):
a = String()

class MyMutation(ObjectType):
create_user = CreateUser.Field()

schema = Schema(query=Query, mutation=MyMutation)
result = schema.execute(''' mutation mymutation {
createUser(name:"Peter") {
name
}
}
''')
assert not result.errors
assert result.data == {
'createUser': {
'name': "Peter"
}
}

0 comments on commit 999bca8

Please sign in to comment.