Skip to content

Commit c42b2c1

Browse files
committed
Use Graphene Datoader in graphene>=3.1.1
Signed-off-by: Erik Wrede <erikwrede2@gmail.com>
1 parent dfee3e9 commit c42b2c1

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

graphene_sqlalchemy/batching.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1+
"""The dataloader uses "select in loading" strategy to load related entities."""
2+
from typing import Any
3+
14
import aiodataloader
25
import sqlalchemy
36
from sqlalchemy.orm import Session, strategies
47
from sqlalchemy.orm.query import QueryContext
58

6-
from .utils import is_sqlalchemy_version_less_than
9+
from .utils import (is_graphene_version_less_than,
10+
is_sqlalchemy_version_less_than)
711

812

9-
def get_batch_resolver(relationship_prop):
13+
def get_data_loader_impl() -> Any:
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+
1023

24+
DataLoader = get_data_loader_impl()
25+
26+
27+
def get_batch_resolver(relationship_prop):
1128
# Cache this across `batch_load_fn` calls
1229
# This is so SQL string generation is cached under-the-hood via `bakery`
1330
selectin_loader = strategies.SelectInLoader(relationship_prop, (('lazy', 'selectin'),))

graphene_sqlalchemy/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ def is_sqlalchemy_version_less_than(version_string):
156156
return pkg_resources.get_distribution('SQLAlchemy').parsed_version < pkg_resources.parse_version(version_string)
157157

158158

159+
def is_graphene_version_less_than(version_string):
160+
"""Check the installed graphene version"""
161+
return pkg_resources.get_distribution('graphene').parsed_version < pkg_resources.parse_version(version_string)
162+
163+
159164
class singledispatchbymatchfunction:
160165
"""
161166
Inspired by @singledispatch, this is a variant that works using a matcher function
@@ -197,6 +202,7 @@ def safe_isinstance_checker(arg):
197202
return isinstance(arg, cls)
198203
except TypeError:
199204
pass
205+
200206
return safe_isinstance_checker
201207

202208

@@ -210,5 +216,6 @@ def registry_sqlalchemy_model_from_str(model_name: str) -> Optional[Any]:
210216

211217
class DummyImport:
212218
"""The dummy module returns 'object' for a query for any member"""
219+
213220
def __getattr__(self, name):
214221
return object

0 commit comments

Comments
 (0)