mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 12:16:58 +03:00
Improved fields logic
This commit is contained in:
parent
e6788993ba
commit
a6346664a3
|
@ -1,9 +1,12 @@
|
|||
import six
|
||||
|
||||
from graphql import GraphQLInterfaceType
|
||||
from .definitions import FieldsMeta, ClassTypeMeta, GrapheneGraphQLType
|
||||
from .definitions import GrapheneGraphQLType
|
||||
from .options import Options
|
||||
from ..utils.is_base_type import is_base_type
|
||||
from ..utils.get_fields import get_fields
|
||||
from ..utils.copy_fields import copy_fields
|
||||
|
||||
|
||||
class GrapheneInterfaceType(GrapheneGraphQLType, GraphQLInterfaceType):
|
||||
pass
|
||||
|
@ -27,14 +30,12 @@ class InterfaceTypeMeta(type):
|
|||
abstract=False
|
||||
)
|
||||
|
||||
from ..utils.get_fields import get_fields
|
||||
|
||||
fields = get_fields(Interface, attrs, bases)
|
||||
attrs = attrs_without_fields(attrs, fields)
|
||||
cls = super_new(cls, name, bases, dict(attrs, _meta=options))
|
||||
|
||||
if not options.graphql_type:
|
||||
fields = copy_fields(fields)
|
||||
fields = copy_fields(fields, parent=cls)
|
||||
options.graphql_type = GrapheneInterfaceType(
|
||||
graphene_type=cls,
|
||||
name=options.name or cls.__name__,
|
||||
|
@ -43,30 +44,16 @@ class InterfaceTypeMeta(type):
|
|||
)
|
||||
else:
|
||||
assert not fields, "Can't mount Fields in an Interface with a defined graphql_type"
|
||||
fields = copy_fields(options.graphql_type.get_fields())
|
||||
fields = copy_fields(options.graphql_type.get_fields(), parent=cls)
|
||||
|
||||
for attname, field in fields.items():
|
||||
field.parent = cls
|
||||
# setattr(cls, field.name, field)
|
||||
for name, field in fields.items():
|
||||
setattr(cls, field.attname or name, field)
|
||||
|
||||
return cls
|
||||
|
||||
|
||||
def attrs_without_fields(attrs, fields):
|
||||
fields_names = fields.keys()
|
||||
return {k: v for k, v in attrs.items() if k not in fields_names}
|
||||
|
||||
|
||||
def copy_fields(fields):
|
||||
from collections import OrderedDict
|
||||
from .field import Field
|
||||
|
||||
_fields = []
|
||||
for attname, field in fields.items():
|
||||
field = Field.copy_and_extend(field, attname=attname)
|
||||
_fields.append(field)
|
||||
|
||||
return OrderedDict((f.name, f) for f in sorted(_fields))
|
||||
return {k: v for k, v in attrs.items() if k not in fields}
|
||||
|
||||
|
||||
class Interface(six.with_metaclass(InterfaceTypeMeta)):
|
||||
|
|
11
graphene/utils/copy_fields.py
Normal file
11
graphene/utils/copy_fields.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
from collections import OrderedDict
|
||||
from ..types.field import Field
|
||||
|
||||
|
||||
def copy_fields(fields, **extra):
|
||||
_fields = []
|
||||
for attname, field in fields.items():
|
||||
field = Field.copy_and_extend(field, attname=attname, **extra)
|
||||
_fields.append(field)
|
||||
|
||||
return OrderedDict((f.name, f) for f in _fields)
|
|
@ -45,4 +45,4 @@ def get_fields(in_type, attrs, bases):
|
|||
|
||||
fields.extend(local_fields)
|
||||
|
||||
return OrderedDict(fields)
|
||||
return OrderedDict(sorted(fields, key=lambda kv: kv[1]))
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import inspect
|
||||
from ..types.objecttype import ObjectType
|
||||
from ..types.inputobjecttype import InputObjectType
|
||||
from ..types.interface import Interface
|
||||
from ..types.scalars import Scalar
|
||||
from ..types.enum import Enum
|
||||
|
||||
|
||||
def is_graphene_type(_type):
|
||||
from ..types.objecttype import ObjectType
|
||||
from ..types.inputobjecttype import InputObjectType
|
||||
from ..types.interface import Interface
|
||||
from ..types.scalars import Scalar
|
||||
from ..types.enum import Enum
|
||||
|
||||
if _type in [Interface]:
|
||||
return False
|
||||
return inspect.isclass(_type) and issubclass(_type, (
|
||||
|
|
Loading…
Reference in New Issue
Block a user