Skip to content

Commit

Permalink
Simplified InputObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jul 12, 2017
1 parent 5ee6e2b commit d8b42dd
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions graphene/types/inputobjecttype.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
import six
from collections import OrderedDict

from ..utils.is_base_type import is_base_type
from ..utils.trim_docstring import trim_docstring
from .abstracttype import AbstractTypeMeta
from .inputfield import InputField
from .options import Options
from .unmountedtype import UnmountedType
from .utils import get_base_fields, merge, yank_fields_from_attrs
from .utils import yank_fields_from_attrs

from .base import BaseOptions, BaseType

class InputObjectTypeMeta(AbstractTypeMeta):

def __new__(cls, name, bases, attrs):
# Also ensure initialization is only performed for subclasses of
# InputObjectType
if not is_base_type(bases, InputObjectTypeMeta):
return type.__new__(cls, name, bases, attrs)
class InputObjectTypeOptions(BaseOptions):
fields = None # type: Dict[str, Field]

options = Options(
attrs.pop('Meta', None),
name=name,
description=trim_docstring(attrs.get('__doc__')),
local_fields=None,
)

options.base_fields = get_base_fields(bases, _as=InputField)

if not options.local_fields:
options.local_fields = yank_fields_from_attrs(attrs, _as=InputField)

options.fields = merge(
options.base_fields,
options.local_fields
)
return type.__new__(cls, name, bases, dict(attrs, _meta=options))

def __str__(cls): # noqa: N802
return cls._meta.name


class InputObjectType(six.with_metaclass(InputObjectTypeMeta, UnmountedType)):
class InputObjectType(UnmountedType, BaseType):
'''
Input Object Type Definition
Expand All @@ -49,6 +21,19 @@ class InputObjectType(six.with_metaclass(InputObjectTypeMeta, UnmountedType)):
Using `NonNull` will ensure that a value must be provided by the query
'''

@classmethod
def __init_subclass_with_meta__(cls, **options):
_meta = InputObjectTypeOptions(cls)

fields = OrderedDict()
for base in reversed(cls.__mro__):
fields.update(
yank_fields_from_attrs(base.__dict__, _as=InputField)
)

_meta.fields = fields
super(InputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)

@classmethod
def get_type(cls):
'''
Expand Down

0 comments on commit d8b42dd

Please sign in to comment.