-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathloader.py
162 lines (139 loc) · 6.72 KB
/
loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
from collections import defaultdict
from typing import (
Any,
AsyncContextManager,
Callable,
Dict,
List,
Mapping,
Optional,
Tuple,
Union,
)
from strawberry_sqlalchemy_mapper.exc import InvalidLocalRemotePairs
from sqlalchemy import select, tuple_
from sqlalchemy.engine.base import Connection
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession
from sqlalchemy.orm import RelationshipProperty, Session
from strawberry.dataloader import DataLoader
class StrawberrySQLAlchemyLoader:
"""
Creates DataLoader instances on-the-fly for SQLAlchemy relationships
"""
_loaders: Dict[RelationshipProperty, DataLoader]
def __init__(
self,
bind: Union[Session, Connection, None] = None,
async_bind_factory: Optional[
Union[
Callable[[], AsyncContextManager[AsyncSession]],
Callable[[], AsyncContextManager[AsyncConnection]],
]
] = None,
) -> None:
self._loaders = {}
self._bind = bind
self._async_bind_factory = async_bind_factory
self._logger = logging.getLogger("strawberry_sqlalchemy_mapper")
if bind is None and async_bind_factory is None:
self._logger.warning(
"One of bind or async_bind_factory must be set for loader to function properly."
)
async def _scalars_all(self, *args, disabled_optimization_to_secondary_tables=False, **kwargs):
if self._async_bind_factory:
async with self._async_bind_factory() as bind:
if disabled_optimization_to_secondary_tables is True:
return (await bind.execute(*args, **kwargs)).all()
return (await bind.scalars(*args, **kwargs)).all()
else:
assert self._bind is not None
if disabled_optimization_to_secondary_tables is True:
return self._bind.execute(*args, **kwargs).all()
return self._bind.scalars(*args, **kwargs).all()
def loader_for(self, relationship: RelationshipProperty) -> DataLoader:
"""
Retrieve or create a DataLoader for the given relationship
"""
try:
return self._loaders[relationship]
except KeyError:
related_model = relationship.entity.entity
async def load_fn(keys: List[Tuple]) -> List[Any]:
def _build_normal_relationship_query(related_model, relationship, keys):
return select(related_model).filter(
tuple_(
*[remote for _, remote in relationship.local_remote_pairs or []]
).in_(keys)
)
def _build_relationship_with_secondary_table_query(related_model, relationship, keys):
# Use another query when relationship uses a secondary table
self_model = relationship.parent.entity
if not relationship.local_remote_pairs:
raise InvalidLocalRemotePairs(
f"{related_model.__name__} -- {self_model.__name__}")
self_model_key_label = str(
relationship.local_remote_pairs[0][1].key)
related_model_key_label = str(
relationship.local_remote_pairs[1][1].key)
self_model_key = str(
relationship.local_remote_pairs[0][0].key)
related_model_key = str(
relationship.local_remote_pairs[1][0].key)
remote_to_use = relationship.local_remote_pairs[0][1]
query_keys = tuple([item[0] for item in keys])
# This query returns rows in this format -> (self_model.key, related_model)
return (
select(
getattr(self_model, self_model_key).label(
self_model_key_label),
related_model
)
.join(
relationship.secondary,
getattr(relationship.secondary.c,
related_model_key_label) == getattr(related_model, related_model_key)
)
.join(
self_model,
getattr(relationship.secondary.c,
self_model_key_label) == getattr(self_model, self_model_key)
)
.filter(
remote_to_use.in_(query_keys)
)
)
def _build_query(*args):
return _build_normal_relationship_query(*args) if relationship.secondary is None else _build_relationship_with_secondary_table_query(*args)
query = _build_query(related_model, relationship, keys)
if relationship.order_by:
query = query.order_by(*relationship.order_by)
if relationship.secondary is not None:
# We need to retrieve values from both the self_model and related_model. To achieve this, we must disable the default SQLAlchemy optimization that returns only related_model values. This is necessary because we use the keys variable to match both related_model and self_model.
rows = await self._scalars_all(query, disabled_optimization_to_secondary_tables=True)
else:
rows = await self._scalars_all(query)
def group_by_remote_key(row: Any) -> Tuple:
return tuple(
[
getattr(row, remote.key)
for _, remote in relationship.local_remote_pairs or []
if remote.key
]
)
grouped_keys: Mapping[Tuple, List[Any]] = defaultdict(list)
if relationship.secondary is None:
for row in rows:
grouped_keys[group_by_remote_key(row)].append(row)
else:
for row in rows:
grouped_keys[(row[0],)].append(row[1])
if relationship.uselist:
return [grouped_keys[key] for key in keys]
else:
return [
grouped_keys[key][0] if grouped_keys[key] else None
for key in keys
]
self._loaders[relationship] = DataLoader(load_fn=load_fn)
return self._loaders[relationship]