Improved ClientIdMutation

This commit is contained in:
Syrus Akbary 2016-06-14 22:04:57 -07:00
parent 8d4cf2d059
commit ac416b6ab0
3 changed files with 41 additions and 37 deletions

View File

@ -10,39 +10,48 @@ from ..utils.copy_fields import copy_fields
from ..utils.props import props from ..utils.props import props
from ..types.objecttype import ObjectType
from ..utils.is_base_type import is_base_type
from ..types.options import Options
class ClientIDMutationMeta(MutationMeta): class ClientIDMutationMeta(MutationMeta):
_construct_field = False
def get_options(cls, meta): def __new__(cls, name, bases, attrs):
options = cls.options_class( super_new = type.__new__
meta,
# Also ensure initialization is only performed for subclasses of Model
# (excluding Model class itself).
if not is_base_type(bases, ClientIDMutationMeta):
return super_new(cls, name, bases, attrs)
options = Options(
attrs.pop('Meta', None),
name=None, name=None,
abstract=False description=None,
) )
options.graphql_type = None
options.interfaces = []
return options
def construct(cls, bases, attrs): Input = attrs.pop('Input', None)
if not cls._meta.abstract:
Input = attrs.pop('Input', None)
input_fields = props(Input) if Input else {}
cls.mutate_and_get_payload = attrs.pop('mutate_and_get_payload', None) cls = super_new(cls, name, bases, dict(attrs, _meta=options))
input_local_fields = copy_fields(InputField, get_fields(InputObjectType, input_fields, ())) input_fields = props(Input) if Input else {}
local_fields = cls._extract_local_fields(attrs) input_local_fields = copy_fields(InputField, get_fields(InputObjectType, input_fields, ()))
assert cls.mutate_and_get_payload, "{}.mutate_and_get_payload method is required in a ClientIDMutation ObjectType.".format(cls.__name__) output_fields = copy_fields(Field, get_fields(ObjectType, attrs, bases))
field = mutation_with_client_mutation_id(
name=cls._meta.name or cls.__name__, mutate_and_get_payload = getattr(cls, 'mutate_and_get_payload', None)
input_fields=input_local_fields, assert mutate_and_get_payload, "{}.mutate_and_get_payload method is required in a ClientIDMutation ObjectType.".format(cls.__name__)
output_fields=cls._fields(bases, attrs, local_fields),
mutate_and_get_payload=cls.mutate_and_get_payload, field = mutation_with_client_mutation_id(
) name=options.name or cls.__name__,
cls._meta.graphql_type = field.type input_fields=input_local_fields,
cls.Field = partial(Field.copy_and_extend, field, type=field.type, _creation_counter=None) output_fields=output_fields,
constructed = super(ClientIDMutationMeta, cls).construct(bases, attrs) mutate_and_get_payload=cls.mutate_and_get_payload,
return constructed )
options.graphql_type = field.type
cls.Field = partial(Field.copy_and_extend, field, type=field.type, _creation_counter=None)
return cls
class ClientIDMutation(six.with_metaclass(ClientIDMutationMeta, Mutation)): class ClientIDMutation(six.with_metaclass(ClientIDMutationMeta, Mutation)):

View File

@ -10,13 +10,6 @@ from ..utils.is_base_type import is_base_type
class MutationMeta(ObjectTypeMeta): class MutationMeta(ObjectTypeMeta):
_construct_field = True
def construct_field(cls, field_args):
resolver = getattr(cls, 'mutate', None)
assert resolver, 'All mutations must define a mutate method in it'
return partial(Field, cls, args=field_args, resolver=resolver)
def __new__(cls, name, bases, attrs): def __new__(cls, name, bases, attrs):
super_new = super(MutationMeta, cls).__new__ super_new = super(MutationMeta, cls).__new__
@ -28,9 +21,10 @@ class MutationMeta(ObjectTypeMeta):
Input = attrs.pop('Input', None) Input = attrs.pop('Input', None)
cls = super_new(cls, name, bases, attrs) cls = super_new(cls, name, bases, attrs)
if cls._construct_field: field_args = props(Input) if Input else {}
field_args = props(Input) if Input else {} resolver = getattr(cls, 'mutate', None)
cls.Field = cls.construct_field(field_args) assert resolver, 'All mutations must define a mutate method in it'
cls.Field = partial(Field, cls, args=field_args, resolver=resolver)
return cls return cls

View File

@ -8,8 +8,9 @@ def is_graphene_type(_type):
from ..types.interface import Interface from ..types.interface import Interface
from ..types.scalars import Scalar from ..types.scalars import Scalar
from ..types.enum import Enum from ..types.enum import Enum
from ..relay.mutation import ClientIDMutation
if _type in [Interface, InputObjectType, ObjectType, Mutation]: if _type in [Interface, InputObjectType, ObjectType, Mutation, ClientIDMutation]:
return False return False
return inspect.isclass(_type) and issubclass(_type, ( return inspect.isclass(_type) and issubclass(_type, (
Interface, Interface,