Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit e709b2d

Browse files
authored
Merge pull request #334 from comigor/coercers-fix
coercers fix
2 parents 8993143 + f9aabe1 commit e709b2d

File tree

8 files changed

+156
-61
lines changed

8 files changed

+156
-61
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 7.1.0-beta.0
4+
5+
**BREAKING CHANGE**
6+
7+
- changed the naming of scalar mappers
8+
39
## 7.0.0-beta.17
410

511
- example indentaion fix

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,33 @@ Each `SchemaMap` is configured this way:
125125
| Option | Default value | Description |
126126
| - | - | - |
127127
| `output` | | Relative path to output the generated code. It should end with `.graphql.dart` or else the generator will need to generate one more file. |
128-
| `schema` | | Relative path to the GraphQL schema. |
129-
| `queries_glob` | | Glob that selects all query files to be used with this schema. |
130-
| `naming_scheme` | `pathedWithTypes` | The naming scheme to be used on generated classes names. `pathedWithTypes` is the default for retrocompatibility, where the names of previous types are used as prefix of the next class. This can generate duplication on certain schemas. With `pathedWithFields`, the names of previous fields are used as prefix of the next class and with `simple`, only the actual GraphQL class nameis considered. |
131-
| `type_name_field` | `__typename` | The name of the field used to differentiate interfaces and union types (commonly `__typename` or `__resolveType`). Note that `__typename` field are not added automatically to the query. If you want interface/union type resolution, you need to manually add it there or set `append_type_name` to `true`. |
128+
| `schema` | | Relative path to the GraphQL schema. | | `queries_glob` | | Glob that selects all query files to be used
129+
with this schema. | | `naming_scheme` | `pathedWithTypes` | The naming scheme to be used on generated classes
130+
names. `pathedWithTypes` is the default for retrocompatibility, where the names of previous types are used as prefix of
131+
the next class. This can generate duplication on certain schemas. With `pathedWithFields`, the names of previous fields
132+
are used as prefix of the next class and with `simple`, only the actual GraphQL class nameis considered. |
133+
| `type_name_field` | `__typename` | The name of the field used to differentiate interfaces and union types (
134+
commonly `__typename` or `__resolveType`). Note that `__typename` field are not added automatically to the query. If you
135+
want interface/union type resolution, you need to manually add it there or set `append_type_name` to `true`. |
132136
| `append_type_name` | `false` | Appends `type_name_field` value to the query selections set. |
133137

134138
See [examples](./example) for more information and configuration options.
135139

136140
### **Custom scalars**
137-
If your schema uses custom scalars, they must be defined on `build.yaml`. If it needs a custom parser (to decode from/to json), the `custom_parser_import` path must be set and the file must implement both `fromGraphQL___ToDart___` and `fromDart___toGraphQL___` constant functions.
141+
142+
If your schema uses custom scalars, they must be defined on `build.yaml`. If it needs a custom parser (to decode from/to
143+
json), the `custom_parser_import` path must be set and the file must implement both `fromGraphQL___ToDart___`
144+
and `fromDart___toGraphQL___` constant functions.
145+
`___ToDart___` and `___toGraphQL___` should be named including nullability, here is an example
146+
147+
`file: Upload` => `fromGraphQLUploadNullableToDartMultipartFileNullable`
148+
and `fromDartMultipartFileNullableToGraphQLUploadNullable`
149+
`file: Upload!` => `fromGraphQLUploadToDartMultipartFile` and `fromDartMultipartFileToGraphQLUpload`
150+
`file: [Upload]` => `fromGraphQLListNullableUploadNullableToDartListNullableMultipartFileNullable`
151+
and `fromDartListNullableMultipartFileNullableToGraphQLListNullableUploadNullable`
152+
`file: [Upload]!` => `fromGraphQLListUploadNullableToDartListMultipartFileNullable`
153+
and `fromDartListMultipartFileNullableToGraphQLListUploadNullable`
154+
`file: [Upload!]!` => `fromGraphQLListUploadToDartListMultipartFile` and `fromDartListMultipartFileToGraphQLListUpload`
138155

