Skip to content

Commit 87f5fff

Browse files
committed
Rename param to model_attr
1 parent 17536fc commit 87f5fff

File tree

3 files changed

+53
-58
lines changed

3 files changed

+53
-58
lines changed

graphene_sqlalchemy/tests/test_query.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ class Meta:
176176
model = Reporter
177177
interfaces = (Node,)
178178

179-
first_name_v2 = ORMField(prop_name='first_name')
180-
hybrid_prop_v2 = ORMField(prop_name='hybrid_prop')
181-
column_prop_v2 = ORMField(prop_name='column_prop')
179+
first_name_v2 = ORMField(model_attr='first_name')
180+
hybrid_prop_v2 = ORMField(model_attr='hybrid_prop')
181+
column_prop_v2 = ORMField(model_attr='column_prop')
182182
composite_prop = ORMField()
183-
favorite_article_v2 = ORMField(prop_name='favorite_article')
184-
articles_v2 = ORMField(prop_name='articles')
183+
favorite_article_v2 = ORMField(model_attr='favorite_article')
184+
articles_v2 = ORMField(model_attr='articles')
185185

186186
class ArticleType(SQLAlchemyObjectType):
187187
class Meta:

graphene_sqlalchemy/tests/test_types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class Meta:
126126

127127
# columns
128128
email = ORMField(deprecation_reason='Overridden')
129-
email_v2 = ORMField(prop_name='email', type=Int)
129+
email_v2 = ORMField(model_attr='email', type=Int)
130130

131131
# column_property
132132
column_prop = ORMField(type=String)

graphene_sqlalchemy/types.py

