Fixed mutation with unbound mutate method. Fixed #311

This commit is contained in:
Syrus Akbary 2016-10-03 19:59:26 -07:00
parent 02a6c1c603
commit 999bca84c9
2 changed files with 36 additions and 20 deletions

View File

@ -3,6 +3,7 @@ from functools import partial
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
@ -22,6 +23,7 @@ class MutationMeta(ObjectTypeMeta):
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

View File

@ -2,6 +2,8 @@ import pytest
from ..mutation import Mutation
from ..objecttype import ObjectType
from ..schema import Schema
from ..scalars import String
def test_generate_mutation_no_args():
@ -17,26 +19,6 @@ def test_generate_mutation_no_args():
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):
@ -59,3 +41,35 @@ def test_mutation_raises_exception_if_no_mutate():
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"
}
}