Improved unmounted type logic

This commit is contained in:
Syrus Akbary 2016-08-07 16:31:06 -07:00
parent 73d37a1bb2
commit 0f279abecf
2 changed files with 24 additions and 27 deletions

View File

@ -57,24 +57,3 @@ class UnmountedType(OrderedType):
_creation_counter=self.creation_counter, _creation_counter=self.creation_counter,
**self.kwargs **self.kwargs
) )
def as_mounted(self, cls):
'''
Mount the UnmountedType dinamically as Field or InputField
depending on the class is mounted in.
ObjectType -> Field
InputObjectType -> InputField
'''
from .inputobjecttype import InputObjectType
from .objecttype import ObjectType
from .interface import Interface
if issubclass(cls, (ObjectType, Interface)):
inner = self.as_field()
elif issubclass(cls, (InputObjectType)):
inner = self.as_inputfield()
else:
raise Exception('TypedProxy "{}" cannot be mounted in {}'.format(self.get_type(), cls))
return inner

View File

@ -6,14 +6,32 @@ from .get_graphql_type import get_graphql_type
from .is_graphene_type import is_graphene_type from .is_graphene_type import is_graphene_type
def unmounted_field_in_type(unmounted_field, type):
'''
Mount the UnmountedType dinamically as Field or InputField
depending on where mounted in.
ObjectType -> Field
InputObjectType -> InputField
'''
from ..types.inputobjecttype import InputObjectType
from ..types.objecttype import ObjectType
from ..types.interface import Interface
if issubclass(type, (ObjectType, Interface)):
return unmounted_field.as_field()
elif issubclass(type, (InputObjectType)):
return unmounted_field.as_inputfield()
raise Exception('Unmounted field "{}" cannot be mounted in {}'.format(self.get_type(), type))
def get_fields_from_attrs(in_type, attrs): def get_fields_from_attrs(in_type, attrs):
for attname, value in list(attrs.items()): for attname, value in list(attrs.items()):
is_field = isinstance(value, (Field, InputField)) if isinstance(value, (Field, InputField)):
is_field_proxy = isinstance(value, UnmountedType) yield attname, value
if not (is_field or is_field_proxy): elif isinstance(value, UnmountedType):
continue yield attname, unmounted_field_in_type(value, in_type)
field = value.as_mounted(in_type) if is_field_proxy else value
yield attname, field
def get_fields_from_bases_and_types(bases, types): def get_fields_from_bases_and_types(bases, types):