1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
import inspect
from functools import partial
from ..utils.module_loading import import_string
from .mountedtype import MountedType
from .unmountedtype import UnmountedType
def get_field_as(value, _as=None):
"""
Get type mounted
"""
if isinstance(value, MountedType):
return value
elif isinstance(value, UnmountedType):
if _as is None:
return value
return _as.mounted(value)
def yank_fields_from_attrs(attrs, _as=None, sort=True):
"""
Extract all the fields in given attributes (dict)
and return them ordered
"""
fields_with_names = []
for attname, value in list(attrs.items()):
field = get_field_as(value, _as)
if not field:
continue
fields_with_names.append((attname, field))
if sort:
fields_with_names = sorted(fields_with_names, key=lambda f: f[1])
return dict(fields_with_names)
def get_type(_type):
if isinstance(_type, str):
return import_string(_type)
if inspect.isfunction(_type) or isinstance(_type, partial):
return _type()
return _type
def get_underlying_type(_type):
"""Get the underlying type even if it is wrapped in structures like NonNull"""
while hasattr(_type, "of_type"):
_type = _type.of_type
return _type
|