Skip to content

Commit 66fce82

Browse files
authored
feat: Propagate deprecated GraphQL input fields (#410) (#411)
1 parent 0d4694d commit 66fce82

3 files changed

Lines changed: 126 additions & 1 deletion

File tree

packages/graphql_codegen/lib/src/printer/base/input.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:graphql_codegen/src/printer/base/undefined.dart';
1111
import 'package:graphql_codegen/src/printer/clients/utils.dart';
1212
import 'package:graphql_codegen/src/printer/context.dart';
1313
import 'package:graphql_codegen/src/printer/utils.dart';
14+
import 'package:graphql_codegen/src/printer/base/deprecation.dart';
1415

1516
List<Spec> printInputClasses(PrintContext<ContextInput> context) =>
1617
_printInputClasses(
@@ -34,11 +35,17 @@ List<Spec> _printInputClasses({
3435
properties.map(
3536
(property) => Parameter((b) {
3637
final innerType = printClassPropertyType(context, property);
38+
final deprecationReason = extractDeprecatedReason(property.directives);
39+
3740
b
3841
..named = true
3942
..required = property.isRequired
4043
..type = property.hasDefaultValue ? asNullable(innerType) : innerType
41-
..name = context.namePrinter.printPropertyName(property.name);
44+
..name = context.namePrinter.printPropertyName(property.name)
45+
..annotations = ListBuilder([
46+
if (deprecationReason != null)
47+
refer('Deprecated').call([literalString(deprecationReason)]),
48+
]);
4249
}),
4350
),
4451
);

packages/graphql_codegen/test/assets/deprecated/document.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ enum E {
1010

1111
query Q {
1212
deprecatedField
13+
}
14+
15+
input I {
16+
a: String
17+
d: String @deprecated(reason: "Please don't")
1318
}

packages/graphql_codegen/test/assets/deprecated/document.graphql.dart

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,118 @@
11
import 'package:gql/ast.dart';
22

3+
class Input$I {
4+
factory Input$I({String? a, @Deprecated('Please don\'t') String? d}) =>
5+
Input$I._({if (a != null) r'a': a, if (d != null) r'd': d});
6+
7+
Input$I._(this._$data);
8+
9+
factory Input$I.fromJson(Map<String, dynamic> data) {
10+
final result$data = <String, dynamic>{};
11+
if (data.containsKey('a')) {
12+
final l$a = data['a'];
13+
result$data['a'] = (l$a as String?);
14+
}
15+
if (data.containsKey('d')) {
16+
final l$d = data['d'];
17+
result$data['d'] = (l$d as String?);
18+
}
19+
return Input$I._(result$data);
20+
}
21+
22+
Map<String, dynamic> _$data;
23+
24+
String? get a => (_$data['a'] as String?);
25+
26+
String? get d => (_$data['d'] as String?);
27+
28+
Map<String, dynamic> toJson() {
29+
final result$data = <String, dynamic>{};
30+
if (_$data.containsKey('a')) {
31+
final l$a = a;
32+
result$data['a'] = l$a;
33+
}
34+
if (_$data.containsKey('d')) {
35+
final l$d = d;
36+
result$data['d'] = l$d;
37+
}
38+
return result$data;
39+
}
40+
41+
CopyWith$Input$I<Input$I> get copyWith => CopyWith$Input$I(this, (i) => i);
42+
43+
@override
44+
bool operator ==(Object other) {
45+
if (identical(this, other)) {
46+
return true;
47+
}
48+
if (other is! Input$I || runtimeType != other.runtimeType) {
49+
return false;
50+
}
51+
final l$a = a;
52+
final lOther$a = other.a;
53+
if (_$data.containsKey('a') != other._$data.containsKey('a')) {
54+
return false;
55+
}
56+
if (l$a != lOther$a) {
57+
return false;
58+
}
59+
final l$d = d;
60+
final lOther$d = other.d;
61+
if (_$data.containsKey('d') != other._$data.containsKey('d')) {
62+
return false;
63+
}
64+
if (l$d != lOther$d) {
65+
return false;
66+
}
67+
return true;
68+
}
69+
70+
@override
71+
int get hashCode {
72+
final l$a = a;
73+
final l$d = d;
74+
return Object.hashAll([
75+
_$data.containsKey('a') ? l$a : const {},
76+
_$data.containsKey('d') ? l$d : const {},
77+
]);
78+
}
79+
}
80+
81+
abstract class CopyWith$Input$I<TRes> {
82+
factory CopyWith$Input$I(Input$I instance, TRes Function(Input$I) then) =
83+
_CopyWithImpl$Input$I;
84+
85+
factory CopyWith$Input$I.stub(TRes res) = _CopyWithStubImpl$Input$I;
86+
87+
TRes call({String? a, String? d});
88+
}
89+
90+
class _CopyWithImpl$Input$I<TRes> implements CopyWith$Input$I<TRes> {
91+
_CopyWithImpl$Input$I(this._instance, this._then);
92+
93+
final Input$I _instance;
94+
95+
final TRes Function(Input$I) _then;
96+
97+
static const _undefined = <dynamic, dynamic>{};
98+
99+
TRes call({Object? a = _undefined, Object? d = _undefined}) => _then(
100+
Input$I._({
101+
..._instance._$data,
102+
if (a != _undefined) 'a': (a as String?),
103+
if (d != _undefined) 'd': (d as String?),
104+
}),
105+
);
106+
}
107+
108+
class _CopyWithStubImpl$Input$I<TRes> implements CopyWith$Input$I<TRes> {
109+
_CopyWithStubImpl$Input$I(this._res);
110+
111+
TRes _res;
112+
113+
call({String? a, String? d}) => _res;
114+
}
115+
3116
enum Enum$E {
4117
A,
5118
B,

0 commit comments

Comments
 (0)