Skip to content

Commit 9c27db7

Browse files
committed
Handle complex input types
1 parent b5abccb commit 9c27db7

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

graphene/types/inputobjecttype.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from collections import OrderedDict
22

33
from .base import BaseOptions, BaseType
4+
from .field import Field
45
from .inputfield import InputField
6+
from .objecttype import ObjectType
7+
from .scalars import Scalar
8+
from .structures import List, NonNull
59
from .unmountedtype import UnmountedType
610
from .utils import yank_fields_from_attrs
711

8-
912
# For static type checking with Mypy
1013
MYPY = False
1114
if MYPY:
@@ -24,11 +27,29 @@ class Meta:
2427
def __init__(self, *args, **kwargs):
2528
dict.__init__(self, *args, **kwargs)
2629
for key in self._meta.fields.keys():
27-
setattr(self, key, self.get(key, None))
30+
field = getattr(self, key, None)
31+
if field is None or self.get(key, None) is None:
32+
value = None
33+
else:
34+
value = InputObjectTypeContainer._get_typed_field_value(field, self[key])
35+
setattr(self, key, value)
2836

2937
def __init_subclass__(cls, *args, **kwargs):
3038
pass
3139

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

3354
class InputObjectType(UnmountedType, BaseType):
3455
'''

graphene/types/tests/test_typemap.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
GraphQLInputObjectType, GraphQLInterfaceType,
55
GraphQLObjectType, GraphQLString)
66

7+
from ..structures import List, NonNull
78
from ..dynamic import Dynamic
89
from ..enum import Enum
910
from ..field import Field
1011
from ..inputfield import InputField
1112
from ..inputobjecttype import InputObjectType
1213
from ..interface import Interface
1314
from ..objecttype import ObjectType
14-
from ..scalars import String
15+
from ..scalars import String, Int
1516
from ..typemap import TypeMap
1617

1718

@@ -119,10 +120,18 @@ def resolve_foo(self, args, info):
119120

120121

121122
def test_inputobject():
123+
class OtherObjectType(InputObjectType):
124+
thingy = NonNull(Int)
125+
126+
class MyInnerObjectType(InputObjectType):
127+
some_field = String()
128+
some_other_field = List(OtherObjectType)
129+
122130
class MyInputObjectType(InputObjectType):
123131
'''Description'''
124132
foo_bar = String(description='Field description')
125133
bar = String(name='gizmo')
134+
baz = NonNull(MyInnerObjectType)
126135
own = InputField(lambda: MyInputObjectType)
127136

128137
def resolve_foo_bar(self, args, info):
@@ -136,14 +145,23 @@ def resolve_foo_bar(self, args, info):
136145
assert graphql_type.description == 'Description'
137146

138147
# Container
139-
container = graphql_type.create_container({'bar': 'oh!'})
148+
container = graphql_type.create_container({
149+
'bar': 'oh!',
150+
'baz': {
151+
'some_other_field': [{'thingy': 1}, {'thingy': 2}]
152+
}
153+
})
140154
assert isinstance(container, MyInputObjectType)
141155
assert 'bar' in container
142156
assert container.bar == 'oh!'
143157
assert 'foo_bar' not in container
158+
assert container.foo_bar is None
159+
assert container.baz.some_field is None
160+
assert container.baz.some_other_field[0].thingy == 1
161+
assert container.baz.some_other_field[1].thingy == 2
144162

145163
fields = graphql_type.fields
146-
assert list(fields.keys()) == ['fooBar', 'gizmo', 'own']
164+
assert list(fields.keys()) == ['fooBar', 'gizmo', 'baz', 'own']
147165
own_field = fields['own']
148166
assert own_field.type == graphql_type
149167
foo_field = fields['fooBar']

0 commit comments

Comments
 (0)