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

Commit

Permalink
Merge pull request #334 from comigor/coercers-fix
Browse files Browse the repository at this point in the history
coercers fix
  • Loading branch information
vasilich6107 authored Jun 29, 2021
2 parents 8993143 + f9aabe1 commit e709b2d
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 61 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 7.1.0-beta.0

**BREAKING CHANGE**

- changed the naming of scalar mappers

## 7.0.0-beta.17

- example indentaion fix
Expand Down
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,33 @@ Each `SchemaMap` is configured this way:
| Option | Default value | Description |
| - | - | - |
| `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. |
| `schema` | | Relative path to the GraphQL schema. |
| `queries_glob` | | Glob that selects all query files to be used with this schema. |
| `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. |
| `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`. |
| `schema` | | Relative path to the GraphQL schema. | | `queries_glob` | | Glob that selects all query files to be used
with this schema. | | `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. |
| `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`. |
| `append_type_name` | `false` | Appends `type_name_field` value to the query selections set. |

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

### **Custom scalars**
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.

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.
`___ToDart___` and `___toGraphQL___` should be named including nullability, here is an example

`file: Upload` => `fromGraphQLUploadNullableToDartMultipartFileNullable`
and `fromDartMultipartFileNullableToGraphQLUploadNullable`
`file: Upload!` => `fromGraphQLUploadToDartMultipartFile` and `fromDartMultipartFileToGraphQLUpload`
`file: [Upload]` => `fromGraphQLListNullableUploadNullableToDartListNullableMultipartFileNullable`
and `fromDartListNullableMultipartFileNullableToGraphQLListNullableUploadNullable`
`file: [Upload]!` => `fromGraphQLListUploadNullableToDartListMultipartFileNullable`
and `fromDartListMultipartFileNullableToGraphQLListUploadNullable`
`file: [Upload!]!` => `fromGraphQLListUploadToDartListMultipartFile` and `fromDartListMultipartFileToGraphQLListUpload`

```yaml
targets:
Expand Down
25 changes: 7 additions & 18 deletions lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,24 +349,13 @@ Make sure your query is correct and your schema is updated.''');

if (scalar?.customParserImport != null &&
nextType.name.value == scalar?.graphQLType) {
final graphqlTypeSafeStr = TypeName(
name: gql
.buildTypeName(fieldType, context.options,
dartType: false, schema: context.schema)
.dartTypeSafe);
final dartTypeSafeStr = TypeName(name: dartTypeName.dartTypeSafe);

if (fieldType.isNonNull) {
jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}';
} else {
jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}Nullable';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}Nullable';
}
final graphqlTypeName = gql.buildTypeName(fieldType, context.options,
dartType: false, schema: context.schema);

jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeName.parserSafe}ToDart${dartTypeName.parserSafe}';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeName.parserSafe}ToGraphQL${graphqlTypeName.parserSafe}';
}
} // On enums
else if (nextType is EnumTypeDefinitionNode) {
Expand Down
15 changes: 15 additions & 0 deletions lib/generator/data/definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ abstract class Name extends Equatable with DataPrinter {
/// type name safe to use for dart
String get dartTypeSafe => namePrintable.replaceAll(RegExp(r'[<>?]'), '');

/// type name safe to use for dart
String get parserSafe {
final reGeneric = RegExp(r'<([\S]*)>');
final reNull = RegExp(r'[?]');

return [
namePrintable.replaceAll(reGeneric, '').replaceAll(reNull, 'Nullable'),
reGeneric
.allMatches(namePrintable)
.map((e) => e.group(1))
.join('')
.replaceAll(reNull, 'Nullable')
].join('');
}

/// Name normalization function
String normalize(String name) => normalizeName(name);
}
29 changes: 11 additions & 18 deletions lib/visitor/generator_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,17 @@ class GeneratorVisitor extends RecursiveVisitor {

if (scalar?.customParserImport != null &&
leafType.name.value == scalar?.graphQLType) {
final graphqlTypeSafeStr = TypeName(
name: gql
.buildTypeName(node.type, context.options,
dartType: false, schema: context.schema)
.dartTypeSafe);
final dartTypeSafeStr = TypeName(name: dartTypeName.dartTypeSafe);

if (node.type.isNonNull) {
jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}';
} else {
jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}Nullable';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.dartTypeSafe}Nullable';
}
final graphqlTypeName = gql.buildTypeName(
node.type,
context.options,
dartType: false,
schema: context.schema,
);

jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeName.parserSafe}ToDart${dartTypeName.parserSafe}';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeName.parserSafe}ToGraphQL${graphqlTypeName.parserSafe}';
}
}

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 7.0.0-beta.17
version: 7.1.0-beta.0

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

dev_dependencies:
args: ^2.1.1
build_runner: ^2.0.4
build_runner: ^2.0.5
build_test: ^2.1.0
json_serializable: ^4.1.3
build_resolvers: ^2.0.3
pedantic: ^1.11.1
test: ^1.17.8
test: ^1.17.9
logging: ^1.0.1

Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final LibraryDefinition libraryDefinition =
type: TypeName(name: r'MyUuid'),
name: ClassPropertyName(name: r'idNullabe'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)'
],
isResolveType: false)
],
Expand All @@ -116,14 +116,14 @@ final LibraryDefinition libraryDefinition =
type: TypeName(name: r'MyUuid'),
name: QueryInputName(name: r'previousId'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)'
]),
QueryInput(
type: ListOfTypeName(
typeName: TypeName(name: r'MyUuid'), isNonNull: false),
name: QueryInputName(name: r'listIds'),
annotations: [
r'JsonKey(fromJson: fromGraphQLListMyUuidToDartListMyUuidNullable, toJson: fromDartListMyUuidToGraphQLListMyUuidNullable)'
r'JsonKey(fromJson: fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable, toJson: fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)'
])
],
generateHelpers: true,
Expand Down Expand Up @@ -189,8 +189,8 @@ class Input extends JsonSerializable with EquatableMixin {
late MyUuid id;
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable,
toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)
MyUuid? idNullabe;
@override
Expand All @@ -210,13 +210,15 @@ class CustomArguments extends JsonSerializable with EquatableMixin {
late Input input;
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable,
toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)
final MyUuid? previousId;
@JsonKey(
fromJson: fromGraphQLListMyUuidToDartListMyUuidNullable,
toJson: fromDartListMyUuidToGraphQLListMyUuidNullable)
fromJson:
fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable,
toJson:
fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)
final List<MyUuid?>? listIds;
@override
Expand Down
89 changes: 81 additions & 8 deletions test/query_generator/scalars/custom_scalars_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void main() {
test(
'When they need custom imports',
() async => testGenerator(
query: 'query query { a }',
query: 'query query { a, b, c, d, e, f }',
schema: r'''
scalar MyUuid
Expand All @@ -77,6 +77,11 @@ void main() {
type SomeObject {
a: MyUuid
b: MyUuid!
c: [MyUuid!]!
d: [MyUuid]
e: [MyUuid]!
f: [MyUuid!]
}
''',
libraryDefinition: libraryDefinitionWithCustomImports,
Expand Down Expand Up @@ -133,7 +138,7 @@ final LibraryDefinition libraryDefinitionWithCustomParserFns =
type: TypeName(name: r'MyDartUuid'),
name: ClassPropertyName(name: r'a'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyDartUuidNullable, toJson: fromDartMyDartUuidToGraphQLMyUuidNullable)'
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyDartUuidNullable, toJson: fromDartMyDartUuidNullableToGraphQLMyUuidNullable)'
],
isResolveType: false)
],
Expand All @@ -160,7 +165,48 @@ final LibraryDefinition libraryDefinitionWithCustomImports =
type: TypeName(name: r'MyUuid'),
name: ClassPropertyName(name: r'a'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)'
],
isResolveType: false),
ClassProperty(
type: TypeName(name: r'MyUuid', isNonNull: true),
name: ClassPropertyName(name: r'b'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid)'
],
isResolveType: false),
ClassProperty(
type: ListOfTypeName(
typeName: TypeName(name: r'MyUuid', isNonNull: true),
isNonNull: true),
name: ClassPropertyName(name: r'c'),
annotations: [
r'JsonKey(fromJson: fromGraphQLListMyUuidToDartListMyUuid, toJson: fromDartListMyUuidToGraphQLListMyUuid)'
],
isResolveType: false),
ClassProperty(
type: ListOfTypeName(
typeName: TypeName(name: r'MyUuid'), isNonNull: false),
name: ClassPropertyName(name: r'd'),
annotations: [
r'JsonKey(fromJson: fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable, toJson: fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)'
],
isResolveType: false),
ClassProperty(
type: ListOfTypeName(
typeName: TypeName(name: r'MyUuid'), isNonNull: true),
name: ClassPropertyName(name: r'e'),
annotations: [
r'JsonKey(fromJson: fromGraphQLListMyUuidNullableToDartListMyUuidNullable, toJson: fromDartListMyUuidNullableToGraphQLListMyUuidNullable)'
],
isResolveType: false),
ClassProperty(
type: ListOfTypeName(
typeName: TypeName(name: r'MyUuid', isNonNull: true),
isNonNull: false),
name: ClassPropertyName(name: r'f'),
annotations: [
r'JsonKey(fromJson: fromGraphQLListNullableMyUuidToDartListNullableMyUuid, toJson: fromDartListNullableMyUuidToGraphQLListNullableMyUuid)'
],
isResolveType: false)
],
Expand Down Expand Up @@ -217,8 +263,8 @@ class Query$SomeObject extends JsonSerializable with EquatableMixin {
_$Query$SomeObjectFromJson(json);
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyDartUuidNullable,
toJson: fromDartMyDartUuidToGraphQLMyUuidNullable)
fromJson: fromGraphQLMyUuidNullableToDartMyDartUuidNullable,
toJson: fromDartMyDartUuidNullableToGraphQLMyUuidNullable)
MyDartUuid? a;
@override
Expand Down Expand Up @@ -247,12 +293,39 @@ class Query$SomeObject extends JsonSerializable with EquatableMixin {
_$Query$SomeObjectFromJson(json);
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable,
toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)
MyUuid? a;
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyUuid,
toJson: fromDartMyUuidToGraphQLMyUuid)
late MyUuid b;
@JsonKey(
fromJson: fromGraphQLListMyUuidToDartListMyUuid,
toJson: fromDartListMyUuidToGraphQLListMyUuid)
late List<MyUuid> c;
@JsonKey(
fromJson:
fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable,
toJson:
fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)
List<MyUuid?>? d;
@JsonKey(
fromJson: fromGraphQLListMyUuidNullableToDartListMyUuidNullable,
toJson: fromDartListMyUuidNullableToGraphQLListMyUuidNullable)
late List<MyUuid?> e;
@JsonKey(
fromJson: fromGraphQLListNullableMyUuidToDartListNullableMyUuid,
toJson: fromDartListNullableMyUuidToGraphQLListNullableMyUuid)
List<MyUuid>? f;
@override
List<Object?> get props => [a];
List<Object?> get props => [a, b, c, d, e, f];
@override
Map<String, dynamic> toJson() => _$Query$SomeObjectToJson(this);
}
Expand Down

0 comments on commit e709b2d

Please sign in to comment.