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 #302 from comigor/Nullable_scalar_mapping_types
Browse files Browse the repository at this point in the history
Nullable scalar_mapping types
  • Loading branch information
vasilich6107 authored Apr 30, 2021
2 parents 1d933e0 + 404453a commit aa066f6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 7.0.0-beta.9

- nullable scalar_mapping types

## 7.0.0-beta.8

- packages update
Expand Down
16 changes: 12 additions & 4 deletions lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,18 @@ Make sure your query is correct and your schema is updated.''');
dartType: false, schema: context.schema)
.dartTypeSafe);
final dartTypeSafeStr = TypeName(name: dartTypeName.dartTypeSafe);
jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.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';
}
}
} // On enums
else if (nextType is EnumTypeDefinitionNode) {
Expand Down
16 changes: 12 additions & 4 deletions lib/visitor/generator_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,18 @@ class GeneratorVisitor extends RecursiveVisitor {
dartType: false, schema: context.schema)
.dartTypeSafe);
final dartTypeSafeStr = TypeName(name: dartTypeName.dartTypeSafe);
jsonKeyAnnotation['fromJson'] =
'fromGraphQL${graphqlTypeSafeStr.dartTypeSafe}ToDart${dartTypeSafeStr.dartTypeSafe}';
jsonKeyAnnotation['toJson'] =
'fromDart${dartTypeSafeStr.dartTypeSafe}ToGraphQL${graphqlTypeSafeStr.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';
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 7.0.0-beta.8
version: 7.0.0-beta.9

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void main() {
input Input {
id: MyUuid!
idNullabe: MyUuid
}
''',
libraryDefinition: libraryDefinition,
Expand Down Expand Up @@ -94,6 +95,13 @@ final LibraryDefinition libraryDefinition =
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid)'
],
isResolveType: false),
ClassProperty(
type: TypeName(name: r'MyUuid'),
name: ClassPropertyName(name: r'idNullabe'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
],
isResolveType: false)
],
factoryPossibilities: {},
Expand All @@ -108,14 +116,14 @@ final LibraryDefinition libraryDefinition =
type: TypeName(name: r'MyUuid'),
name: QueryInputName(name: r'previousId'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid)'
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
]),
QueryInput(
type: ListOfTypeName(
typeName: TypeName(name: r'MyUuid'), isNonNull: false),
name: QueryInputName(name: r'listIds'),
annotations: [
r'JsonKey(fromJson: fromGraphQLListMyUuidToDartListMyUuid, toJson: fromDartListMyUuidToGraphQLListMyUuid)'
r'JsonKey(fromJson: fromGraphQLListMyUuidToDartListMyUuidNullable, toJson: fromDartListMyUuidToGraphQLListMyUuidNullable)'
])
],
generateHelpers: true,
Expand Down Expand Up @@ -169,7 +177,7 @@ class Custom$MutationRoot extends JsonSerializable with EquatableMixin {
@JsonSerializable(explicitToJson: true)
class Input extends JsonSerializable with EquatableMixin {
Input({required this.id});
Input({required this.id, this.idNullabe});
factory Input.fromJson(Map<String, dynamic> json) => _$InputFromJson(json);
Expand All @@ -178,8 +186,13 @@ class Input extends JsonSerializable with EquatableMixin {
toJson: fromDartMyUuidToGraphQLMyUuid)
late MyUuid id;
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
MyUuid? idNullabe;
@override
List<Object?> get props => [id];
List<Object?> get props => [id, idNullabe];
Map<String, dynamic> toJson() => _$InputToJson(this);
}
Expand All @@ -194,13 +207,13 @@ class CustomArguments extends JsonSerializable with EquatableMixin {
late Input input;
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyUuid,
toJson: fromDartMyUuidToGraphQLMyUuid)
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
final MyUuid? previousId;
@JsonKey(
fromJson: fromGraphQLListMyUuidToDartListMyUuid,
toJson: fromDartListMyUuidToGraphQLListMyUuid)
fromJson: fromGraphQLListMyUuidToDartListMyUuidNullable,
toJson: fromDartListMyUuidToGraphQLListMyUuidNullable)
final List<MyUuid?>? listIds;
@override
Expand Down
12 changes: 6 additions & 6 deletions test/query_generator/scalars/custom_scalars_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ final LibraryDefinition libraryDefinitionWithCustomParserFns =
type: TypeName(name: r'MyDartUuid'),
name: ClassPropertyName(name: r'a'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyDartUuid, toJson: fromDartMyDartUuidToGraphQLMyUuid)'
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyDartUuidNullable, toJson: fromDartMyDartUuidToGraphQLMyUuidNullable)'
],
isResolveType: false)
],
Expand All @@ -160,7 +160,7 @@ final LibraryDefinition libraryDefinitionWithCustomImports =
type: TypeName(name: r'MyUuid'),
name: ClassPropertyName(name: r'a'),
annotations: [
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid)'
r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuidNullable, toJson: fromDartMyUuidToGraphQLMyUuidNullable)'
],
isResolveType: false)
],
Expand Down Expand Up @@ -216,8 +216,8 @@ class Query$SomeObject extends JsonSerializable with EquatableMixin {
_$Query$SomeObjectFromJson(json);
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyDartUuid,
toJson: fromDartMyDartUuidToGraphQLMyUuid)
fromJson: fromGraphQLMyUuidToDartMyDartUuidNullable,
toJson: fromDartMyDartUuidToGraphQLMyUuidNullable)
MyDartUuid? a;
@override
Expand Down Expand Up @@ -245,8 +245,8 @@ class Query$SomeObject extends JsonSerializable with EquatableMixin {
_$Query$SomeObjectFromJson(json);
@JsonKey(
fromJson: fromGraphQLMyUuidToDartMyUuid,
toJson: fromDartMyUuidToGraphQLMyUuid)
fromJson: fromGraphQLMyUuidToDartMyUuidNullable,
toJson: fromDartMyUuidToGraphQLMyUuidNullable)
MyUuid? a;
@override
Expand Down

0 comments on commit aa066f6

Please sign in to comment.