Skip to content

Commit

Permalink
Simplified inputobjecttype implementation+tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Nov 15, 2017
1 parent 6dd9e5f commit eb5108f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
27 changes: 4 additions & 23 deletions graphene/types/inputobjecttype.py
Expand Up @@ -2,7 +2,6 @@

from .base import BaseOptions, BaseType
from .inputfield import InputField
from .structures import List, NonNull
from .unmountedtype import UnmountedType
from .utils import yank_fields_from_attrs

Expand All @@ -14,7 +13,7 @@

class InputObjectTypeOptions(BaseOptions):
fields = None # type: Dict[str, InputField]
create_container = None # type: Callable
container = None # type: InputObjectTypeContainer


class InputObjectTypeContainer(dict, BaseType):
Expand All @@ -24,30 +23,11 @@ class Meta:
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
for key in self._meta.fields.keys():
field = getattr(self, key, None)
if field is None or self.get(key, None) is None:
value = None
else:
value = InputObjectTypeContainer._get_typed_field_value(field, self[key])
setattr(self, key, value)
setattr(self, key, self.get(key, None))

def __init_subclass__(cls, *args, **kwargs):
pass

@staticmethod
def _get_typed_field_value(field_or_type, value):
if isinstance(field_or_type, NonNull):
return InputObjectTypeContainer._get_typed_field_value(field_or_type.of_type, value)
elif isinstance(field_or_type, List):
return [
InputObjectTypeContainer._get_typed_field_value(field_or_type.of_type, v)
for v in value
]
elif hasattr(field_or_type, '_meta') and hasattr(field_or_type._meta, 'container'):
return field_or_type._meta.container(value)
else:
return value


class InputObjectType(UnmountedType, BaseType):
'''
Expand All @@ -73,7 +53,8 @@ def __init_subclass_with_meta__(cls, container=None, **options):
if container is None:
container = type(cls.__name__, (InputObjectTypeContainer, cls), {})
_meta.container = container
super(InputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
super(InputObjectType, cls).__init_subclass_with_meta__(
_meta=_meta, **options)

@classmethod
def get_type(cls):
Expand Down
24 changes: 16 additions & 8 deletions graphene/types/tests/test_typemap.py
Expand Up @@ -39,15 +39,17 @@ def deprecation_reason(self):
assert graphql_enum.description == 'Description'
values = graphql_enum.values
assert values == [
GraphQLEnumValue(name='foo', value=1, description='Description foo=1', deprecation_reason='Is deprecated'),
GraphQLEnumValue(name='foo', value=1, description='Description foo=1',
deprecation_reason='Is deprecated'),
GraphQLEnumValue(name='bar', value=2, description='Description bar=2'),
]


def test_objecttype():
class MyObjectType(ObjectType):
'''Description'''
foo = String(bar=String(description='Argument description', default_value='x'), description='Field description')
foo = String(bar=String(description='Argument description',
default_value='x'), description='Field description')
bar = String(name='gizmo')

def resolve_foo(self, bar):
Expand Down Expand Up @@ -92,8 +94,10 @@ class MyObjectType(ObjectType):
def test_interface():
class MyInterface(Interface):
'''Description'''
foo = String(bar=String(description='Argument description', default_value='x'), description='Field description')
bar = String(name='gizmo', first_arg=String(), other_arg=String(name='oth_arg'))
foo = String(bar=String(description='Argument description',
default_value='x'), description='Field description')
bar = String(name='gizmo', first_arg=String(),
other_arg=String(name='oth_arg'))
own = Field(lambda: MyInterface)

def resolve_foo(self, args, info):
Expand Down Expand Up @@ -144,12 +148,16 @@ def resolve_foo_bar(self, args, info):
assert graphql_type.name == 'MyInputObjectType'
assert graphql_type.description == 'Description'

# Container
other_graphql_type = typemap['OtherObjectType']
inner_graphql_type = typemap['MyInnerObjectType']
container = graphql_type.create_container({
'bar': 'oh!',
'baz': {
'some_other_field': [{'thingy': 1}, {'thingy': 2}]
}
'baz': inner_graphql_type.create_container({
'some_other_field': [
other_graphql_type.create_container({'thingy': 1}),
other_graphql_type.create_container({'thingy': 2})
]
})
})
assert isinstance(container, MyInputObjectType)
assert 'bar' in container
Expand Down

0 comments on commit eb5108f

Please sign in to comment.