5
5
import copy
6
6
import dataclasses
7
7
import itertools
8
- from collections import Counter , defaultdict
9
- from collections .abc import Callable , Iterable
8
+ from collections import Counter
9
+ from collections .abc import Callable
10
10
from typing import (
11
11
TYPE_CHECKING ,
12
12
Any ,
42
42
from strawberry .schema .schema import Schema
43
43
from strawberry .schema .schema_converter import get_arguments
44
44
from strawberry .types import get_object_definition
45
- from strawberry .types .base import StrawberryContainer , StrawberryType
45
+ from strawberry .types .base import StrawberryContainer
46
46
from strawberry .types .info import Info
47
47
from strawberry .types .lazy_type import LazyType
48
48
from strawberry .types .object_type import StrawberryObjectDefinition
49
- from typing_extensions import TypeGuard , assert_never , assert_type
49
+ from typing_extensions import assert_never , assert_type
50
50
51
51
from strawberry_django .fields .types import resolve_model_field_name
52
52
from strawberry_django .pagination import OffsetPaginated , apply_window_pagination
59
59
PrefetchInspector ,
60
60
get_model_field ,
61
61
get_model_fields ,
62
+ get_possible_concrete_types ,
62
63
get_possible_type_definitions ,
64
+ is_polymorphic_model ,
63
65
)
64
66
from .utils .typing import (
65
67
AnnotateCallable ,
77
79
from collections .abc import Generator
78
80
79
81
from django .contrib .contenttypes .fields import GenericRelation
80
- from polymorphic .models import PolymorphicModel
81
82
from strawberry .types .execution import ExecutionContext
82
83
from strawberry .types .field import StrawberryField
83
84
from strawberry .utils .await_maybe import AwaitableOrValue
96
97
97
98
_sentinel = object ()
98
99
_annotate_placeholder = "__annotated_placeholder__"
99
- _interfaces : defaultdict [
100
- Schema ,
101
- dict [StrawberryObjectDefinition , list [StrawberryObjectDefinition ]],
102
- ] = defaultdict (
103
- dict ,
104
- )
105
-
106
-
107
- def _is_polymorphic_model (v : type ) -> TypeGuard [type [PolymorphicModel ]]:
108
- try :
109
- from polymorphic .models import PolymorphicModel
110
- except ImportError :
111
- return False
112
- return issubclass (v , PolymorphicModel )
113
100
114
101
115
102
@dataclasses .dataclass
@@ -1038,7 +1025,7 @@ def _get_model_hints(
1038
1025
# These must be prefixed with app_label__ModelName___ (note three underscores)
1039
1026
# This is a special syntax for django-polymorphic:
1040
1027
# https://django-polymorphic.readthedocs.io/en/stable/advanced.html#polymorphic-filtering-for-fields-in-inherited-classes
1041
- if _is_polymorphic_model (model ) and issubclass (dj_definition .model , model ):
1028
+ if is_polymorphic_model (model ) and issubclass (dj_definition .model , model ):
1042
1029
return _get_model_hints (
1043
1030
dj_definition .model ,
1044
1031
schema ,
@@ -1060,7 +1047,7 @@ def _get_model_hints(
1060
1047
store .only .append (prefix + pk .attname )
1061
1048
1062
1049
# If this is a polymorphic Model, make sure to select its content type
1063
- if _is_polymorphic_model (model ):
1050
+ if is_polymorphic_model (model ):
1064
1051
store .only .extend (prefix + f for f in model .polymorphic_internal_model_fields )
1065
1052
1066
1053
selections = [
@@ -1214,7 +1201,7 @@ def _get_model_hints_from_connection(
1214
1201
if node .name .value != "node" :
1215
1202
continue
1216
1203
1217
- for concrete_n_type in _get_possible_concrete_types (
1204
+ for concrete_n_type in get_possible_concrete_types (
1218
1205
model , schema , n_definition
1219
1206
):
1220
1207
n_gql_definition = _get_gql_definition (schema , concrete_n_type )
@@ -1271,9 +1258,7 @@ def _get_model_hints_from_paginated(
1271
1258
if selection .name .value != "results" :
1272
1259
continue
1273
1260
1274
- for concrete_n_type in _get_possible_concrete_types (
1275
- model , schema , n_definition
1276
- ):
1261
+ for concrete_n_type in get_possible_concrete_types (model , schema , n_definition ):
1277
1262
n_gql_definition = _get_gql_definition (
1278
1263
schema ,
1279
1264
concrete_n_type ,
@@ -1305,38 +1290,6 @@ def _get_model_hints_from_paginated(
1305
1290
return store
1306
1291
1307
1292
1308
- def _get_possible_concrete_types (
1309
- model : type [models .Model ],
1310
- schema : Schema ,
1311
- strawberry_type : StrawberryObjectDefinition | StrawberryType ,
1312
- ) -> Iterable [StrawberryObjectDefinition ]:
1313
- for object_definition in get_possible_type_definitions (strawberry_type ):
1314
- if object_definition .is_interface :
1315
- interface_definitions = _interfaces [schema ].get (object_definition )
1316
- if interface_definitions is None :
1317
- interface_definitions = []
1318
- for t in schema .schema_converter .type_map .values ():
1319
- t_definition = t .definition
1320
- if isinstance (
1321
- t_definition , StrawberryObjectDefinition
1322
- ) and issubclass (t_definition .origin , object_definition .origin ):
1323
- interface_definitions .append (t_definition )
1324
- _interfaces [schema ][object_definition ] = interface_definitions
1325
-
1326
- for interface_definition in interface_definitions :
1327
- dj_definition = get_django_definition (interface_definition .origin )
1328
- if dj_definition and (
1329
- issubclass (model , dj_definition .model )
1330
- or (
1331
- _is_polymorphic_model (model )
1332
- and issubclass (dj_definition .model , model )
1333
- )
1334
- ):
1335
- yield interface_definition
1336
- else :
1337
- yield object_definition
1338
-
1339
-
1340
1293
def optimize (
1341
1294
qs : QuerySet [_M ] | BaseManager [_M ],
1342
1295
info : GraphQLResolveInfo | Info ,
@@ -1400,7 +1353,7 @@ def optimize(
1400
1353
if strawberry_type is None :
1401
1354
return qs
1402
1355
1403
- for inner_object_definition in _get_possible_concrete_types (
1356
+ for inner_object_definition in get_possible_concrete_types (
1404
1357
qs .model , schema , strawberry_type
1405
1358
):
1406
1359
parent_type = _get_gql_definition (schema , inner_object_definition )
0 commit comments