1
- use std:: borrow:: Borrow ;
1
+ use std:: { borrow:: Borrow , fmt :: Display } ;
2
2
3
3
use crate :: {
4
4
ts_types:: {
@@ -72,7 +72,7 @@ fn get_schema_metadata_type(document: &TypeSystemDocument) -> TSType {
72
72
(
73
73
op. as_str ( ) ,
74
74
TSType :: TypeVariable ( ty. into ( ) ) ,
75
- schema_def. description . clone ( ) ,
75
+ schema_def. description . as_ref ( ) . map ( |d| d . value . clone ( ) ) ,
76
76
)
77
77
} ) ) ;
78
78
}
@@ -171,13 +171,14 @@ impl TypePrinter for ScalarTypeDefinition<'_> {
171
171
impl TypePrinter for ObjectTypeDefinition < ' _ > {
172
172
fn print_type (
173
173
& self ,
174
- _context : & SchemaTypePrinterContext ,
174
+ context : & SchemaTypePrinterContext ,
175
175
writer : & mut impl SourceMapWriter ,
176
176
) -> SchemaTypePrinterResult < ( ) > {
177
177
let type_name_ident = Ident {
178
178
name : "__typename" ,
179
179
position : Pos :: builtin ( ) ,
180
180
} ;
181
+ let schema_type = context. schema . get_type ( self . name . name ) ;
181
182
let obj_type = TSType :: object (
182
183
vec ! [ (
183
184
& type_name_ident,
@@ -186,12 +187,18 @@ impl TypePrinter for ObjectTypeDefinition<'_> {
186
187
) ]
187
188
. into_iter ( )
188
189
. chain ( self . fields . iter ( ) . map ( |field| {
190
+ let schema_field = schema_type
191
+ . and_then ( |ty| ty. as_object ( ) )
192
+ . and_then ( |ty| ty. fields . iter ( ) . find ( |f| f. name == field. name . name ) ) ;
189
193
(
190
194
& field. name ,
191
195
get_ts_type_of_type ( & field. r#type , |name| {
192
196
TSType :: TypeVariable ( ( & name. name ) . into ( ) )
193
197
} ) ,
194
- field. description . clone ( ) ,
198
+ make_ts_description (
199
+ & field. description ,
200
+ & schema_field. and_then ( |f| f. deprecation . as_ref ( ) ) ,
201
+ ) ,
195
202
)
196
203
} ) ) ,
197
204
) ;
@@ -283,10 +290,16 @@ impl TypePrinter for InputObjectTypeDefinition<'_> {
283
290
context : & SchemaTypePrinterContext ,
284
291
writer : & mut impl SourceMapWriter ,
285
292
) -> SchemaTypePrinterResult < ( ) > {
293
+ let schema_type = context. schema . get_type ( self . name . name ) ;
286
294
let obj_type = TSType :: Object (
287
295
self . fields
288
296
. iter ( )
289
297
. map ( |field| {
298
+ let schema_field = schema_type
299
+ . and_then ( |t| t. as_input_object ( ) )
300
+ . and_then ( |t| t. fields . iter ( ) . find ( |f| f. name == field. name . name ) )
301
+ . expect ( "Type system error" ) ;
302
+
290
303
let ts_type = get_ts_type_of_type ( & field. r#type , |name| {
291
304
TSType :: TypeVariable ( ( & name. name ) . into ( ) )
292
305
} )
@@ -297,7 +310,10 @@ impl TypePrinter for InputObjectTypeDefinition<'_> {
297
310
readonly : true ,
298
311
optional : context. options . input_nullable_field_is_optional
299
312
&& !field. r#type . is_nonnull ( ) ,
300
- description : field. description . clone ( ) ,
313
+ description : make_ts_description (
314
+ & field. description ,
315
+ & schema_field. deprecation ,
316
+ ) ,
301
317
}
302
318
} )
303
319
. collect ( ) ,
@@ -318,3 +334,19 @@ fn print_description(description: &Option<StringValue>, writer: &mut impl Source
318
334
jsdoc_print_description ( description, writer) ;
319
335
}
320
336
}
337
+
338
+ /// Combines description and deprecation reason into a single string.
339
+ fn make_ts_description (
340
+ description : & Option < StringValue > ,
341
+ deprecation : & Option < impl Display > ,
342
+ ) -> Option < String > {
343
+ match ( description, deprecation) {
344
+ ( Some ( description) , Some ( deprecation) ) => Some ( format ! (
345
+ "{}\n \n @deprecated {}" ,
346
+ description. value, deprecation
347
+ ) ) ,
348
+ ( Some ( description) , None ) => Some ( description. value . clone ( ) ) ,
349
+ ( None , Some ( deprecation) ) => format ! ( "@deprecated {}" , deprecation) . into ( ) ,
350
+ ( None , None ) => None ,
351
+ }
352
+ }
0 commit comments