mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-11-04 01:47:45 +03:00 
			
		
		
		
	Simplified fields implementation
This commit is contained in:
		
							parent
							
								
									018811036b
								
							
						
					
					
						commit
						f53c26c7ab
					
				| 
						 | 
				
			
			@ -2,8 +2,8 @@ import six
 | 
			
		|||
 | 
			
		||||
from ..utils.is_base_type import is_base_type
 | 
			
		||||
from .options import Options
 | 
			
		||||
from .utils import (get_fields_in_type, get_base_fields,
 | 
			
		||||
                    yank_fields_from_attrs, merge)
 | 
			
		||||
from .utils import (yank_fields_from_attrs, get_base_fields,
 | 
			
		||||
                    merge)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AbstractTypeMeta(type):
 | 
			
		||||
| 
						 | 
				
			
			@ -19,10 +19,9 @@ class AbstractTypeMeta(type):
 | 
			
		|||
                # raise Exception('You can only extend AbstractTypes after the base definition.')
 | 
			
		||||
                return type.__new__(cls, name, bases, attrs)
 | 
			
		||||
 | 
			
		||||
        base_fields = get_base_fields(AbstractType, bases)
 | 
			
		||||
        base_fields = get_base_fields(bases, _as=None)
 | 
			
		||||
 | 
			
		||||
        fields = get_fields_in_type(AbstractType, attrs)
 | 
			
		||||
        yank_fields_from_attrs(attrs, fields)
 | 
			
		||||
        fields = yank_fields_from_attrs(attrs, _as=None)
 | 
			
		||||
 | 
			
		||||
        options = Options(
 | 
			
		||||
            fields=merge(base_fields, fields)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,8 +4,8 @@ from ..utils.is_base_type import is_base_type
 | 
			
		|||
from .abstracttype import AbstractTypeMeta
 | 
			
		||||
from .options import Options
 | 
			
		||||
from .unmountedtype import UnmountedType
 | 
			
		||||
from .utils import (get_fields_in_type, yank_fields_from_attrs,
 | 
			
		||||
                    get_base_fields, merge)
 | 
			
		||||
from .utils import yank_fields_from_attrs, get_base_fields, merge
 | 
			
		||||
from .inputfield import InputField
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class InputObjectTypeMeta(AbstractTypeMeta):
 | 
			
		||||
| 
						 | 
				
			
			@ -23,11 +23,10 @@ class InputObjectTypeMeta(AbstractTypeMeta):
 | 
			
		|||
            local_fields=None,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        options.base_fields = get_base_fields(InputObjectType, bases)
 | 
			
		||||
        options.base_fields = get_base_fields(bases, _as=InputField)
 | 
			
		||||
 | 
			
		||||
        if not options.local_fields:
 | 
			
		||||
            options.local_fields = get_fields_in_type(InputObjectType, attrs)
 | 
			
		||||
            yank_fields_from_attrs(attrs, options.local_fields)
 | 
			
		||||
            options.local_fields = yank_fields_from_attrs(attrs, _as=InputField)
 | 
			
		||||
 | 
			
		||||
        options.fields = merge(
 | 
			
		||||
            options.base_fields,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,8 +3,8 @@ import six
 | 
			
		|||
from ..utils.is_base_type import is_base_type
 | 
			
		||||
from .abstracttype import AbstractTypeMeta
 | 
			
		||||
from .options import Options
 | 
			
		||||
from .utils import (get_fields_in_type, yank_fields_from_attrs,
 | 
			
		||||
                    get_base_fields, merge)
 | 
			
		||||
from .utils import yank_fields_from_attrs, get_base_fields, merge
 | 
			
		||||
from .field import Field
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class InterfaceMeta(AbstractTypeMeta):
 | 
			
		||||
| 
						 | 
				
			
			@ -22,11 +22,10 @@ class InterfaceMeta(AbstractTypeMeta):
 | 
			
		|||
            local_fields=None,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        options.base_fields = get_base_fields(Interface, bases)
 | 
			
		||||
        options.base_fields = get_base_fields(bases, _as=Field)
 | 
			
		||||
 | 
			
		||||
        if not options.local_fields:
 | 
			
		||||
            options.local_fields = get_fields_in_type(Interface, attrs)
 | 
			
		||||
            yank_fields_from_attrs(attrs, options.local_fields)
 | 
			
		||||
            options.local_fields = yank_fields_from_attrs(attrs, _as=Field)
 | 
			
		||||
 | 
			
		||||
        options.fields = merge(
 | 
			
		||||
            options.base_fields,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,8 +6,8 @@ from ..utils.is_base_type import is_base_type
 | 
			
		|||
from .abstracttype import AbstractTypeMeta
 | 
			
		||||
from .interface import Interface
 | 
			
		||||
from .options import Options
 | 
			
		||||
from .utils import (get_fields_in_type, yank_fields_from_attrs,
 | 
			
		||||
                    get_base_fields, merge)
 | 
			
		||||
from .utils import yank_fields_from_attrs, get_base_fields, merge
 | 
			
		||||
from .field import Field
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ObjectTypeMeta(AbstractTypeMeta):
 | 
			
		||||
| 
						 | 
				
			
			@ -26,11 +26,10 @@ class ObjectTypeMeta(AbstractTypeMeta):
 | 
			
		|||
            interfaces=(),
 | 
			
		||||
            local_fields=OrderedDict(),
 | 
			
		||||
        )
 | 
			
		||||
        options.base_fields = get_base_fields(ObjectType, bases)
 | 
			
		||||
        options.base_fields = get_base_fields(bases, _as=Field)
 | 
			
		||||
 | 
			
		||||
        if not options.local_fields:
 | 
			
		||||
            options.local_fields = get_fields_in_type(ObjectType, attrs)
 | 
			
		||||
            yank_fields_from_attrs(attrs, options.local_fields)
 | 
			
		||||
            options.local_fields = yank_fields_from_attrs(attrs=attrs, _as=Field)
 | 
			
		||||
 | 
			
		||||
        options.interface_fields = OrderedDict()
 | 
			
		||||
        for interface in options.interfaces:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,19 +6,6 @@ from .inputfield import InputField
 | 
			
		|||
from .unmountedtype import UnmountedType
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def merge_fields_in_attrs(bases, attrs):
 | 
			
		||||
    from ..types import AbstractType, Interface
 | 
			
		||||
    inherited_bases = (AbstractType, Interface)
 | 
			
		||||
    for base in bases:
 | 
			
		||||
        if base in inherited_bases or not issubclass(base, inherited_bases):
 | 
			
		||||
            continue
 | 
			
		||||
        for name, field in base._meta.fields.items():
 | 
			
		||||
            if name in attrs:
 | 
			
		||||
                continue
 | 
			
		||||
            attrs[name] = field
 | 
			
		||||
    return attrs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def merge(*dicts):
 | 
			
		||||
    merged = OrderedDict()
 | 
			
		||||
    for _dict in dicts:
 | 
			
		||||
| 
						 | 
				
			
			@ -26,62 +13,56 @@ def merge(*dicts):
 | 
			
		|||
    return merged
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_base_fields(in_type, bases):
 | 
			
		||||
def get_base_fields(bases, _as=None):
 | 
			
		||||
    fields = OrderedDict()
 | 
			
		||||
    fields = merge_fields_in_attrs(bases, fields)
 | 
			
		||||
    return get_fields_in_type(in_type, fields, order=False)
 | 
			
		||||
    from ..types import AbstractType, Interface
 | 
			
		||||
    # We allow inheritance in AbstractTypes and Interfaces but not ObjectTypes
 | 
			
		||||
    inherited_bases = (AbstractType, Interface)
 | 
			
		||||
    for base in bases:
 | 
			
		||||
        if base in inherited_bases or not issubclass(base, inherited_bases):
 | 
			
		||||
            continue
 | 
			
		||||
        for name, field in base._meta.fields.items():
 | 
			
		||||
            if name in fields:
 | 
			
		||||
                continue
 | 
			
		||||
            fields[name] = get_field_as(field, _as=_as)
 | 
			
		||||
 | 
			
		||||
    return fields
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def unmounted_field_in_type(unmounted_field, type):
 | 
			
		||||
def mount_as(unmounted_field, _as):
 | 
			
		||||
    '''
 | 
			
		||||
    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
 | 
			
		||||
    from ..types.abstracttype import AbstractType
 | 
			
		||||
    from ..types.inputobjecttype import InputObjectType
 | 
			
		||||
    if _as is None:
 | 
			
		||||
        return unmounted_field
 | 
			
		||||
 | 
			
		||||
    if issubclass(type, (ObjectType, Interface)):
 | 
			
		||||
    elif _as is Field:
 | 
			
		||||
        return unmounted_field.Field()
 | 
			
		||||
 | 
			
		||||
    elif issubclass(type, (AbstractType)):
 | 
			
		||||
        return unmounted_field
 | 
			
		||||
    elif issubclass(type, (InputObjectType)):
 | 
			
		||||
    elif _as is InputField:
 | 
			
		||||
        return unmounted_field.InputField()
 | 
			
		||||
 | 
			
		||||
    raise Exception(
 | 
			
		||||
        'Unmounted field "{}" cannot be mounted in {}.'.format(
 | 
			
		||||
            unmounted_field, type
 | 
			
		||||
            unmounted_field, _as
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_field(in_type, value):
 | 
			
		||||
def get_field_as(value, _as=None):
 | 
			
		||||
    if isinstance(value, (Field, InputField, Dynamic)):
 | 
			
		||||
        return value
 | 
			
		||||
    elif isinstance(value, UnmountedType):
 | 
			
		||||
        return unmounted_field_in_type(value, in_type)
 | 
			
		||||
        return mount_as(value, _as)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_fields_in_type(in_type, attrs, order=True):
 | 
			
		||||
def yank_fields_from_attrs(attrs, _as=None):
 | 
			
		||||
    fields_with_names = []
 | 
			
		||||
    for attname, value in list(attrs.items()):
 | 
			
		||||
        field = get_field(in_type, value)
 | 
			
		||||
        field = get_field_as(value, _as)
 | 
			
		||||
        if not field:
 | 
			
		||||
            continue
 | 
			
		||||
        fields_with_names.append((attname, field))
 | 
			
		||||
 | 
			
		||||
    if not order:
 | 
			
		||||
        return OrderedDict(fields_with_names)
 | 
			
		||||
        del attrs[attname]
 | 
			
		||||
 | 
			
		||||
    return OrderedDict(sorted(fields_with_names, key=lambda f: f[1]))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def yank_fields_from_attrs(attrs, fields):
 | 
			
		||||
    for name in fields.keys():
 | 
			
		||||
        del attrs[name]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user