From 0f279abecff6aea1cd03defe9b226b9aa438b562 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sun, 7 Aug 2016 16:31:06 -0700 Subject: [PATCH] Improved unmounted type logic --- graphene/types/unmountedtype.py | 21 --------------------- graphene/utils/get_fields.py | 30 ++++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/graphene/types/unmountedtype.py b/graphene/types/unmountedtype.py index d2978b2a..b3a20f55 100644 --- a/graphene/types/unmountedtype.py +++ b/graphene/types/unmountedtype.py @@ -57,24 +57,3 @@ class UnmountedType(OrderedType): _creation_counter=self.creation_counter, **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 diff --git a/graphene/utils/get_fields.py b/graphene/utils/get_fields.py index 4e18de67..ca35895f 100644 --- a/graphene/utils/get_fields.py +++ b/graphene/utils/get_fields.py @@ -6,14 +6,32 @@ from .get_graphql_type import get_graphql_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): for attname, value in list(attrs.items()): - is_field = isinstance(value, (Field, InputField)) - is_field_proxy = isinstance(value, UnmountedType) - if not (is_field or is_field_proxy): - continue - field = value.as_mounted(in_type) if is_field_proxy else value - yield attname, field + if isinstance(value, (Field, InputField)): + yield attname, value + elif isinstance(value, UnmountedType): + yield attname, unmounted_field_in_type(value, in_type) def get_fields_from_bases_and_types(bases, types):