+47-52
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class ORMField(OrderedType):
2828
def __init__(
2929
self,
30-
prop_name=None,
30+
model_attr=None,
3131
type=None,
3232
required=None,
3333
description=None,
@@ -55,27 +55,26 @@ class Meta:
5555
-> MyType.id will be of type Int (vs ID).
5656
-> MyType.name will be of type NonNull(String) (vs String).
5757
58-
Parameters
59-
- prop_name : str, optional
60-
Name of the SQLAlchemy property used to resolve this field.
58+
:param str model_attr:
59+
Name of the SQLAlchemy model attribute used to resolve this field.
6160
Default to the name of the attribute referencing the ORMField.
62-
- type: optional
61+
:param type:
6362
Default to the type mapping in converter.py.
64-
- description: str, optional
63+
:param str description:
6564
Default to the `doc` attribute of the SQLAlchemy column property.
66-
- required: bool, optional
65+
:param bool required:
6766
Default to the opposite of the `nullable` attribute of the SQLAlchemy column property.
68-
- description: str, optional
67+
:param str description:
6968
Same behavior as in graphene.Field. Defaults to None.
70-
- deprecation_reason: str, optional
69+
:param str deprecation_reason:
7170
Same behavior as in graphene.Field. Defaults to None.
72-
- _creation_counter: int, optional
71+
:param int _creation_counter:
7372
Same behavior as in graphene.Field.
7473
"""
7574
super(ORMField, self).__init__(_creation_counter=_creation_counter)
7675
# The is only useful for documentation and auto-completion
7776
common_kwargs = {
78-
'prop_name': prop_name,
77+
'model_attr': model_attr,
7978
'type': type,
8079
'required': required,
8180
'description': description,
@@ -92,25 +91,21 @@ def construct_fields(
9291
"""
9392
Construct all the fields for a SQLAlchemyObjectType.
9493
The main steps are:
95-
- Gather all the relevant attributes from the SQLAlchemy model
96-
- Gather all the ORM fields defined on the type
97-
- Merge in overrides and build up all the fields
98-
99-
Parameters
100-
- obj_type : SQLAlchemyObjectType
101-
- model : the SQLAlchemy model
102-
- registry : Registry
103-
- only_fields : tuple[string]
104-
- exclude_fields : tuple[string]
105-
- connection_field_factory : function
106-
107-
Returns
108-
- fields
109-
An OrderedDict of field names to graphene.Field
94+
- Gather all the relevant attributes from the SQLAlchemy model
95+
- Gather all the ORM fields defined on the type
96+
- Merge in overrides and build up all the fields
97+
98+
:param SQLAlchemyObjectType obj_type:
99+
:param model: the SQLAlchemy model
100+
:param Registry registry:
101+
:param tuple[string] only_fields:
102+
:param tuple[string] exclude_fields:
103+
:param function connection_field_factory:
104+
:rtype: OrderedDict[str, graphene.Field]
110105
"""
111106
inspected_model = sqlalchemyinspect(model)
112-
# Gather all the relevant attributes from the SQLAlchemy model
113-
all_model_props = OrderedDict(
107+
# Gather all the relevant attributes from the SQLAlchemy model in order
108+
all_model_attrs = OrderedDict(
114109
inspected_model.column_attrs.items() +
115110
inspected_model.composites.items() +
116111
[(name, item) for name, item in inspected_model.all_orm_descriptors.items()
@@ -120,57 +115,57 @@ def construct_fields(
120115

121116
# Filter out excluded fields
122117
auto_orm_field_names = []
123-
for prop_name, prop in all_model_props.items():
124-
if (only_fields and prop_name not in only_fields) or (prop_name in exclude_fields):
118+
for attr_name, attr in all_model_attrs.items():
119+
if (only_fields and attr_name not in only_fields) or (attr_name in exclude_fields):
125120
continue
126-
auto_orm_field_names.append(prop_name)
121+
auto_orm_field_names.append(attr_name)
127122

128123
# Gather all the ORM fields defined on the type
129124
custom_orm_fields_items = [
130-
(attname, value)
125+
(attn_name, attr)
131126
for base in reversed(obj_type.__mro__)
132-
for attname, value in base.__dict__.items()
133-
if isinstance(value, ORMField)
127+
for attn_name, attr in base.__dict__.items()
128+
if isinstance(attr, ORMField)
134129
]
135130
custom_orm_fields_items = sorted(custom_orm_fields_items, key=lambda item: item[1])
136131

137-
# Set the prop_name if not set
132+
# Set the model_attr if not set
138133
for orm_field_name, orm_field in custom_orm_fields_items:
139-
prop_name = orm_field.kwargs.get('prop_name', orm_field_name)
140-
if prop_name not in all_model_props:
134+
attr_name = orm_field.kwargs.get('model_attr', orm_field_name)
135+
if attr_name not in all_model_attrs:
141136
raise Exception('Cannot map ORMField "{}" to SQLAlchemy model property'.format(orm_field_name))
142-
orm_field.kwargs['prop_name'] = prop_name
137+
orm_field.kwargs['model_attr'] = attr_name
143138

144139
# Merge automatic fields with custom ORM fields
145140
orm_fields = OrderedDict(custom_orm_fields_items)
146141
for orm_field_name in auto_orm_field_names:
147142
if orm_field_name in orm_fields:
148143
continue
149-
orm_fields[orm_field_name] = ORMField(prop_name=orm_field_name)
144+
orm_fields[orm_field_name] = ORMField(model_attr=orm_field_name)
150145

151146
# Build all the field dictionary
152147
fields = OrderedDict()
153148
for orm_field_name, orm_field in orm_fields.items():
154-
prop_name = orm_field.kwargs.pop('prop_name')
155-
prop = all_model_props[prop_name]
156-
157-
if isinstance(prop, ColumnProperty):
158-
field = convert_sqlalchemy_column(prop, registry, **orm_field.kwargs)
159-
elif isinstance(prop, RelationshipProperty):
160-
field = convert_sqlalchemy_relationship(prop, registry, connection_field_factory, **orm_field.kwargs)
161-
elif isinstance(prop, CompositeProperty):
162-
if prop_name != orm_field_name or orm_field.kwargs:
149+
attr_name = orm_field.kwargs.pop('model_attr')
150+
attr = all_model_attrs[attr_name]
151+
152+
if isinstance(attr, ColumnProperty):
153+
field = convert_sqlalchemy_column(attr, registry, **orm_field.kwargs)
154+
elif isinstance(attr, RelationshipProperty):
155+
field = convert_sqlalchemy_relationship(attr, registry, connection_field_factory, **orm_field.kwargs)
156+
elif isinstance(attr, CompositeProperty):
157+
if attr_name != orm_field_name or orm_field.kwargs:
163158
# TODO Add a way to override composite property fields
164159
raise ValueError(
165160
"ORMField kwargs for composite fields must be empty. "
166161
"Field: {}.{}".format(obj_type.__name__, orm_field_name))
167-
field = convert_sqlalchemy_composite(prop, registry)
168-
elif isinstance(prop, hybrid_property):
169-
field = convert_sqlalchemy_hybrid_method(prop, prop_name, **orm_field.kwargs)
162+
field = convert_sqlalchemy_composite(attr, registry)
163+
elif isinstance(attr, hybrid_property):
164+
field = convert_sqlalchemy_hybrid_method(attr, attr_name, **orm_field.kwargs)
170165
else:
171166
raise Exception('Property type is not supported') # Should never happen
172167

173-
registry.register_orm_field(obj_type, orm_field_name, prop)
168+
registry.register_orm_field(obj_type, orm_field_name, attr)
174169
fields[orm_field_name] = field
175170

176171
return fields

0 commit comments

Comments
 (0)