25
25
from strawberry .utils .deprecations import DEPRECATION_MESSAGES , DeprecatedDescriptor
26
26
from strawberry .utils .str_converters import to_camel_case
27
27
28
- from ._dataclasses_field import StrawberryDataclassField
29
28
from .base import StrawberryObjectDefinition
30
29
from .field import StrawberryField , field
31
30
from .type_resolver import _get_fields
@@ -134,17 +133,22 @@ def _inject_default_for_maybe_annotations(
134
133
setattr (cls , name , None )
135
134
136
135
137
- def _convert_field_annotations_to_dataclass_fields (
138
- cls : builtins . type [ T ], annotations : dict [str , Any ]
136
+ def _preprocess_type (
137
+ cls : T , original_type_annotations : dict [str , Any ], is_input : bool
139
138
) -> None :
139
+ annotations = getattr (cls , "__annotations__" , {})
140
+
140
141
for field_name in annotations :
141
142
field = getattr (cls , field_name , None )
142
143
143
144
if field and isinstance (field , StrawberryField ):
144
- dataclass_field = StrawberryDataclassField .from_strawberry_field (field )
145
- annotations [field_name ] = dataclass_field .type
145
+ if field .type_annotation :
146
+ original_type_annotations [field_name ] = field .type_annotation .annotation
147
+
148
+ field = field .as_dataclass_field ()
146
149
147
- setattr (cls , field_name , dataclass_field )
150
+ if is_input :
151
+ _inject_default_for_maybe_annotations (cls , annotations )
148
152
149
153
150
154
def _process_type (
@@ -156,8 +160,20 @@ def _process_type(
156
160
description : Optional [str ] = None ,
157
161
directives : Optional [Sequence [object ]] = (),
158
162
extend : bool = False ,
159
- original_type_annotations : Optional [dict [str , Any ]] = None ,
160
163
) -> T :
164
+ # when running `_wrap_dataclass` we lose some of the information about the
165
+ # the passed types, especially the type_annotation inside the StrawberryField
166
+ # this makes it impossible to customise the field type, like this:
167
+ # >>> @strawberry.type
168
+ # >>> class Query:
169
+ # >>> a: int = strawberry.field(graphql_type=str)
170
+ # so we need to extract the information before running `_wrap_dataclass`
171
+ original_type_annotations : dict [str , Any ] = {}
172
+
173
+ _preprocess_type (cls , original_type_annotations , is_input )
174
+
175
+ cls = _wrap_dataclass (cls )
176
+
161
177
name = name or to_camel_case (cls .__name__ )
162
178
original_type_annotations = original_type_annotations or {}
163
179
@@ -300,33 +316,14 @@ def wrap(cls: T) -> T:
300
316
exc = ObjectIsNotClassError .type
301
317
raise exc (cls )
302
318
303
- # when running `_wrap_dataclass` we lose some of the information about the
304
- # the passed types, especially the type_annotation inside the StrawberryField
305
- # this makes it impossible to customise the field type, like this:
306
- # >>> @strawberry.type
307
- # >>> class Query:
308
- # >>> a: int = strawberry.field(graphql_type=str)
309
- # so we need to extract the information before running `_wrap_dataclass`
310
- original_type_annotations : dict [str , Any ] = {}
311
-
312
- annotations = getattr (cls , "__annotations__" , {})
313
-
314
- _convert_field_annotations_to_dataclass_fields (cls , annotations )
315
-
316
- if is_input :
317
- _inject_default_for_maybe_annotations (cls , annotations )
318
-
319
- wrapped = _wrap_dataclass (cls )
320
-
321
319
return _process_type ( # type: ignore
322
- wrapped ,
320
+ cls ,
323
321
name = name ,
324
322
is_input = is_input ,
325
323
is_interface = is_interface ,
326
324
description = description ,
327
325
directives = directives ,
328
326
extend = extend ,
329
- original_type_annotations = original_type_annotations ,
330
327
)
331
328
332
329
if cls is None :
0 commit comments