Fixed SQLAlchemy integration

This commit is contained in:
Syrus Akbary 2016-08-19 09:18:29 -07:00
parent 4a2f10b116
commit dac4f2dc19
2 changed files with 11 additions and 8 deletions

View File

@ -3,7 +3,7 @@ import six
from sqlalchemy.inspection import inspect as sqlalchemyinspect
from sqlalchemy.orm.exc import NoResultFound
from graphene import ObjectType
from graphene import ObjectType, Field
from graphene.relay import is_node
from .converter import (convert_sqlalchemy_column,
convert_sqlalchemy_relationship)
@ -13,7 +13,7 @@ from graphene.types.objecttype import ObjectTypeMeta
from graphene.types.options import Options
from .registry import Registry, get_global_registry
from graphene.utils.is_base_type import is_base_type
from graphene.types.utils import get_fields_in_type, merge
from graphene.types.utils import yank_fields_from_attrs, merge
from .utils import get_query
@ -89,9 +89,9 @@ class SQLAlchemyObjectTypeMeta(ObjectTypeMeta):
options.registry.register(cls)
options.sqlalchemy_fields = get_fields_in_type(
ObjectType,
construct_fields(options)
options.sqlalchemy_fields = yank_fields_from_attrs(
construct_fields(options),
_as=Field,
)
options.fields = merge(
options.interface_fields,

View File

@ -65,7 +65,7 @@ def get_field_as(value, _as=None):
return mount_as(value, _as)
def yank_fields_from_attrs(attrs, _as=None):
def yank_fields_from_attrs(attrs, _as=None, delete=True, sort=True):
'''
Extract all the fields in given attributes (dict)
and return them ordered
@ -76,6 +76,9 @@ def yank_fields_from_attrs(attrs, _as=None):
if not field:
continue
fields_with_names.append((attname, field))
if delete:
del attrs[attname]
return OrderedDict(sorted(fields_with_names, key=lambda f: f[1]))
if sort:
fields_with_names = sorted(fields_with_names, key=lambda f: f[1])
return OrderedDict(fields_with_names)