mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-13 10:00:39 +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 .base import BaseOptions, BaseType
|
||||||
from .inputfield import InputField
|
from .inputfield import InputField
|
||||||
from .structures import List, NonNull
|
|
||||||
from .unmountedtype import UnmountedType
|
from .unmountedtype import UnmountedType
|
||||||
from .utils import yank_fields_from_attrs
|
from .utils import yank_fields_from_attrs
|
||||||
|
|
||||||
|
@ -14,7 +13,7 @@ if MYPY:
|
||||||
|
|
||||||
class InputObjectTypeOptions(BaseOptions):
|
class InputObjectTypeOptions(BaseOptions):
|
||||||
fields = None # type: Dict[str, InputField]
|
fields = None # type: Dict[str, InputField]
|
||||||
create_container = None # type: Callable
|
container = None # type: InputObjectTypeContainer
|
||||||
|
|
||||||
|
|
||||||
class InputObjectTypeContainer(dict, BaseType):
|
class InputObjectTypeContainer(dict, BaseType):
|
||||||
|
@ -24,30 +23,11 @@ class InputObjectTypeContainer(dict, BaseType):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
dict.__init__(self, *args, **kwargs)
|
dict.__init__(self, *args, **kwargs)
|
||||||
for key in self._meta.fields.keys():
|
for key in self._meta.fields.keys():
|
||||||
field = getattr(self, key, None)
|
setattr(self, key, self.get(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)
|
|
||||||
|
|
||||||
def __init_subclass__(cls, *args, **kwargs):
|
def __init_subclass__(cls, *args, **kwargs):
|
||||||
pass
|
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):
|
class InputObjectType(UnmountedType, BaseType):
|
||||||
'''
|
'''
|
||||||
|
@ -73,7 +53,8 @@ class InputObjectType(UnmountedType, BaseType):
|
||||||
if container is None:
|
if container is None:
|
||||||
container = type(cls.__name__, (InputObjectTypeContainer, cls), {})
|
container = type(cls.__name__, (InputObjectTypeContainer, cls), {})
|
||||||
_meta.container = container
|
_meta.container = container
|
||||||
super(InputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
|
super(InputObjectType, cls).__init_subclass_with_meta__(
|
||||||
|
_meta=_meta, **options)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_type(cls):
|
def get_type(cls):
|
||||||
|
|
|
@ -39,7 +39,8 @@ def test_enum():
|
||||||
assert graphql_enum.description == 'Description'
|
assert graphql_enum.description == 'Description'
|
||||||
values = graphql_enum.values
|
values = graphql_enum.values
|
||||||
assert 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'),
|
GraphQLEnumValue(name='bar', value=2, description='Description bar=2'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -47,7 +48,8 @@ def test_enum():
|
||||||
def test_objecttype():
|
def test_objecttype():
|
||||||
class MyObjectType(ObjectType):
|
class MyObjectType(ObjectType):
|
||||||
'''Description'''
|
'''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')
|
bar = String(name='gizmo')
|
||||||
|
|
||||||
def resolve_foo(self, bar):
|
def resolve_foo(self, bar):
|
||||||
|
@ -92,8 +94,10 @@ def test_dynamic_objecttype():
|
||||||
def test_interface():
|
def test_interface():
|
||||||
class MyInterface(Interface):
|
class MyInterface(Interface):
|
||||||
'''Description'''
|
'''Description'''
|
||||||
foo = String(bar=String(description='Argument description', default_value='x'), description='Field description')
|
foo = String(bar=String(description='Argument description',
|
||||||
bar = String(name='gizmo', first_arg=String(), other_arg=String(name='oth_arg'))
|
default_value='x'), description='Field description')
|
||||||
|
bar = String(name='gizmo', first_arg=String(),
|
||||||
|
other_arg=String(name='oth_arg'))
|
||||||
own = Field(lambda: MyInterface)
|
own = Field(lambda: MyInterface)
|
||||||
|
|
||||||
def resolve_foo(self, args, info):
|
def resolve_foo(self, args, info):
|
||||||
|
@ -144,12 +148,16 @@ def test_inputobject():
|
||||||
assert graphql_type.name == 'MyInputObjectType'
|
assert graphql_type.name == 'MyInputObjectType'
|
||||||
assert graphql_type.description == 'Description'
|
assert graphql_type.description == 'Description'
|
||||||
|
|
||||||
# Container
|
other_graphql_type = typemap['OtherObjectType']
|
||||||
|
inner_graphql_type = typemap['MyInnerObjectType']
|
||||||
container = graphql_type.create_container({
|
container = graphql_type.create_container({
|
||||||
'bar': 'oh!',
|
'bar': 'oh!',
|
||||||
'baz': {
|
'baz': inner_graphql_type.create_container({
|
||||||
'some_other_field': [{'thingy': 1}, {'thingy': 2}]
|
'some_other_field': [
|
||||||
}
|
other_graphql_type.create_container({'thingy': 1}),
|
||||||
|
other_graphql_type.create_container({'thingy': 2})
|
||||||
|
]
|
||||||
|
})
|
||||||
})
|
})
|
||||||
assert isinstance(container, MyInputObjectType)
|
assert isinstance(container, MyInputObjectType)
|
||||||
assert 'bar' in container
|
assert 'bar' in container
|
||||||
|
|
Loading…
Reference in New Issue
Block a user