Skip to content

Commit

Permalink
remove nullable feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilich6107 committed Jul 16, 2021
1 parent e709b2d commit 7e6a420
Show file tree
Hide file tree
Showing 44 changed files with 1,357 additions and 371 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Files and directories created by pub
.dart_tool/
.packages
.fvm
build/
# If you're building an application, you may want to check-in your pubspec.lock
/pubspec.lock
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 7.1.1-beta.0

- exclude nullable items from input

## 7.1.0-beta.0

**BREAKING CHANGE**
Expand Down
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,30 @@ json), the `custom_parser_import` path must be set and the file must implement b
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`
`file: Upload`

- `fromGraphQLUploadNullableToDartMultipartFileNullable`
- `fromDartMultipartFileNullableToGraphQLUploadNullable`

`file: Upload!`

- `fromGraphQLUploadToDartMultipartFile`
- `fromDartMultipartFileToGraphQLUpload`

`file: [Upload]`

- `fromGraphQLListNullableUploadNullableToDartListNullableMultipartFileNullable`
- `fromDartListNullableMultipartFileNullableToGraphQLListNullableUploadNullable`

`file: [Upload]!`

- `fromGraphQLListUploadNullableToDartListMultipartFileNullable`
- `fromDartListMultipartFileNullableToGraphQLListUploadNullable`

`file: [Upload!]!`

- `fromGraphQLListUploadToDartListMultipartFile`
- `fromDartListMultipartFileToGraphQLListUpload`

```yaml
targets:
Expand Down
10 changes: 9 additions & 1 deletion lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ class GraphQLQueryBuilder implements Builder {

writeLibraryDefinitionToBuffer(
buffer,
options.ignoreForFile,
options,
schemaMap,
libDefinition,
);

Expand All @@ -196,6 +197,13 @@ class GraphQLQueryBuilder implements Builder {
await buildStep.writeAsString(
forwarderOutputFileId, writeLibraryForwarder(libDefinition));
}

if (options.schemaMapping.any((e) => e.toJsonExclude)) {
final nullableOptputFileId =
AssetId(buildStep.inputId.package, 'nullable.graphql.dart');
await buildStep.writeAsString(
nullableOptputFileId, writeLibraryForwarder(libDefinition));
}
}
}
}
17 changes: 10 additions & 7 deletions lib/generator.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:artemis/generator/data/annotation.dart';
import 'package:artemis/generator/data/data.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:artemis/generator/data/nullable.dart';
Expand All @@ -8,6 +9,7 @@ import 'package:artemis/visitor/schema_definition_visitor.dart';
import 'package:artemis/visitor/type_definition_node_visitor.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:gql/ast.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:path/path.dart' as p;

import './generator/ephemeral_data.dart';
Expand Down Expand Up @@ -265,7 +267,11 @@ ClassProperty createClassProperty({
return ClassProperty(
type: TypeName(name: 'String'),
name: fieldName,
annotations: ['JsonKey(name: \'${context.schemaMap.typeNameField}\')'],
annotations: [
JsonKeyAnnotation(
jsonKey: JsonKeyItem(name: context.schemaMap.typeNameField),
)
],
isResolveType: true,
);
}
Expand Down Expand Up @@ -341,7 +347,7 @@ Make sure your query is correct and your schema is updated.''');
// On custom scalars
final jsonKeyAnnotation = <String, String>{};
if (name.namePrintable != name.name) {
jsonKeyAnnotation['name'] = '\'${name.name}\'';
jsonKeyAnnotation['name'] = name.name;
}

if (nextType is ScalarTypeDefinitionNode) {
Expand Down Expand Up @@ -380,13 +386,10 @@ Make sure your query is correct and your schema is updated.''');
final fieldDirectives =
regularField?.directives ?? regularInputField?.directives;

var annotations = <String>[];
var annotations = <Annotation>[];

if (jsonKeyAnnotation.isNotEmpty) {
final jsonKey = jsonKeyAnnotation.entries
.map<String>((e) => '${e.key}: ${e.value}')
.join(', ');
annotations.add('JsonKey($jsonKey)');
annotations.add(JsonKeyAnnotation.fromMap(jsonKeyAnnotation));
}
annotations.addAll(proceedDeprecated(fieldDirectives));

Expand Down
158 changes: 158 additions & 0 deletions lib/generator/data/annotation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import 'package:artemis/generator/data_printer.dart';
import 'package:artemis/generator/data/definition.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

abstract class Annotation extends Equatable with DataPrinter {
String toAnnotation();
}

class JsonKeyItem extends Equatable with DataPrinter {
final String? defaultValue;

final String? disallowNullValue;

final String? fromJson;

final String? ignore;

final String? includeIfNull;

final String? name;

final String? required;

final String? toJson;

final String? unknownEnumValue;

const JsonKeyItem({
this.defaultValue,
this.disallowNullValue,
this.fromJson,
this.ignore,
this.includeIfNull,
this.name,
this.required,
this.toJson,
this.unknownEnumValue,
});

@override
Map<String, Object?> get namedProps {
return {
'defaultValue': defaultValue,
'disallowNullValue': disallowNullValue,
'fromJson': fromJson,
'ignore': ignore,
'includeIfNull': includeIfNull,
'name': name,
'required': required,
'toJson': toJson,
'unknownEnumValue': unknownEnumValue,
};
}

JsonKeyItem copyWith({
String? defaultValue,
String? disallowNullValue,
String? fromJson,
String? ignore,
String? includeIfNull,
String? name,
String? required,
String? toJson,
String? unknownEnumValue,
}) {
return JsonKeyItem(
defaultValue: defaultValue ?? this.defaultValue,
disallowNullValue: disallowNullValue ?? this.disallowNullValue,
fromJson: fromJson ?? this.fromJson,
ignore: ignore ?? this.ignore,
includeIfNull: includeIfNull ?? this.includeIfNull,
name: name ?? this.name,
required: required ?? this.required,
toJson: toJson ?? this.toJson,
unknownEnumValue: unknownEnumValue ?? this.unknownEnumValue,
);
}
}

