Skip to content

Commit a0f1737

Browse files
authored
fix: Bad mapper for default value (heftapp#312)
1 parent 7c79931 commit a0f1737

11 files changed

Lines changed: 798 additions & 28 deletions

File tree

packages/graphql_codegen/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.13.6
2+
3+
- Fix bad copy class on list with default value.
4+
15
# 0.13.5
26

37
- Run builder before `json_serializable`

packages/graphql_codegen/example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ packages:
329329
path: ".."
330330
relative: true
331331
source: path
332-
version: "0.13.5"
332+
version: "0.13.6"
333333
graphql_flutter:
334334
dependency: "direct main"
335335
description:

packages/graphql_codegen/lib/src/context/context.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:gql/ast.dart';
44
import 'package:graphql_codegen/src/context/schema.dart';
55
import 'package:graphql_codegen/src/config/config.dart';
66
import 'package:graphql_codegen/src/errors.dart';
7+
import 'package:graphql_codegen/src/printer/base/utils.dart';
78
import 'package:graphql_codegen/src/transform/add_typename_transforming_visitor.dart';
89

910
import 'name.dart';
@@ -161,6 +162,9 @@ class ContextProperty {
161162
final List<DirectiveNode> fieldDirectives;
162163
final bool hasDefaultValue;
163164

165+
TypeNode get nullableTypeOnDefaultValue =>
166+
hasDefaultValue ? typeNodeAsNullable(type) : type;
167+
164168
NameNode get name => alias ?? _name;
165169

166170
ContextProperty({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Method? _printDeepCopyTypeNode(
7272
ContextProperty property,
7373
bool abstract,
7474
) {
75-
final node = property.type;
75+
final node = property.nullableTypeOnDefaultValue;
7676
final namePrinter = printContext.namePrinter;
7777
final propertyName = namePrinter.printPropertyName(property.name);
7878
final copyClassName = namePrinter.printCopyWithClassName(

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,6 @@ 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-
289
Method printEqualityOperator(
2910
PrintContext c,
3011
String name,
@@ -72,7 +53,7 @@ Method printEqualityOperator(
7253
),
7354
],
7455
_printPropertyEqualityCheck(
75-
e.hasDefaultValue ? _typeNodeAsNullable(e.type) : e.type,
56+
e.nullableTypeOnDefaultValue,
7657
localThisName,
7758
localOtherName,
7859
)
@@ -142,9 +123,7 @@ Method printHashCodeMethod(
142123
final localProp = context.namePrinter
143124
.printLocalPropertyName(property.name);
144125
final hash = _printPropertyHash(
145-
property.hasDefaultValue
146-
? _typeNodeAsNullable(property.type)
147-
: property.type,
126+
property.nullableTypeOnDefaultValue,
148127
refer(localProp),
149128
);
150129
if (dataObjectCheckResolver != null &&
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:gql/ast.dart';
2+
3+
TypeNode typeNodeAsNullable(TypeNode node) {
4+
if (!node.isNonNull) {
5+
return node;
6+
}
7+
if (node is ListTypeNode) {
8+
return ListTypeNode(
9+
type: node.type,
10+
isNonNull: false,
11+
);
12+
}
13+
if (node is NamedTypeNode) {
14+
return NamedTypeNode(
15+
name: node.name,
16+
isNonNull: false,
17+
);
18+
}
19+
return node;
20+
}

packages/graphql_codegen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: |
33
Simple, opinionated, codegen library for GraphQL. It allows you to
44
generate serializers and client helpers to easily call and parse your data.
55
6-
version: 0.13.5
6+
version: 0.13.6
77
homepage: https://github.com/heftapp/graphql_codegen/tree/main/packages/graphql_codegen
88
repository: https://github.com/heftapp/graphql_codegen/tree/main/packages/graphql_codegen
99

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input TemplateDisciplineTopicContentInput {
2+
howStudyIt: String!
3+
infoContentBlocks: [I1!]!
4+
taskContentBlocks: [I2!]
5+
testContentBlocks: [I3!]! = []
6+
whyStudyIt: String!
7+
}
8+
9+
input I1 {
10+
data: String
11+
}
12+
13+
input I2 {
14+
data: String
15+
}
16+
17+
input I3 {
18+
data: String
19+
}

0 commit comments

Comments
 (0)