Skip to content

Commit dad257a

Browse files
authored
ref: Default value code (heftapp#296)
1 parent 7301a84 commit dad257a

6 files changed

Lines changed: 128 additions & 43 deletions

File tree

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ import 'package:graphql_codegen/src/printer/context.dart';
66

77
typedef DataObjResolver = Expression Function();
88

9+
TypeNode _typeNodeAsNullable(TypeNode node) {
10+
if (!node.isNonNull) {
11+
return node;
12+
}
13+
if (node is ListTypeNode) {
14+
return ListTypeNode(
15+
type: node.type,
16+
isNonNull: false,
17+
);
18+
}
19+
if (node is NamedTypeNode) {
20+
return NamedTypeNode(
21+
name: node.name,
22+
isNonNull: false,
23+
);
24+
}
25+
return node;
26+
}
27+
928
Method printEqualityOperator(
1029
PrintContext c,
1130
String name,
@@ -53,23 +72,17 @@ Method printEqualityOperator(
5372
),
5473
],
5574
_printPropertyEqualityCheck(
56-
e.type,
75+
e.hasDefaultValue ? _typeNodeAsNullable(e.type) : e.type,
5776
localThisName,
5877
localOtherName,
59-
hasDefaultValue: e.hasDefaultValue,
6078
)
6179
];
6280
},
6381
),
6482
literalTrue.returned.statement,
6583
])));
6684

