mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-08 23:50:38 +03:00
Simplified inputobjecttype implementation+tests
This commit is contained in:
parent
6dd9e5ff1f
commit
eb5108f81e
|
@ -2,7 +2,6 @@ from collections import OrderedDict
|
|||
|
||||
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
|
||||
|
||||
|
@ -14,7 +13,7 @@ if MYPY:
|
|||
|
||||
class InputObjectTypeOptions(BaseOptions):
|
||||
fields = None # type: Dict[str, InputField]
|
||||
create_container = None # type: Callable
|
||||
container = None # type: InputObjectTypeContainer
|
||||
|
||||
|
||||
class InputObjectTypeContainer(dict, BaseType):
|
||||
|
@ -24,30 +23,11 @@ class InputObjectTypeContainer(dict, BaseType):
|
|||
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):
|
||||
'''
|
||||
|
@ -73,7 +53,8 @@ class InputObjectType(UnmountedType, BaseType):
|
|||
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):
|
||||
|
|
|
@ -39,7 +39,8 @@ def test_enum():
|
|||
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'),
|
||||
]
|
||||
|
||||
|
@ -47,7 +48,8 @@ def test_enum():
|
|||
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):
|
||||
|
@ -92,8 +94,10 @@ def test_dynamic_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):
|
||||
|
@ -144,12 +148,16 @@ def test_inputobject():
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user