-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Avoid calling Type.get_queryset method more than once
This change avoids calling Type.get_queryset method more than once when the type defines one. This used to happen specially for connections This change also paves the way for the upcoming nested list/connection optimizations that we want to do. Fix #528
- Loading branch information
1 parent
0efbc1d
commit 6905513
Showing
6 changed files
with
77 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import dataclasses | ||
from typing import Any, Optional, TypeVar | ||
|
||
from django.db.models import Model | ||
from django.db.models.query import QuerySet | ||
from strawberry import Info | ||
|
||
_M = TypeVar("_M", bound=Model) | ||
|
||
CONFIG_KEY = "_strawberry_django_config" | ||
|
||
|
||
@dataclasses.dataclass | ||
class StrawberryDjangoQuerySetConfig: | ||
optimized: bool = False | ||
type_get_queryset_did_run: bool = False | ||
|
||
|
||
def get_queryset_config(queryset: QuerySet) -> StrawberryDjangoQuerySetConfig: | ||
return getattr(queryset, CONFIG_KEY, None) or StrawberryDjangoQuerySetConfig() | ||
|
||
|
||
def run_type_get_queryset( | ||
qs: QuerySet[_M], | ||
origin: Any, | ||
info: Optional[Info] = None, | ||
) -> QuerySet[_M]: | ||
config = get_queryset_config(qs) | ||
get_queryset = getattr(origin, "get_queryset", None) | ||
|
||
if get_queryset and not config.type_get_queryset_did_run: | ||
qs = get_queryset(qs, info) | ||
new_config = get_queryset_config(qs) | ||
new_config.type_get_queryset_did_run = True | ||
|
||
return qs | ||
|
||
|
||
_original_clone = QuerySet._clone # type: ignore | ||
|
||
|
||
def _qs_clone(self): | ||
config = get_queryset_config(self) | ||
cloned = _original_clone(self) | ||
setattr(cloned, CONFIG_KEY, dataclasses.replace(config)) | ||
return cloned | ||
|
||
|
||
# Monkey patch the QuerySet._clone method to make sure our config is copied | ||
# to the new QuerySet instance once it is cloned. | ||
QuerySet._clone = _qs_clone # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters