File tree 2 files changed +27
-3
lines changed
2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change
1
+ """The dataloader uses "select in loading" strategy to load related entities."""
2
+ from typing import Any
3
+
1
4
import aiodataloader
2
5
import sqlalchemy
3
6
from sqlalchemy .orm import Session , strategies
4
7
from sqlalchemy .orm .query import QueryContext
5
8
6
- from .utils import is_sqlalchemy_version_less_than
9
+ from .utils import (is_graphene_version_less_than ,
10
+ is_sqlalchemy_version_less_than )
7
11
8
12
9
- def get_batch_resolver (relationship_prop ):
13
+ def get_data_loader_impl () -> Any : # pragma: no cover
14
+ """Graphene >= 3.1.1 ships a copy of aiodataloader with minor fixes. To preserve backward-compatibility,
15
+ aiodataloader is used in conjunction with older versions of graphene"""
16
+ if is_graphene_version_less_than ("3.1.1" ):
17
+ from aiodataloader import DataLoader
18
+ else :
19
+ from graphene .utils .dataloader import DataLoader
20
+
21
+ return DataLoader
22
+
10
23
24
+ DataLoader = get_data_loader_impl ()
25
+
26
+
27
+ def get_batch_resolver (relationship_prop ):
11
28
# Cache this across `batch_load_fn` calls
12
29
# This is so SQL string generation is cached under-the-hood via `bakery`
13
30
selectin_loader = strategies .SelectInLoader (relationship_prop , (('lazy' , 'selectin' ),))
Original file line number Diff line number Diff line change @@ -151,11 +151,16 @@ def sort_argument_for_model(cls, has_default=True):
151
151
return Argument (List (enum ), default_value = enum .default )
152
152
153
153
154
- def is_sqlalchemy_version_less_than (version_string ):
154
+ def is_sqlalchemy_version_less_than (version_string ): # pragma: no cover
155
155
"""Check the installed SQLAlchemy version"""
156
156
return pkg_resources .get_distribution ('SQLAlchemy' ).parsed_version < pkg_resources .parse_version (version_string )
157
157
158
158
159
+ def is_graphene_version_less_than (version_string ): # pragma: no cover
160
+ """Check the installed graphene version"""
161
+ return pkg_resources .get_distribution ('graphene' ).parsed_version < pkg_resources .parse_version (version_string )
162
+
163
+
159
164
class singledispatchbymatchfunction :
160
165
"""
161
166
Inspired by @singledispatch, this is a variant that works using a matcher function
@@ -197,6 +202,7 @@ def safe_isinstance_checker(arg):
197
202
return isinstance (arg , cls )
198
203
except TypeError :
199
204
pass
205
+
200
206
return safe_isinstance_checker
201
207
202
208
@@ -210,5 +216,6 @@ def registry_sqlalchemy_model_from_str(model_name: str) -> Optional[Any]:
210
216
211
217
class DummyImport :
212
218
"""The dummy module returns 'object' for a query for any member"""
219
+
213
220
def __getattr__ (self , name ):
214
221
return object
You can’t perform that action at this time.
0 commit comments