class StringAnnotation extends Annotation {
final String name;

StringAnnotation({required this.name});

@override
String toAnnotation() => name;

@override
Map<String, Object?> get namedProps => {
'name': name,
};
}

class OverrideAnnotation extends StringAnnotation {
OverrideAnnotation() : super(name: 'override');
}

class JsonKeyAnnotation extends Annotation {
final JsonKeyItem jsonKey;

JsonKeyAnnotation({required this.jsonKey});

factory JsonKeyAnnotation.fromMap(Map<String, dynamic> jsonKeyAnnotation) {
return JsonKeyAnnotation(
jsonKey: JsonKeyItem(
defaultValue: jsonKeyAnnotation['defaultValue'] as String?,
disallowNullValue: jsonKeyAnnotation['disallowNullValue'] as String?,
fromJson: jsonKeyAnnotation['fromJson'] as String?,
ignore: jsonKeyAnnotation['ignore'] as String?,
includeIfNull: jsonKeyAnnotation['includeIfNull'] as String?,
name: jsonKeyAnnotation['name'] as String?,
required: jsonKeyAnnotation['required'] as String?,
toJson: jsonKeyAnnotation['toJson'] as String?,
unknownEnumValue: jsonKeyAnnotation['unknownEnumValue'] as String?,
),
);
}

@override
String toAnnotation() {
final jsonKeyAnnotation = <String, dynamic>{
'disallowNullValue': jsonKey.disallowNullValue,
'fromJson': jsonKey.fromJson,
'ignore': jsonKey.ignore,
'includeIfNull': jsonKey.includeIfNull,
'name': jsonKey.name,
'required': jsonKey.required,
'toJson': jsonKey.toJson,
'unknownEnumValue': jsonKey.unknownEnumValue
};

final key = jsonKeyAnnotation.entries
.map<String?>((e) {
if (e.value != null) {
switch (e.key) {
case 'name':
return '${e.key}: \'${e.value}\'';
default:
return '${e.key}: ${e.value}';
}
}

return null;
})
.whereType<String>()
.join(', ');

return 'JsonKey($key)';
}

@override
Map<String, Object?> get namedProps {
return {
'jsonKey': jsonKey,
};
}
}
5 changes: 3 additions & 2 deletions lib/generator/data/class_property.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:artemis/generator/data/annotation.dart';
import 'package:artemis/generator/data/definition.dart';
import 'package:artemis/generator/data_printer.dart';
import 'package:artemis/generator/helpers.dart';
Expand All @@ -12,7 +13,7 @@ class ClassProperty extends Definition with DataPrinter {
final TypeName type;

/// Some other custom annotation.
final List<String> annotations;
final List<Annotation> annotations;

/// Whether this parameter corresponds to the __resolveType (or equivalent)
final bool isResolveType;
Expand All @@ -33,7 +34,7 @@ class ClassProperty extends Definition with DataPrinter {
ClassProperty copyWith({
TypeName? type,
ClassPropertyName? name,
List<String>? annotations,
List<Annotation>? annotations,
bool? isResolveType,
}) =>
ClassProperty(
Expand Down
3 changes: 2 additions & 1 deletion lib/generator/data/enum_value_definition.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:artemis/generator/data/annotation.dart';
import 'package:artemis/generator/data/definition.dart';
import 'package:artemis/generator/data_printer.dart';
import 'package:recase/recase.dart';
Expand All @@ -8,7 +9,7 @@ class EnumValueDefinition extends Definition with DataPrinter {
final EnumValueName name;

/// Some other custom annotation.
final List<String> annotations;
final List<Annotation> annotations;

/// Instantiate an enum value
EnumValueDefinition({
Expand Down
5 changes: 3 additions & 2 deletions lib/generator/data/query_input.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:artemis/generator/data/annotation.dart';
import 'package:artemis/generator/data/class_property.dart';
import 'package:artemis/generator/data/definition.dart';
import 'package:artemis/generator/data_printer.dart';
Expand All @@ -12,14 +13,14 @@ class QueryInput extends Definition with DataPrinter {
final TypeName type;

/// Some other custom annotation.
final List<String> annotations;
final List<Annotation> annotations;

/// Instantiate an input parameter.
QueryInput({
required this.type,
this.annotations = const [],
required this.name,
}) : assert(hasValue(type) && hasValue(name)),
}) : assert(hasValue(type) && hasValue(name)),
super(name: name);

@override
Expand Down
7 changes: 4 additions & 3 deletions lib/generator/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:artemis/generator/data/annotation.dart';
import 'package:artemis/generator/ephemeral_data.dart';
import 'package:build/build.dart';
import 'package:collection/collection.dart' show IterableExtension;
Expand Down Expand Up @@ -136,10 +137,10 @@ bool hasValue(Object? obj) {
}

/// Proceeds deprecated annotation
List<String> proceedDeprecated(
List<Annotation> proceedDeprecated(
List<DirectiveNode>? directives,
) {
final annotations = <String>[];
final annotations = <Annotation>[];

final deprecatedDirective = directives?.firstWhereOrNull(
(directive) => directive.name.value == 'deprecated',
Expand All @@ -154,7 +155,7 @@ List<String> proceedDeprecated(
? reasonValueNode.value
: 'No longer supported';

annotations.add("Deprecated('$reason')");
annotations.add(StringAnnotation(name: "Deprecated('$reason')"));
}

return annotations;
Expand Down
Loading

0 comments on commit 7e6a420

Please sign in to comment.