139156
```yaml
140157
targets:

lib/generator.dart

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -349,24 +349,13 @@ Make sure your query is correct and your schema is updated.''');
349349

350350
if (scalar?.customParserImport != null &&
351351
nextType.name.value == scalar?.graphQLType) {
352-
final graphqlTypeSafeStr = TypeName(
353-
name: gql
354-
.buildTypeName(fieldType, context.options,
355-
dartType: false, schema: context.schema)
356-
.dartTypeSafe);
357-
final dartTypeSafeStr = TypeName(name: dartTypeName.dartTypeSafe);
358-
359-
if (fieldType.isNonNull) {
360-
jsonKeyAnnotation['fromJson'] =
361-
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}';
362-
jsonKeyAnnotation['toJson'] =
363-
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}';
364-
} else {
365-
jsonKeyAnnotation['fromJson'] =
366-
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}Nullable';
367-
jsonKeyAnnotation['toJson'] =
368-
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}Nullable';
369-
}
352+
final graphqlTypeName = gql.buildTypeName(fieldType, context.options,
353+
dartType: false, schema: context.schema);
354+
355+
jsonKeyAnnotation['fromJson'] =
356+
'fromGraphQL${graphqlTypeName.parserSafe}ToDart${dartTypeName.parserSafe}';
357+
jsonKeyAnnotation['toJson'] =
358+
'fromDart${dartTypeName.parserSafe}ToGraphQL${graphqlTypeName.parserSafe}';
370359
}
371360
} // On enums
372361
else if (nextType is EnumTypeDefinitionNode) {

lib/generator/data/definition.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ abstract class Name extends Equatable with DataPrinter {
2525
/// type name safe to use for dart
2626
String get dartTypeSafe => namePrintable.replaceAll(RegExp(r'[<>?]'), '');
2727

28+
/// type name safe to use for dart
29+
String get parserSafe {
30+
final reGeneric = RegExp(r'<([\S]*)>');
31+
final reNull = RegExp(r'[?]');
32+
33+
return [
34+
namePrintable.replaceAll(reGeneric, '').replaceAll(reNull, 'Nullable'),
35+
reGeneric
36+
.allMatches(namePrintable)
37+
.map((e) => e.group(1))
38+
.join('')
39+
.replaceAll(reNull, 'Nullable')
40+
].join('');
41+
}
42+
2843
/// Name normalization function
2944
String normalize(String name) => normalizeName(name);
3045
}

lib/visitor/generator_visitor.dart

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,17 @@ class GeneratorVisitor extends RecursiveVisitor {
183183

184184
if (scalar?.customParserImport != null &&
185185
leafType.name.value == scalar?.graphQLType) {
186-
final graphqlTypeSafeStr = TypeName(
187-
name: gql
188-
.buildTypeName(node.type, context.options,
189-
dartType: false, schema: context.schema)
190-
.dartTypeSafe);
191-
final dartTypeSafeStr = TypeName(name: dartTypeName.dartTypeSafe);
192-
193-
if (node.type.isNonNull) {
194-
jsonKeyAnnotation['fromJson'] =
195-
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}';
196-
jsonKeyAnnotation['toJson'] =
197-
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}';
198-
} else {
199-
jsonKeyAnnotation['fromJson'] =
200-
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}Nullable';
201-
jsonKeyAnnotation['toJson'] =
202-
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}Nullable';
203-
}
186+
final graphqlTypeName = gql.buildTypeName(
187+
node.type,
188+
context.options,
189+
dartType: false,
190+
schema: context.schema,
191+
);
192+
193+
jsonKeyAnnotation['fromJson'] =
194+
'fromGraphQL${graphqlTypeName.parserSafe}ToDart${dartTypeName.parserSafe}';
195+
jsonKeyAnnotation['toJson'] =
196+
'fromDart${dartTypeName.parserSafe}ToGraphQL${graphqlTypeName.parserSafe}';
204197
}
205198
}
206199

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: artemis
2-
version: 7.0.0-beta.17
2+
version: 7.1.0-beta.0
33

44
description: Build dart types from GraphQL schemas and queries (using Introspection Query).
55
homepage: https://github.com/comigor/artemis
@@ -30,11 +30,11 @@ dependencies:
3030

3131
dev_dependencies:
3232
args: ^2.1.1
33-
build_runner: ^2.0.4
33+
build_runner: ^2.0.5
3434
build_test: ^2.1.0
3535
json_serializable: ^4.1.3
3636
build_resolvers: ^2.0.3
3737
pedantic: ^1.11.1
38-
test: ^1.17.8
38+
test: ^1.17.9
3939
logging: ^1.0.1
4040

test/query_generator/mutations_and_inputs/custom_scalars_on_input_objects_test.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ final LibraryDefinition libraryDefinition =
100100
type: TypeName(name: r'MyUuid'),
101101
name: ClassPropertyName(name: r'idNullabe'),
102102
annotations: [
103-
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
103+
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)'
104104
],
105105
isResolveType: false)
106106
],
@@ -116,14 +116,14 @@ final LibraryDefinition libraryDefinition =
116116
type: TypeName(name: r'MyUuid'),
117117
name: QueryInputName(name: r'previousId'),
118118
annotations: [
119-
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
119+
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)'
120120
]),
121121
QueryInput(
122122
type: ListOfTypeName(
123123
typeName: TypeName(name: r'MyUuid'), isNonNull: false),
124124
name: QueryInputName(name: r'listIds'),
125125
annotations: [
126-
r'JsonKey(fromJson: fromGraphQLListMyUuidToDartListMyUuidNullable, toJson: fromDartListMyUuidToGraphQLListMyUuidNullable)'
126+
r'JsonKey(fromJson: fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable, toJson: fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)'
127127
])
128128
],
129129
generateHelpers: true,
@@ -189,8 +189,8 @@ class Input extends JsonSerializable with EquatableMixin {
189189
late MyUuid id;
190190
191191
@JsonKey(
192-
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
193-
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
192+
fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable,
193+
toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)
194194
MyUuid? idNullabe;
195195
196196
@override
@@ -210,13 +210,15 @@ class CustomArguments extends JsonSerializable with EquatableMixin {
210210
late Input input;
211211
212212
@JsonKey(
213-
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
214-
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
213+
fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable,
214+
toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)
215215
final MyUuid? previousId;
216216
217217
@JsonKey(
218-
fromJson: fromGraphQLListMyUuidToDartListMyUuidNullable,
219-
toJson: fromDartListMyUuidToGraphQLListMyUuidNullable)
218+
fromJson:
219+
fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable,
220+
toJson:
221+
fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)
220222
final List<MyUuid?>? listIds;
221223
222224
@override

test/query_generator/scalars/custom_scalars_test.dart

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void main() {
6767
test(
6868
'When they need custom imports',
6969
() async => testGenerator(
70-
query: 'query query { a }',
70+
query: 'query query { a, b, c, d, e, f }',
7171
schema: r'''
7272
scalar MyUuid
7373
@@ -77,6 +77,11 @@ void main() {
7777
7878
type SomeObject {
7979
a: MyUuid
80+
b: MyUuid!
81+
c: [MyUuid!]!
82+
d: [MyUuid]
83+
e: [MyUuid]!
84+
f: [MyUuid!]
8085
}
8186
''',
8287
libraryDefinition: libraryDefinitionWithCustomImports,
@@ -133,7 +138,7 @@ final LibraryDefinition libraryDefinitionWithCustomParserFns =
133138
type: TypeName(name: r'MyDartUuid'),
134139
name: ClassPropertyName(name: r'a'),
135140
annotations: [
136-
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyDartUuidNullable, toJson: fromDartMyDartUuidToGraphQLMyUuidNullable)'
141+
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyDartUuidNullable, toJson: fromDartMyDartUuidNullableToGraphQLMyUuidNullable)'
137142
],
138143
isResolveType: false)
139144
],
@@ -160,7 +165,48 @@ final LibraryDefinition libraryDefinitionWithCustomImports =
160165
type: TypeName(name: r'MyUuid'),
161166
name: ClassPropertyName(name: r'a'),
162167
annotations: [
163-
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
168+
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)'
169+
],
170+
isResolveType: false),
171+
ClassProperty(
172+
type: TypeName(name: r'MyUuid', isNonNull: true),
173+
name: ClassPropertyName(name: r'b'),
174+
annotations: [
175+
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid)'
176+
],
177+
isResolveType: false),
178+
ClassProperty(
179+
type: ListOfTypeName(
180+
typeName: TypeName(name: r'MyUuid', isNonNull: true),
181+
isNonNull: true),
182+
name: ClassPropertyName(name: r'c'),
183+
annotations: [
184+
r'JsonKey(fromJson: fromGraphQLListMyUuidToDartListMyUuid, toJson: fromDartListMyUuidToGraphQLListMyUuid)'
185+
],
186+
isResolveType: false),
187+
ClassProperty(
188+
type: ListOfTypeName(
189+
typeName: TypeName(name: r'MyUuid'), isNonNull: false),
190+
name: ClassPropertyName(name: r'd'),
191+
annotations: [
192+
r'JsonKey(fromJson: fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable, toJson: fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)'
193+
],
194+
isResolveType: false),
195+
ClassProperty(
196+
type: ListOfTypeName(
197+
typeName: TypeName(name: r'MyUuid'), isNonNull: true),
198+
name: ClassPropertyName(name: r'e'),
199+
annotations: [
200+
r'JsonKey(fromJson: fromGraphQLListMyUuidNullableToDartListMyUuidNullable, toJson: fromDartListMyUuidNullableToGraphQLListMyUuidNullable)'
201+
],
202+
isResolveType: false),
203+
ClassProperty(
204+
type: ListOfTypeName(
205+
typeName: TypeName(name: r'MyUuid', isNonNull: true),
206+
isNonNull: false),
207+
name: ClassPropertyName(name: r'f'),
208+
annotations: [
209+
r'JsonKey(fromJson: fromGraphQLListNullableMyUuidToDartListNullableMyUuid, toJson: fromDartListNullableMyUuidToGraphQLListNullableMyUuid)'
164210
],
165211
isResolveType: false)
166212
],
@@ -217,8 +263,8 @@ class Query$SomeObject extends JsonSerializable with EquatableMixin {
217263
_$Query$SomeObjectFromJson(json);
218264
219265
@JsonKey(
220-
fromJson: fromGraphQLMyUuidToDartMyDartUuidNullable,
221-
toJson: fromDartMyDartUuidToGraphQLMyUuidNullable)
266+
fromJson: fromGraphQLMyUuidNullableToDartMyDartUuidNullable,
267+
toJson: fromDartMyDartUuidNullableToGraphQLMyUuidNullable)
222268
MyDartUuid? a;
223269
224270
@override
@@ -247,12 +293,39 @@ class Query$SomeObject extends JsonSerializable with EquatableMixin {
247293
_$Query$SomeObjectFromJson(json);
248294
249295
@JsonKey(
250-
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
251-
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
296+
fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable,
297+
toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)
252298
MyUuid? a;
253299
300+
@JsonKey(
301+
fromJson: fromGraphQLMyUuidToDartMyUuid,
302+
toJson: fromDartMyUuidToGraphQLMyUuid)
303+
late MyUuid b;
304+
305+
@JsonKey(
306+
fromJson: fromGraphQLListMyUuidToDartListMyUuid,
307+
toJson: fromDartListMyUuidToGraphQLListMyUuid)
308+
late List<MyUuid> c;
309+
310+
@JsonKey(
311+
fromJson:
312+
fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable,
313+
toJson:
314+
fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)
315+
List<MyUuid?>? d;
316+
317+
@JsonKey(
318+
fromJson: fromGraphQLListMyUuidNullableToDartListMyUuidNullable,
319+
toJson: fromDartListMyUuidNullableToGraphQLListMyUuidNullable)
320+
late List<MyUuid?> e;
321+
322+
@JsonKey(
323+
fromJson: fromGraphQLListNullableMyUuidToDartListNullableMyUuid,
324+
toJson: fromDartListNullableMyUuidToGraphQLListNullableMyUuid)
325+
List<MyUuid>? f;
326+
254327
@override
255-
List<Object?> get props => [a];
328+
List<Object?> get props => [a, b, c, d, e, f];
256329
@override
257330
Map<String, dynamic> toJson() => _$Query$SomeObjectToJson(this);
258331
}

0 commit comments

Comments
 (0)