Skip to content

Commit 2d2a61c

Browse files
committed
Reimplement field deprecation annotations
1 parent a554885 commit 2d2a61c

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

graphql_client_codegen/src/codegen/selection.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::resolution::SelectionRef;
88
use crate::schema::TypeRef;
99
use crate::shared::field_rename_annotation;
1010
use crate::{
11+
deprecation::DeprecationStrategy,
1112
field_type::GraphqlTypeQualifier,
1213
// deprecation::DeprecationStrategy,
1314
resolution::{InlineFragment, OperationRef, ResolvedQuery, Selection, SelectionId},
@@ -198,6 +199,7 @@ fn calculate_selection<'a>(
198199
graphql_name: None,
199200
rust_name: fragment_ref.name().to_snake_case().into(),
200201
struct_id,
202+
deprecation: None,
201203
})
202204
}
203205
}
@@ -231,6 +233,7 @@ fn calculate_selection<'a>(
231233
field_type: context.schema().r#enum(enm).name().into(),
232234
field_type_qualifiers: schema_field.type_qualifiers(),
233235
flatten: false,
236+
deprecation: schema_field.deprecation(),
234237
});
235238
}
236239
TypeId::Scalar(scalar) => {
@@ -243,6 +246,7 @@ fn calculate_selection<'a>(
243246
struct_id,
244247
rust_name,
245248
flatten: false,
249+
deprecation: schema_field.deprecation(),
246250
});
247251
}
248252
TypeId::Object(_) | TypeId::Interface(_) | TypeId::Union(_) => {
@@ -255,6 +259,7 @@ fn calculate_selection<'a>(
255259
field_type_qualifiers: schema_field.type_qualifiers(),
256260
field_type: Cow::Owned(struct_name_string.clone()),
257261
flatten: false,
262+
deprecation: schema_field.deprecation(),
258263
});
259264

260265
let type_id = context.push_type(ExpandedType {
@@ -298,6 +303,7 @@ fn calculate_selection<'a>(
298303
rust_name: final_field_name,
299304
struct_id,
300305
flatten: true,
306+
deprecation: None,
301307
});
302308

303309
// We stop here, because the structs for the fragments are generated separately, to
@@ -317,10 +323,11 @@ struct ExpandedField<'a> {
317323
field_type_qualifiers: &'a [GraphqlTypeQualifier],
318324
struct_id: ResponseTypeId,
319325
flatten: bool,
326+
deprecation: Option<Option<&'a str>>,
320327
}
321328

322329
impl<'a> ExpandedField<'a> {
323-
fn render(&self) -> TokenStream {
330+
fn render(&self, options: &GraphQLClientCodegenOptions) -> Option<TokenStream> {
324331
let ident = Ident::new(&self.rust_name, Span::call_site());
325332
let qualified_type = decorate_type(
326333
&Ident::new(&self.field_type, Span::call_site()),
@@ -337,28 +344,25 @@ impl<'a> ExpandedField<'a> {
337344
None
338345
};
339346

340-
// TODO: deprecation
341-
// let deprecation_annotation = match (
342-
// field.schema_field().is_deprecated(),
343-
// options.deprecation_strategy(),
344-
// ) {
345-
// (false, _) | (true, DeprecationStrategy::Allow) => None,
346-
// (true, DeprecationStrategy::Warn) => {
347-
// let msg = field
348-
// .schema_field()
349-
// .deprecation_message()
350-
// .unwrap_or("This field is deprecated.");
351-
352-
// Some(quote!(#[deprecated(note = #msg)]))
353-
// }
354-
// (true, DeprecationStrategy::Deny) => continue,
355-
// };
356-
357-
quote! {
347+
let optional_deprecation_annotation =
348+
match (self.deprecation, options.deprecation_strategy()) {
349+
(None, _) | (Some(_), DeprecationStrategy::Allow) => None,
350+
(Some(msg), DeprecationStrategy::Warn) => {
351+
let optional_msg = msg.map(|msg| quote!((note = #msg)));
352+
353+
Some(quote!(#[deprecated#optional_msg]))
354+
}
355+
(Some(_), DeprecationStrategy::Deny) => return None,
356+
};
357+
358+
let tokens = quote! {
358359
#optional_flatten
359360
#optional_rename
361+
#optional_deprecation_annotation
360362
pub #ident: #qualified_type
361-
}
363+
};
364+
365+
Some(tokens)
362366
}
363367
}
364368

@@ -449,7 +453,7 @@ impl<'a> ExpandedSelection<'a> {
449453
.fields
450454
.iter()
451455
.filter(|field| field.struct_id == type_id)
452-
.map(|field| field.render())
456+
.filter_map(|field| field.render(self.options))
453457
.peekable();
454458

455459
let on_variants: Vec<TokenStream> = self

graphql_client_codegen/src/schema.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,13 @@ impl<'a> FieldRef<'a> {
601601
.as_ref()
602602
.and_then(|item| item.as_ref().map(String::as_str))
603603
}
604+
605+
pub(crate) fn deprecation(&self) -> Option<Option<&'a str>> {
606+
self.field()
607+
.deprecation
608+
.as_ref()
609+
.map(|o| o.as_ref().map(String::as_str))
610+
}
604611
}
605612

606613
impl<'a> InputRef<'a> {

0 commit comments

Comments
 (0)