mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 19:43:56 +03:00
Fixed mutation with unbound mutate method. Fixed #311
This commit is contained in:
parent
02a6c1c603
commit
999bca84c9
|
@ -3,6 +3,7 @@ from functools import partial
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from ..utils.is_base_type import is_base_type
|
from ..utils.is_base_type import is_base_type
|
||||||
|
from ..utils.get_unbound_function import get_unbound_function
|
||||||
from ..utils.props import props
|
from ..utils.props import props
|
||||||
from .field import Field
|
from .field import Field
|
||||||
from .objecttype import ObjectType, ObjectTypeMeta
|
from .objecttype import ObjectType, ObjectTypeMeta
|
||||||
|
@ -22,6 +23,7 @@ class MutationMeta(ObjectTypeMeta):
|
||||||
field_args = props(input_class) if input_class else {}
|
field_args = props(input_class) if input_class else {}
|
||||||
resolver = getattr(cls, 'mutate', None)
|
resolver = getattr(cls, 'mutate', None)
|
||||||
assert resolver, 'All mutations must define a mutate method in it'
|
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)
|
cls.Field = partial(Field, cls, args=field_args, resolver=resolver)
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ import pytest
|
||||||
|
|
||||||
from ..mutation import Mutation
|
from ..mutation import Mutation
|
||||||
from ..objecttype import ObjectType
|
from ..objecttype import ObjectType
|
||||||
|
from ..schema import Schema
|
||||||
|
from ..scalars import String
|
||||||
|
|
||||||
|
|
||||||
def test_generate_mutation_no_args():
|
def test_generate_mutation_no_args():
|
||||||
|
@ -17,26 +19,6 @@ def test_generate_mutation_no_args():
|
||||||
assert MyMutation.Field().resolver == MyMutation.mutate
|
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():
|
def test_generate_mutation_with_meta():
|
||||||
class MyMutation(Mutation):
|
class MyMutation(Mutation):
|
||||||
|
|
||||||
|
@ -59,3 +41,35 @@ def test_mutation_raises_exception_if_no_mutate():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
assert "All mutations must define a mutate method in it" == str(excinfo.value)
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user