Improved fields logic

This commit is contained in:
Syrus Akbary 2016-06-12 00:08:25 -07:00
parent e6788993ba
commit a6346664a3
4 changed files with 27 additions and 28 deletions

View File

@ -1,9 +1,12 @@
import six import six
from graphql import GraphQLInterfaceType from graphql import GraphQLInterfaceType
from .definitions import FieldsMeta, ClassTypeMeta, GrapheneGraphQLType from .definitions import GrapheneGraphQLType
from .options import Options from .options import Options
from ..utils.is_base_type import is_base_type 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): class GrapheneInterfaceType(GrapheneGraphQLType, GraphQLInterfaceType):
pass pass
@ -27,14 +30,12 @@ class InterfaceTypeMeta(type):
abstract=False abstract=False
) )
from ..utils.get_fields import get_fields
fields = get_fields(Interface, attrs, bases) fields = get_fields(Interface, attrs, bases)
attrs = attrs_without_fields(attrs, fields) attrs = attrs_without_fields(attrs, fields)
cls = super_new(cls, name, bases, dict(attrs, _meta=options)) cls = super_new(cls, name, bases, dict(attrs, _meta=options))
if not options.graphql_type: if not options.graphql_type:
fields = copy_fields(fields) fields = copy_fields(fields, parent=cls)
options.graphql_type = GrapheneInterfaceType( options.graphql_type = GrapheneInterfaceType(
graphene_type=cls, graphene_type=cls,
name=options.name or cls.__name__, name=options.name or cls.__name__,
@ -43,30 +44,16 @@ class InterfaceTypeMeta(type):
) )
else: else:
assert not fields, "Can't mount Fields in an Interface with a defined graphql_type" 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(): for name, field in fields.items():
field.parent = cls setattr(cls, field.attname or name, field)
# setattr(cls, field.name, field)
return cls return cls
def attrs_without_fields(attrs, fields): def attrs_without_fields(attrs, fields):
fields_names = fields.keys() return {k: v for k, v in attrs.items() if k not in fields}
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))
class Interface(six.with_metaclass(InterfaceTypeMeta)): class Interface(six.with_metaclass(InterfaceTypeMeta)):

View 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)

View File

@ -45,4 +45,4 @@ def get_fields(in_type, attrs, bases):
fields.extend(local_fields) fields.extend(local_fields)
return OrderedDict(fields) return OrderedDict(sorted(fields, key=lambda kv: kv[1]))

View File

@ -1,12 +1,13 @@
import inspect 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): 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]: if _type in [Interface]:
return False return False
return inspect.isclass(_type) and issubclass(_type, ( return inspect.isclass(_type) and issubclass(_type, (