Skip to content

Commit 1e3817f

Browse files
author
Julien Nakache
committed
fix field ordering in python version < 3.5
1 parent 7734a23 commit 1e3817f

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

graphene_sqlalchemy/tests/test_types.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ class Meta:
6262
interfaces = (Node,)
6363

6464
assert list(ReporterType._meta.fields.keys()) == [
65-
"column_prop",
65+
# Columns
66+
"column_prop", # SQLAlchemy retuns column properties first
6667
"id",
6768
"first_name",
6869
"last_name",
6970
"email",
7071
"favorite_pet_kind",
72+
# Composite
7173
"composite_prop",
74+
# Hybrid
7275
"hybrid_prop",
76+
# Relationship
7377
"pets",
7478
"articles",
7579
"favorite_article",
@@ -147,6 +151,7 @@ class Meta:
147151
use_connection = False
148152

149153
assert list(ReporterType._meta.fields.keys()) == [
154+
# First the ORMField in the order they were defined
150155
"first_name",
151156
"last_name",
152157
"email",
@@ -157,6 +162,7 @@ class Meta:
157162
"favorite_article",
158163
"articles",
159164
"pets",
165+
# Then the automatic SQLAlchemy fields
160166
"id",
161167
"favorite_pet_kind",
162168
]

graphene_sqlalchemy/types.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from graphene.relay import Connection, Node
1212
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
1313
from graphene.types.utils import yank_fields_from_attrs
14+
from graphene.utils.orderedtype import OrderedType
1415

1516
from .converter import (convert_sqlalchemy_column,
1617
convert_sqlalchemy_composite,
@@ -23,16 +24,18 @@
2324
from .utils import get_query, is_mapped_class, is_mapped_instance
2425

2526

26-
class ORMField(object):
27+
class ORMField(OrderedType):
2728
def __init__(
2829
self,
2930
type=None,
3031
prop_name=None,
3132
description=None,
3233
deprecation_reason=None,
3334
required=None,
35+
_creation_counter=None,
3436
**field_kwargs
3537
):
38+
super(ORMField, self).__init__(_creation_counter=_creation_counter)
3639
# The is only useful for documentation and auto-completion
3740
common_kwargs = {
3841
'type': type,
@@ -65,18 +68,19 @@ def construct_fields(
6568
auto_orm_field_names.append(prop_name)
6669

6770
# TODO Get ORMField fields defined on parent classes
68-
custom_orm_fields = OrderedDict()
71+
custom_orm_fields_items = []
6972
for attname, value in list(obj_type.__dict__.items()):
7073
if isinstance(value, ORMField):
71-
custom_orm_fields[attname] = value
74+
custom_orm_fields_items.append((attname, value))
75+
custom_orm_fields_items = sorted(custom_orm_fields_items, key=lambda item: item[1])
7276

73-
for orm_field_name, orm_field in custom_orm_fields.items():
77+
for orm_field_name, orm_field in custom_orm_fields_items:
7478
prop_name = orm_field.kwargs.get('prop_name', orm_field_name)
7579
if prop_name not in all_model_props:
7680
raise Exception('Cannot map ORMField "{}" to SQLAlchemy model property'.format(orm_field_name))
7781
orm_field.kwargs['prop_name'] = prop_name
7882

79-
orm_fields = custom_orm_fields.copy()
83+
orm_fields = OrderedDict(custom_orm_fields_items)
8084
for orm_field_name in auto_orm_field_names:
8185
if orm_field_name in orm_fields:
8286
continue
@@ -159,6 +163,7 @@ def __init_subclass_with_meta__(
159163
connection_field_factory=connection_field_factory,
160164
),
161165
_as=Field,
166+
sort=False,
162167
)
163168

164169
if use_connection is None and interfaces:

0 commit comments

Comments
 (0)