mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-13 10:00:39 +03:00
Simplified mutation
This commit is contained in:
parent
d8b42dd1ca
commit
2d557d6ed7
|
@ -1,33 +1,58 @@
|
||||||
from functools import partial
|
from collections import OrderedDict
|
||||||
|
|
||||||
import six
|
|
||||||
|
|
||||||
from ..utils.is_base_type import is_base_type
|
|
||||||
from ..utils.get_unbound_function import get_unbound_function
|
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 .utils import yank_fields_from_attrs
|
||||||
|
from .objecttype import ObjectType, ObjectTypeOptions
|
||||||
|
|
||||||
|
from .base import BaseOptions, BaseType
|
||||||
|
|
||||||
|
|
||||||
class MutationMeta(ObjectTypeMeta):
|
class MutationOptions(ObjectTypeOptions):
|
||||||
def __new__(cls, name, bases, attrs):
|
arguments = None # type: Dict[str, Argument]
|
||||||
# Also ensure initialization is only performed for subclasses of
|
output = None # type: Type[ObjectType]
|
||||||
# Mutation
|
resolver = None # type: Function
|
||||||
if not is_base_type(bases, MutationMeta):
|
|
||||||
return type.__new__(cls, name, bases, attrs)
|
|
||||||
|
|
||||||
input_class = attrs.pop('Input', None)
|
|
||||||
|
|
||||||
cls = ObjectTypeMeta.__new__(cls, name, bases, attrs)
|
class Mutation(ObjectType):
|
||||||
field_args = props(input_class) if input_class else {}
|
'''
|
||||||
output_class = getattr(cls, 'Output', cls)
|
Mutation Type Definition
|
||||||
resolver = getattr(cls, 'mutate', None)
|
'''
|
||||||
|
@classmethod
|
||||||
|
def __init_subclass_with_meta__(cls, resolver=None, output=None, arguments=None, **options):
|
||||||
|
_meta = MutationOptions(cls)
|
||||||
|
|
||||||
|
output = output or getattr(cls, 'Output', None)
|
||||||
|
fields = {}
|
||||||
|
if not output:
|
||||||
|
# If output is defined, we don't need to get the fields
|
||||||
|
fields = OrderedDict()
|
||||||
|
for base in reversed(cls.__mro__):
|
||||||
|
fields.update(
|
||||||
|
yank_fields_from_attrs(base.__dict__, _as=Field)
|
||||||
|
)
|
||||||
|
output = cls
|
||||||
|
|
||||||
|
if not arguments:
|
||||||
|
input_class = getattr(cls, 'Input', None)
|
||||||
|
if input_class:
|
||||||
|
arguments = props(input_class)
|
||||||
|
else:
|
||||||
|
arguments = {}
|
||||||
|
|
||||||
|
resolver = resolver or get_unbound_function(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, output_class, args=field_args, resolver=resolver)
|
|
||||||
return cls
|
|
||||||
|
|
||||||
|
_meta.fields = fields
|
||||||
|
_meta.output = output
|
||||||
|
_meta.resolver = resolver
|
||||||
|
_meta.arguments = arguments
|
||||||
|
|
||||||
class Mutation(six.with_metaclass(MutationMeta, ObjectType)):
|
super(Mutation, cls).__init_subclass_with_meta__(_meta=_meta, **options)
|
||||||
pass
|
|
||||||
|
@classmethod
|
||||||
|
def Field(cls, *args, **kwargs):
|
||||||
|
return Field(
|
||||||
|
cls._meta.output, args=cls._meta.arguments, resolver=cls._meta.resolver
|
||||||
|
)
|
||||||
|
|
|
@ -23,7 +23,8 @@ class ObjectType(BaseType):
|
||||||
have a name, but most importantly describe their fields.
|
have a name, but most importantly describe their fields.
|
||||||
'''
|
'''
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass_with_meta__(cls, interfaces=(), possible_types=(), default_resolver=None, **options):
|
def __init_subclass_with_meta__(cls, interfaces=(), possible_types=(), default_resolver=None, _meta=None, **options):
|
||||||
|
if not _meta:
|
||||||
_meta = ObjectTypeOptions(cls)
|
_meta = ObjectTypeOptions(cls)
|
||||||
|
|
||||||
fields = OrderedDict()
|
fields = OrderedDict()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user