67-
Code _printPropertyEqualityCheck(
68-
TypeNode type,
69-
String self,
70-
String other, {
71-
bool hasDefaultValue = false,
72-
}) {
85+
Code _printPropertyEqualityCheck(TypeNode type, String self, String other) {
7386
if (type is NamedTypeNode) {
7487
return Code(
7588
"if (${self} != ${other}) {return false;}",
@@ -91,7 +104,7 @@ Code _printPropertyEqualityCheck(
91104
innerCheck,
92105
Code("}")
93106
]);
94-
if (type.isNonNull && !hasDefaultValue) return listCheck;
107+
if (type.isNonNull) return listCheck;
95108
return Block.of([
96109
Code("if (${self} != null && ${other} != null) {"),
97110
listCheck,
@@ -129,9 +142,10 @@ Method printHashCodeMethod(
129142
final localProp = context.namePrinter
130143
.printLocalPropertyName(property.name);
131144
final hash = _printPropertyHash(
132-
property.type,
145+
property.hasDefaultValue
146+
? _typeNodeAsNullable(property.type)
147+
: property.type,
133148
refer(localProp),
134-
hasDefaultValue: property.hasDefaultValue,
135149
);
136150
if (dataObjectCheckResolver != null &&
137151
!property.isRequired) {
@@ -155,9 +169,8 @@ Method printHashCodeMethod(
155169

156170
Expression _printPropertyHash(
157171
TypeNode type,
158-
Expression name, {
159-
bool hasDefaultValue = false,
160-
}) {
172+
Expression name,
173+
) {
161174
if (type is NamedTypeNode) {
162175
return name;
163176
}
@@ -173,7 +186,7 @@ Expression _printPropertyHash(
173186
).closure,
174187
]),
175188
]);
176-
if (type.isNonNull && !hasDefaultValue) {
189+
if (type.isNonNull) {
177190
return inner;
178191
}
179192
return name.equalTo(literalNull).conditional(literalNull, inner);

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,17 @@ Code _printToJson(PrintContext context, Iterable<ContextProperty> properties) {
208208
refer(resultDataVariableName)
209209
.index(literalString(property.name.value))
210210
.assign(
211-
property.hasDefaultValue && property.isNonNull
212-
? printNullCheck(
213-
refer(
214-
context.namePrinter.printLocalPropertyName(property.name),
215-
),
216-
printToJsonValue(
217-
context,
218-
property,
219-
context.namePrinter.printLocalPropertyName(property.name),
220-
))
221-
: printToJsonValue(
222-
context,
223-
property,
224-
context.namePrinter.printLocalPropertyName(property.name),
225-
),
211+
printToJsonValueOnExpression(
212+
context,
213+
property,
214+
printMaybeAddCast(
215+
refer(
216+
context.namePrinter.printLocalPropertyName(property.name),
217+
),
218+
property.hasDefaultValue && property.isNonNull,
219+
printClassPropertyType(context, property),
220+
),
221+
),
226222
)
227223
.statement,
228224
if (!property.isRequired) Code('}'),

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ Expression printToJsonValue(
160160
PrintContext context,
161161
ContextProperty property,
162162
String value,
163+
) =>
164+
_printToJsonValue(
165+
context,
166+
property.type,
167+
refer(value),
168+
property.path,
169+
);
170+
171+
Expression printToJsonValueOnExpression(
172+
PrintContext context,
173+
ContextProperty property,
174+
Expression value,
163175
) =>
164176
_printToJsonValue(
165177
context,
@@ -171,10 +183,9 @@ Expression printToJsonValue(
171183
Expression _printToJsonValue(
172184
PrintContext context,
173185
TypeNode type,
174-
String value,
186+
Expression valueRef,
175187
Name? propertyContext,
176188
) {
177-
final valueRef = refer(value);
178189
if (type is ListTypeNode) {
179190
final mappedAccess = (type.isNonNull
180191
? valueRef.property('map')
@@ -187,7 +198,7 @@ Expression _printToJsonValue(
187198
..body = _printToJsonValue(
188199
context,
189200
type.type,
190-
'e',
201+
refer('e'),
191202
propertyContext,
192203
).code,
193204
).closure

packages/graphql_codegen/lib/src/printer/utils.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,6 @@ Method printIdentityFunction() => Method(
204204
..requiredParameters = ListBuilder([Parameter((b) => b..name = 'i')])
205205
..body = refer('i').code,
206206
);
207+
208+
Expression printMaybeAddCast(Expression exp, bool condition, Expression asA) =>
209+
condition ? exp.asA(asA) : exp;

packages/graphql_codegen/test/assets/issue_293/doc_test.dart

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,72 @@ void main() {
3838
}),
3939
);
4040
});
41+
test(
42+
'It should send value non-required non-null values if provided on copy',
43+
() {
44+
expect(
45+
Variables$Query$Q(
46+
nonNull: false,
47+
nonNullList: [],
48+
nonNullEnum: Enum$E.V1,
49+
)
50+
.copyWith(
51+
nonNullWithDefault: true,
52+
nonNullListWithDefault: [null],
53+
nonNullEnumWithDefault: Enum$E.V1,
54+
)
55+
.toJson(),
56+
equals({
57+
'nonNull': false,
58+
'nonNullWithDefault': true,
59+
'nonNullList': [],
60+
'nonNullListWithDefault': [null],
61+
'nonNullEnum': 'V1',
62+
'nonNullEnumWithDefault': 'V1',
63+
}),
64+
);
65+
});
66+
test(
67+
'It should not send value non-required non-null values if null provided',
68+
() {
69+
expect(
70+
Variables$Query$Q(
71+
nonNull: false,
72+
nonNullWithDefault: null,
73+
nonNullList: [],
74+
nonNullListWithDefault: null,
75+
nonNullEnum: Enum$E.V1,
76+
nonNullEnumWithDefault: null,
77+
).toJson(),
78+
equals({
79+
'nonNull': false,
80+
'nonNullList': [],
81+
'nonNullEnum': 'V1',
82+
}),
83+
);
84+
});
85+
test(
86+
'It should not send value non-required non-null values if null provided (on copy)',
87+
() {
88+
expect(
89+
Variables$Query$Q(
90+
nonNull: false,
91+
nonNullList: [],
92+
nonNullEnum: Enum$E.V1,
93+
)
94+
.copyWith(
95+
nonNullWithDefault: null,
96+
nonNullListWithDefault: null,
97+
nonNullEnumWithDefault: null,
98+
)
99+
.toJson(),
100+
equals({
101+
'nonNull': false,
102+
'nonNullList': [],
103+
'nonNullEnum': 'V1',
104+
}),
105+
);
106+
});
41107
test('Equality should work', () {
42108
expect(
43109
Variables$Query$Q(

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class Input$I {
4949
result$data['nonNull'] = l$nonNull;
5050
if (_$data.containsKey('nonNullWithDefault')) {
5151
final l$nonNullWithDefault = nonNullWithDefault;
52-
result$data['nonNullWithDefault'] =
53-
l$nonNullWithDefault == null ? null : l$nonNullWithDefault;
52+
result$data['nonNullWithDefault'] = (l$nonNullWithDefault as bool);
5453
}
5554
if (_$data.containsKey('nullable')) {
5655
final l$nullable = nullable;
@@ -512,8 +511,7 @@ class Variables$Query$Q {
512511
result$data['nonNull'] = l$nonNull;
513512
if (_$data.containsKey('nonNullWithDefault')) {
514513
final l$nonNullWithDefault = nonNullWithDefault;
515-
result$data['nonNullWithDefault'] =
516-
l$nonNullWithDefault == null ? null : l$nonNullWithDefault;
514+
result$data['nonNullWithDefault'] = (l$nonNullWithDefault as bool);
517515
}
518516
if (_$data.containsKey('nullable')) {
519517
final l$nullable = nullable;
@@ -527,9 +525,8 @@ class Variables$Query$Q {
527525
result$data['nonNullEnum'] = toJson$Enum$E(l$nonNullEnum);
528526
if (_$data.containsKey('nonNullEnumWithDefault')) {
529527
final l$nonNullEnumWithDefault = nonNullEnumWithDefault;
530-
result$data['nonNullEnumWithDefault'] = l$nonNullEnumWithDefault == null
531-
? null
532-
: toJson$Enum$E(l$nonNullEnumWithDefault);
528+
result$data['nonNullEnumWithDefault'] =
529+
toJson$Enum$E((l$nonNullEnumWithDefault as Enum$E));
533530
}
534531
if (_$data.containsKey('nullableEnum')) {
535532
final l$nullableEnum = nullableEnum;
@@ -547,9 +544,8 @@ class Variables$Query$Q {
547544
l$nonNullList.map((e) => e == null ? null : toJson$Enum$E(e)).toList();
548545
if (_$data.containsKey('nonNullListWithDefault')) {
549546
final l$nonNullListWithDefault = nonNullListWithDefault;
550-
result$data['nonNullListWithDefault'] = l$nonNullListWithDefault == null
551-
? null
552-
: l$nonNullListWithDefault
547+
result$data['nonNullListWithDefault'] =
548+
(l$nonNullListWithDefault as List<Enum$E?>)
553549
.map((e) => e == null ? null : toJson$Enum$E(e))
554550
.toList();
555551
}

0 commit comments

Comments
 (0)