Skip to content

Commit eb5108f

Browse files
committed
Simplified inputobjecttype implementation+tests
1 parent 6dd9e5f commit eb5108f

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

graphene/types/inputobjecttype.py

+4-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from .base import BaseOptions, BaseType
44
from .inputfield import InputField
5-
from .structures import List, NonNull
65
from .unmountedtype import UnmountedType
76
from .utils import yank_fields_from_attrs
87

@@ -14,7 +13,7 @@
1413

1514
class InputObjectTypeOptions(BaseOptions):
1615
fields = None # type: Dict[str, InputField]
17-
create_container = None # type: Callable
16+
container = None # type: InputObjectTypeContainer
1817

1918

2019
class InputObjectTypeContainer(dict, BaseType):
@@ -24,30 +23,11 @@ class Meta:
2423
def __init__(self, *args, **kwargs):
2524
dict.__init__(self, *args, **kwargs)
2625
for key in self._meta.fields.keys():
27-
field = getattr(self, key, None)
28-
if field is None or self.get(key, None) is None:
29-
value = None
30-
else:
31-
value = InputObjectTypeContainer._get_typed_field_value(field, self[key])
32-
setattr(self, key, value)
26+
setattr(self, key, self.get(key, None))
3327

3428
def __init_subclass__(cls, *args, **kwargs):
3529
pass
3630

37-
@staticmethod
38-
def _get_typed_field_value(field_or_type, value):
39-
if isinstance(field_or_type, NonNull):
40-
return InputObjectTypeContainer._get_typed_field_value(field_or_type.of_type, value)
41-
elif isinstance(field_or_type, List):
42-
return [
43-
InputObjectTypeContainer._get_typed_field_value(field_or_type.of_type, v)
44-
for v in value
45-
]
46-
elif hasattr(field_or_type, '_meta') and hasattr(field_or_type._meta, 'container'):
47-
return field_or_type._meta.container(value)
48-
else:
49-
return value
50-
5131

5232
class InputObjectType(UnmountedType, BaseType):
5333
'''
@@ -73,7 +53,8 @@ def __init_subclass_with_meta__(cls, container=None, **options):
7353
if container is None:
7454
container = type(cls.__name__, (InputObjectTypeContainer, cls), {})
7555
_meta.container = container
76-
super(InputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
56+
super(InputObjectType, cls).__init_subclass_with_meta__(
57+
_meta=_meta, **options)
7758

7859
@classmethod
7960
def get_type(cls):

graphene/types/tests/test_typemap.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ def deprecation_reason(self):
3939
assert graphql_enum.description == 'Description'
4040
values = graphql_enum.values
4141
assert values == [
42-
GraphQLEnumValue(name='foo', value=1, description='Description foo=1', deprecation_reason='Is deprecated'),
42+
GraphQLEnumValue(name='foo', value=1, description='Description foo=1',
43+
deprecation_reason='Is deprecated'),
4344
GraphQLEnumValue(name='bar', value=2, description='Description bar=2'),
4445
]
4546

4647

4748
def test_objecttype():
4849
class MyObjectType(ObjectType):
4950
'''Description'''
50-
foo = String(bar=String(description='Argument description', default_value='x'), description='Field description')
51+
foo = String(bar=String(description='Argument description',
52+
default_value='x'), description='Field description')
5153
bar = String(name='gizmo')
5254

5355
def resolve_foo(self, bar):
@@ -92,8 +94,10 @@ class MyObjectType(ObjectType):
9294
def test_interface():
9395
class MyInterface(Interface):
9496
'''Description'''
95-
foo = String(bar=String(description='Argument description', default_value='x'), description='Field description')
96-
bar = String(name='gizmo', first_arg=String(), other_arg=String(name='oth_arg'))
97+
foo = String(bar=String(description='Argument description',
98+
default_value='x'), description='Field description')
99+
bar = String(name='gizmo', first_arg=String(),
100+
other_arg=String(name='oth_arg'))
97101
own = Field(lambda: MyInterface)
98102

99103
def resolve_foo(self, args, info):
@@ -144,12 +148,16 @@ def resolve_foo_bar(self, args, info):
144148
assert graphql_type.name == 'MyInputObjectType'
145149
assert graphql_type.description == 'Description'
146150

147-
# Container
151+
other_graphql_type = typemap['OtherObjectType']
152+
inner_graphql_type = typemap['MyInnerObjectType']
148153
container = graphql_type.create_container({
149154
'bar': 'oh!',
150-
'baz': {
151-
'some_other_field': [{'thingy': 1}, {'thingy': 2}]
152-
}
155+
'baz': inner_graphql_type.create_container({
156+
'some_other_field': [
157+
other_graphql_type.create_container({'thingy': 1}),
158+
other_graphql_type.create_container({'thingy': 2})
159+
]
160+
})
153161
})
154162
assert isinstance(container, MyInputObjectType)
155163
assert 'bar' in container

0 commit comments

Comments
 (0)