Skip to content

Commit a03a8b1

Browse files
authored
Use Graphene DataLoader in graphene>=3.1.1 (#360)
* Use Graphene Datolader in graphene>=3.1.1
1 parent dfee3e9 commit a03a8b1

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

graphene_sqlalchemy/batching.py

+19-2
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: # 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+
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

+8-1
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,16 @@ def sort_argument_for_model(cls, has_default=True):
151151
return Argument(List(enum), default_value=enum.default)
152152

153153

154-
def is_sqlalchemy_version_less_than(version_string):
154+
def is_sqlalchemy_version_less_than(version_string): # pragma: no cover
155155
"""Check the installed SQLAlchemy version"""
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): # 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+
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)