|
1 | 1 | from enum import EnumMeta
|
2 | 2 |
|
3 | 3 | from singledispatch import singledispatch
|
4 |
| -from sqlalchemy import inspect, types |
| 4 | +from sqlalchemy import types |
5 | 5 | from sqlalchemy.dialects import postgresql
|
6 |
| -from sqlalchemy.ext.hybrid import hybrid_property |
7 |
| -from sqlalchemy.orm import (ColumnProperty, CompositeProperty, |
8 |
| - RelationshipProperty, interfaces, strategies) |
9 |
| -from sqlalchemy.orm.attributes import InstrumentedAttribute |
| 6 | +from sqlalchemy.orm import (ColumnProperty, RelationshipProperty, class_mapper, |
| 7 | + interfaces, strategies) |
10 | 8 |
|
11 | 9 | from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List,
|
12 | 10 | String)
|
@@ -36,39 +34,35 @@ def is_column_nullable(column):
|
36 | 34 | return bool(getattr(column, "nullable", True))
|
37 | 35 |
|
38 | 36 |
|
39 |
| -def convert_sqlalchemy_association_proxy(association_prop, obj_type, registry, connection_field_factory, batching, resolver, **field_kwargs): |
40 |
| - model = association_prop.target_class |
41 |
| - |
42 |
| - attr = getattr(model, association_prop.value_attr) |
43 |
| - if isinstance(attr, InstrumentedAttribute): |
44 |
| - attr = inspect(attr).property |
45 |
| - |
| 37 | +def convert_sqlalchemy_association_proxy(parent, assoc_prop, obj_type, registry, |
| 38 | + connection_field_factory, batching, resolver, **field_kwargs): |
46 | 39 | def dynamic_type():
|
| 40 | + prop = class_mapper(parent).attrs[assoc_prop.target_collection] |
| 41 | + scalar = not prop.uselist |
| 42 | + model = prop.mapper.class_ |
| 43 | + attr = class_mapper(model).attrs[assoc_prop.value_attr] |
| 44 | + |
47 | 45 | if isinstance(attr, ColumnProperty):
|
48 | 46 | field = convert_sqlalchemy_column(
|
49 | 47 | attr,
|
50 | 48 | registry,
|
51 | 49 | resolver,
|
52 | 50 | **field_kwargs
|
53 | 51 | )
|
54 |
| - if not association_prop.scalar: |
| 52 | + if not scalar: |
55 | 53 | # repackage as List
|
56 | 54 | field.__dict__['_type'] = List(field.type)
|
57 |
| - |
58 | 55 | return field
|
59 | 56 | elif isinstance(attr, RelationshipProperty):
|
60 |
| - batching_ = field_kwargs.pop('batching', batching) |
61 | 57 | return convert_sqlalchemy_relationship(
|
62 | 58 | attr,
|
63 | 59 | obj_type,
|
64 | 60 | connection_field_factory,
|
65 |
| - batching_, |
66 |
| - association_prop.value_attr, |
| 61 | + field_kwargs.pop('batching', batching), |
| 62 | + assoc_prop.value_attr, |
67 | 63 | **field_kwargs
|
68 |
| - # resolve Dynamic type |
69 | 64 | ).get_type()
|
70 |
| - |
71 |
| - raise NotImplementedError(attr) |
| 65 | + # else, not supported |
72 | 66 |
|
73 | 67 | return Dynamic(dynamic_type)
|
74 | 68 |
|
|
0 commit comments