mirror of
https://github.com/graphql-python/graphene.git
synced 2024-12-02 06:24:01 +03:00
81fff0f1b5
* Improve enum compatibility by supporting return enum as well as values and names * Handle invalid enum values * Rough implementation of compat middleware * Move enum middleware into compat module * Fix tests * Tweak enum examples * Add some tests for the middleware * Clean up tests * Add missing imports * Remove enum compat middleware * Use custom dedent function and pin graphql-core to >3.1.2
51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
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
|