diff --git a/.gitignore b/.gitignore index dfe2e347..cf68a49b 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 37abf74a..512200ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 7.1.1-beta.0 + +- exclude nullable items from input + ## 7.1.0-beta.0 **BREAKING CHANGE** diff --git a/README.md b/README.md index 851f62be..1d9d12af 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/builder.dart b/lib/builder.dart index 4c5b107a..f9b13255 100644 --- a/lib/builder.dart +++ b/lib/builder.dart @@ -184,7 +184,8 @@ class GraphQLQueryBuilder implements Builder { writeLibraryDefinitionToBuffer( buffer, - options.ignoreForFile, + options, + schemaMap, libDefinition, ); diff --git a/lib/generator.dart b/lib/generator.dart index 123702e5..2ecd945d 100644 --- a/lib/generator.dart +++ b/lib/generator.dart @@ -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'; @@ -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'; @@ -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, ); } @@ -341,7 +347,7 @@ Make sure your query is correct and your schema is updated.'''); // On custom scalars final jsonKeyAnnotation = {}; if (name.namePrintable != name.name) { - jsonKeyAnnotation['name'] = '\'${name.name}\''; + jsonKeyAnnotation['name'] = name.name; } if (nextType is ScalarTypeDefinitionNode) { @@ -380,13 +386,10 @@ Make sure your query is correct and your schema is updated.'''); final fieldDirectives = regularField?.directives ?? regularInputField?.directives; - var annotations = []; + var annotations = []; if (jsonKeyAnnotation.isNotEmpty) { - final jsonKey = jsonKeyAnnotation.entries - .map((e) => '${e.key}: ${e.value}') - .join(', '); - annotations.add('JsonKey($jsonKey)'); + annotations.add(JsonKeyAnnotation.fromMap(jsonKeyAnnotation)); } annotations.addAll(proceedDeprecated(fieldDirectives)); diff --git a/lib/generator/data/annotation.dart b/lib/generator/data/annotation.dart new file mode 100644 index 00000000..5a1ddc3d --- /dev/null +++ b/lib/generator/data/annotation.dart @@ -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 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 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 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 = { + '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((e) { + if (e.value != null) { + switch (e.key) { + case 'name': + return '${e.key}: \'${e.value}\''; + default: + return '${e.key}: ${e.value}'; + } + } + + return null; + }) + .whereType() + .join(', '); + + return 'JsonKey($key)'; + } + + @override + Map get namedProps { + return { + 'jsonKey': jsonKey, + }; + } +} diff --git a/lib/generator/data/class_property.dart b/lib/generator/data/class_property.dart index a6871023..de241633 100644 --- a/lib/generator/data/class_property.dart +++ b/lib/generator/data/class_property.dart @@ -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'; @@ -12,7 +13,7 @@ class ClassProperty extends Definition with DataPrinter { final TypeName type; /// Some other custom annotation. - final List annotations; + final List annotations; /// Whether this parameter corresponds to the __resolveType (or equivalent) final bool isResolveType; @@ -33,7 +34,7 @@ class ClassProperty extends Definition with DataPrinter { ClassProperty copyWith({ TypeName? type, ClassPropertyName? name, - List? annotations, + List? annotations, bool? isResolveType, }) => ClassProperty( diff --git a/lib/generator/data/enum_value_definition.dart b/lib/generator/data/enum_value_definition.dart index 1eae635f..a3694f24 100644 --- a/lib/generator/data/enum_value_definition.dart +++ b/lib/generator/data/enum_value_definition.dart @@ -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'; @@ -8,7 +9,7 @@ class EnumValueDefinition extends Definition with DataPrinter { final EnumValueName name; /// Some other custom annotation. - final List annotations; + final List annotations; /// Instantiate an enum value EnumValueDefinition({ diff --git a/lib/generator/data/query_input.dart b/lib/generator/data/query_input.dart index 168e8600..e1bc508c 100644 --- a/lib/generator/data/query_input.dart +++ b/lib/generator/data/query_input.dart @@ -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'; @@ -12,14 +13,14 @@ class QueryInput extends Definition with DataPrinter { final TypeName type; /// Some other custom annotation. - final List annotations; + final List 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 diff --git a/lib/generator/helpers.dart b/lib/generator/helpers.dart index fdd57606..2e470bbb 100644 --- a/lib/generator/helpers.dart +++ b/lib/generator/helpers.dart @@ -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; @@ -136,10 +137,10 @@ bool hasValue(Object? obj) { } /// Proceeds deprecated annotation -List proceedDeprecated( +List proceedDeprecated( List? directives, ) { - final annotations = []; + final annotations = []; final deprecatedDirective = directives?.firstWhereOrNull( (directive) => directive.name.value == 'deprecated', @@ -154,7 +155,7 @@ List proceedDeprecated( ? reasonValueNode.value : 'No longer supported'; - annotations.add("Deprecated('$reason')"); + annotations.add(StringAnnotation(name: "Deprecated('$reason')")); } return annotations; diff --git a/lib/generator/print_helpers.dart b/lib/generator/print_helpers.dart index e3119190..ae30a162 100644 --- a/lib/generator/print_helpers.dart +++ b/lib/generator/print_helpers.dart @@ -1,5 +1,7 @@ +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/schema/options.dart'; import 'package:code_builder/code_builder.dart'; import 'package:collection/collection.dart' show IterableExtension; import 'package:dart_style/dart_style.dart'; @@ -8,6 +10,10 @@ import 'package:recase/recase.dart'; import '../generator/helpers.dart'; +final _overrideCodeExpresion = CodeExpression(Code( + OverrideAnnotation().toAnnotation(), +)); + /// Generates a [Spec] of a single enum definition. Spec enumDefinitionToSpec(EnumDefinition definition) => CodeExpression(Code('''enum ${definition.name.namePrintable} { @@ -16,7 +22,7 @@ Spec enumDefinitionToSpec(EnumDefinition definition) => String _enumValueToSpec(EnumValueDefinition value) { final annotations = value.annotations - .map((annotation) => '@$annotation') + .map((annotation) => '@${annotation.toAnnotation()}') .followedBy(['@JsonValue(\'${value.name.name}\')']).join(' '); return '$annotations${value.name.namePrintable}, '; @@ -38,7 +44,7 @@ String _fromJsonBody(ClassDefinition definition) { return buffer.toString(); } -String _toJsonBody(ClassDefinition definition) { +String _toJsonBody(ClassDefinition definition, bool excludeNullable) { final buffer = StringBuffer(); final typeName = definition.typeNameField.namePrintable; buffer.writeln('''switch ($typeName) {'''); @@ -49,8 +55,15 @@ String _toJsonBody(ClassDefinition definition) { } buffer.writeln(''' default: - } - return _\$${definition.name.namePrintable}ToJson(this);'''); + }'''); + + if (definition.isInput && excludeNullable) { + buffer.writeln( + 'return _excludeNullable(_\$${definition.name.namePrintable}ToJson(this));'); + } else { + buffer.writeln('return _\$${definition.name.namePrintable}ToJson(this);'); + } + return buffer.toString(); } @@ -58,17 +71,79 @@ Method _propsMethod(String body) { return Method((m) => m ..type = MethodType.getter ..returns = refer('List') - ..annotations.add(CodeExpression(Code('override'))) + ..annotations.add(_overrideCodeExpresion) ..name = 'props' ..lambda = true ..body = Code(body)); } +List _jsonKeyAnnotationNullable( + List existingAnnotations) { + final jsonKeyAnnotation = + existingAnnotations.indexWhere((e) => e is JsonKeyAnnotation); + + if (jsonKeyAnnotation != -1) { + final annotation = + existingAnnotations[jsonKeyAnnotation] as JsonKeyAnnotation; + final updatedJsonKeyAnnotation = JsonKeyAnnotation( + jsonKey: annotation.jsonKey.copyWith( + fromJson: '_nullableFromJson', + toJson: '_nullableToJson', + ), + ); + + return List.from(existingAnnotations) + ..replaceRange( + jsonKeyAnnotation, + jsonKeyAnnotation, + [updatedJsonKeyAnnotation], + ); + } + + return List.from(existingAnnotations) + ..add( + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: '_nullableFromJson', + toJson: '_nullableToJson', + ), + ), + ); +} + +Method _excludeNullable(Iterable inputs) { + return Method( + (m) => m + ..name = '_excludeNullable' + ..lambda = false + ..returns = refer('Map') + ..requiredParameters.add(Parameter( + (p) => p + ..name = 'json' + ..type = refer('Map'), + )) + ..annotations.add(_overrideCodeExpresion) + ..body = Code(''' + ${inputs.map((p) { + if (!p.type.isNonNull) { + return 'if(${p.name.namePrintable} == null) json.remove(\'${p.name.namePrintable}\');'; + } + return ''; + }).join('')} + + return json; + '''), + ); +} + /// Generates a [Spec] of a single class definition. Spec classDefinitionToSpec( - ClassDefinition definition, - Iterable fragments, - Iterable classes) { + ClassDefinition definition, + Iterable fragments, + Iterable classes, + GeneratorOptions options, + SchemaMap schemaMap, +) { final fromJson = definition.factoryPossibilities.isNotEmpty ? Constructor( (b) => b @@ -98,17 +173,19 @@ Spec classDefinitionToSpec( ? Method( (m) => m ..name = 'toJson' - ..annotations.add(CodeExpression(Code('override'))) + ..annotations.add(_overrideCodeExpresion) ..returns = refer('Map') - ..body = Code(_toJsonBody(definition)), + ..body = Code(_toJsonBody(definition, schemaMap.toJsonExclude)), ) : Method( (m) => m ..name = 'toJson' ..lambda = true - ..annotations.add(CodeExpression(Code('override'))) + ..annotations.add(_overrideCodeExpresion) ..returns = refer('Map') - ..body = Code('_\$${definition.name.namePrintable}ToJson(this)'), + ..body = Code(schemaMap.toJsonExclude && definition.isInput + ? '_excludeNullable(_\$${definition.name.namePrintable}ToJson(this))' + : '_\$${definition.name.namePrintable}ToJson(this)'), ); final props = definition.mixins @@ -127,63 +204,91 @@ Spec classDefinitionToSpec( classes.firstWhereOrNull((e) => e.name == definition.extension); return Class( - (b) => b - ..annotations - .add(CodeExpression(Code('JsonSerializable(explicitToJson: true)'))) - ..name = definition.name.namePrintable - ..mixins.add(refer('EquatableMixin')) - ..mixins.addAll(definition.mixins.map((i) => refer(i.namePrintable))) - ..methods.add(_propsMethod('[${props.join(',')}]')) - ..extend = definition.extension != null - ? refer(definition.extension!.namePrintable) - : refer('JsonSerializable') - ..implements.addAll(definition.implementations.map((i) => refer(i))) - ..constructors.add(Constructor((b) { - if (definition.isInput) { - b.optionalParameters.addAll(definition.properties - .where( - (property) => !property.isOverride && !property.isResolveType) - .map( - (property) => Parameter( - (p) { - p - ..name = property.name.namePrintable - ..named = true - ..toThis = true - ..required = property.type.isNonNull; - }, - ), - )); - } - })) - ..constructors.add(fromJson) - ..methods.add(toJson) - ..fields.addAll(definition.properties.map((p) { - if (extendedClass != null && - extendedClass.properties.any((e) => e == p)) { - // if class has the same prop as in extension - p.annotations.add('override'); - } + (b) { + b + ..annotations + .add(CodeExpression(Code('JsonSerializable(explicitToJson: true)'))) + ..name = definition.name.namePrintable + ..mixins.add(refer('EquatableMixin')) + ..mixins.addAll(definition.mixins.map((i) => refer(i.namePrintable))) + ..methods.add(_propsMethod('[${props.join(',')}]')) + ..extend = definition.extension != null + ? refer(definition.extension!.namePrintable) + : refer('JsonSerializable') + ..implements.addAll(definition.implementations.map((i) => refer(i))) + ..constructors.add(Constructor((b) { + if (definition.isInput) { + b.optionalParameters.addAll(definition.properties + .where((property) => + !property.isOverride && !property.isResolveType) + .map( + (property) => Parameter( + (p) { + p + ..name = property.name.namePrintable + ..named = true + ..toThis = true + ..required = property.type.isNonNull; + }, + ), + )); + } + })) + ..constructors.add(fromJson) + ..methods.add(toJson) + ..fields.addAll(definition.properties.map((p) { + if (extendedClass != null && + extendedClass.properties.any((e) => e == p)) { + // if class has the same prop as in extension + p.annotations.add(OverrideAnnotation()); + } + + final lateModifier = + p.type.isNonNull && !definition.isInput ? 'late ' : ''; - final field = Field((f) { - f - ..name = p.name.namePrintable - // TODO: remove this workaround when code_builder includes late field modifier: - // https://github.com/dart-lang/code_builder/pull/310 - ..type = refer( - '${p.type.isNonNull ? 'late ' : ''} ${p.type.namePrintable}') - ..annotations.addAll( - p.annotations.map((e) => CodeExpression(Code(e))), - ); - - if (p.type.isNonNull) { - // TODO: apply this fix when code_builder includes late field modifier: - // https://github.com/dart-lang/code_builder/pull/310 - // f.modifier = FieldModifier.late$; + var annotations = p.annotations; + + if (schemaMap.toJsonExclude && + definition.isInput && + !p.type.isNonNull) { + annotations = _jsonKeyAnnotationNullable(p.annotations); } - }); - return field; - })), + + final field = Field((f) { + f + ..name = p.name.namePrintable + ..type = refer(lateModifier + + (schemaMap.toJsonExclude && + definition.isInput && + !p.type.isNonNull + ? 'Nullable<${p.type.namePrintable}>?' + : p.type.namePrintable)) + ..annotations.addAll( + annotations.map((e) => CodeExpression(Code(e.toAnnotation()))), + ); + if (definition.isInput) { + f.modifier = FieldModifier.final$; + } + + if (p.type.isNonNull) { + // TODO: apply this fix when code_builder includes late field modifier: + // https://github.com/dart-lang/code_builder/pull/310 + // f.modifier = FieldModifier.late$; + } + }); + return field; + })); + + if (schemaMap.toJsonExclude && definition.isInput) { + b.methods.add(_excludeNullable(definition.properties + .where( + (property) => !property.isOverride && !property.isResolveType) + .map( + (p) => QueryInput( + name: QueryInputName(name: p.name.name), type: p.type), + ))); + } + }, ); } @@ -191,7 +296,7 @@ Spec classDefinitionToSpec( Spec fragmentClassDefinitionToSpec(FragmentClassDefinition definition) { final fields = definition.properties.map((p) { final lines = []; - lines.addAll(p.annotations.map((e) => '@$e')); + lines.addAll(p.annotations.map((e) => '@${e.toAnnotation()}')); lines.add( '${p.type.isNonNull ? 'late ' : ''}${p.type.namePrintable} ${p.name.namePrintable};'); return lines.join('\n'); @@ -203,69 +308,85 @@ Spec fragmentClassDefinitionToSpec(FragmentClassDefinition definition) { } /// Generates a [Spec] of a mutation argument class. -Spec generateArgumentClassSpec(QueryDefinition definition) { +Spec generateArgumentClassSpec( + QueryDefinition definition, + GeneratorOptions options, + SchemaMap schemaMap, +) { return Class( - (b) => b - ..annotations - .add(CodeExpression(Code('JsonSerializable(explicitToJson: true)'))) - ..name = '${definition.className}Arguments' - ..extend = refer('JsonSerializable') - ..mixins.add(refer('EquatableMixin')) - ..methods.add(_propsMethod( - '[${definition.inputs.map((input) => input.name.namePrintable).join(',')}]')) - ..constructors.add(Constructor( - (b) => b - ..optionalParameters.addAll(definition.inputs.map( - (input) => Parameter( - (p) { - p - ..name = input.name.namePrintable - ..named = true - ..toThis = true - ..required = input.type.isNonNull; - }, - ), - )), - )) - ..constructors.add(Constructor( - (b) => b - ..factory = true - ..name = 'fromJson' - ..lambda = true - ..requiredParameters.add(Parameter( - (p) => p - ..type = refer('Map') - ..name = 'json', - )) - ..annotations.add(CodeExpression(Code('override'))) - ..body = Code('_\$${definition.className}ArgumentsFromJson(json)'), - )) - ..methods.add(Method( - (m) => m - ..name = 'toJson' - ..lambda = true - ..returns = refer('Map') - ..annotations.add(CodeExpression(Code('override'))) - ..body = Code('_\$${definition.className}ArgumentsToJson(this)'), - )) - ..fields.addAll(definition.inputs.map( - (p) => Field( - (f) { - f - ..name = p.name.namePrintable - // TODO: remove this workaround when code_builder includes late field modifier: - // https://github.com/dart-lang/code_builder/pull/310 - ..type = refer( - '${p.type.isNonNull ? 'late ' : ''} ${p.type.namePrintable}') - ..annotations - .addAll(p.annotations.map((e) => CodeExpression(Code(e)))); - - if (!p.type.isNonNull) { - f.modifier = FieldModifier.final$; - } - }, - ), - )), + (b) { + b + ..annotations + .add(CodeExpression(Code('JsonSerializable(explicitToJson: true)'))) + ..name = '${definition.className}Arguments' + ..extend = refer('JsonSerializable') + ..mixins.add(refer('EquatableMixin')) + ..methods.add(_propsMethod( + '[${definition.inputs.map((input) => input.name.namePrintable).join(',')}]')) + ..constructors.add(Constructor( + (b) => b + ..optionalParameters.addAll(definition.inputs.map( + (input) => Parameter( + (p) { + p + ..name = input.name.namePrintable + ..named = true + ..toThis = true + ..required = input.type.isNonNull; + }, + ), + )), + )) + ..constructors.add(Constructor( + (b) => b + ..factory = true + ..name = 'fromJson' + ..lambda = true + ..requiredParameters.add(Parameter( + (p) => p + ..type = refer('Map') + ..name = 'json', + )) + ..annotations.add(_overrideCodeExpresion) + ..body = Code('_\$${definition.className}ArgumentsFromJson(json)'), + )) + ..methods.add(Method( + (m) => m + ..name = 'toJson' + ..lambda = true + ..returns = refer('Map') + ..annotations.add(_overrideCodeExpresion) + ..body = schemaMap.toJsonExclude + ? Code( + '_excludeNullable(_\$${definition.className}ArgumentsToJson(this))') + : Code('_\$${definition.className}ArgumentsToJson(this)'), + )) + ..fields.addAll(definition.inputs.map( + (p) => Field( + (f) { + var annotations = p.annotations; + + if (schemaMap.toJsonExclude && !p.type.isNonNull) { + annotations = _jsonKeyAnnotationNullable(p.annotations); + } + + f + ..name = p.name.namePrintable + ..type = refer(schemaMap.toJsonExclude && !p.type.isNonNull + ? 'Nullable<${p.type.namePrintable}>?' + : p.type.namePrintable) + ..annotations.addAll(annotations.map((e) => CodeExpression( + Code(e.toAnnotation()), + ))) + ..modifier = FieldModifier.final$; + }, + ), + )); + + if (schemaMap.toJsonExclude) { + b.methods.add(_excludeNullable(definition.inputs)); + } + }, ); } @@ -292,7 +413,7 @@ List generateQueryClassSpec(QueryDefinition definition) { final fields = [ Field( (f) => f - ..annotations.add(CodeExpression(Code('override'))) + ..annotations.add(_overrideCodeExpresion) ..modifier = FieldModifier.final$ ..type = refer('DocumentNode', 'package:gql/ast.dart') ..name = 'document' @@ -300,7 +421,7 @@ List generateQueryClassSpec(QueryDefinition definition) { ), Field( (f) => f - ..annotations.add(CodeExpression(Code('override'))) + ..annotations.add(_overrideCodeExpresion) ..modifier = FieldModifier.final$ ..type = refer('String') ..name = 'operationName' @@ -311,7 +432,7 @@ List generateQueryClassSpec(QueryDefinition definition) { if (definition.inputs.isNotEmpty) { fields.add(Field( (f) => f - ..annotations.add(CodeExpression(Code('override'))) + ..annotations.add(_overrideCodeExpresion) ..modifier = FieldModifier.final$ ..type = refer('${definition.className}Arguments') ..name = 'variables', @@ -335,7 +456,7 @@ List generateQueryClassSpec(QueryDefinition definition) { '[document, operationName${definition.inputs.isNotEmpty ? ', variables' : ''}]')) ..methods.add(Method( (m) => m - ..annotations.add(CodeExpression(Code('override'))) + ..annotations.add(_overrideCodeExpresion) ..returns = refer(definition.name.namePrintable) ..name = 'parse' ..requiredParameters.add(Parameter( @@ -352,7 +473,11 @@ List generateQueryClassSpec(QueryDefinition definition) { /// Gathers and generates a [Spec] of a whole query/mutation and its /// dependencies into a single library file. -Spec generateLibrarySpec(LibraryDefinition definition) { +Spec generateLibrarySpec( + LibraryDefinition definition, + GeneratorOptions options, + SchemaMap schemaMap, +) { final importDirectives = [ Directive.import('package:json_annotation/json_annotation.dart'), Directive.import('package:equatable/equatable.dart'), @@ -375,6 +500,35 @@ Spec generateLibrarySpec(LibraryDefinition definition) { CodeExpression(Code('part \'${definition.basename}.g.dart\';')), ]; + if (schemaMap.toJsonExclude) { + bodyDirectives.add(Code(r''' + class Nullable { + final T _value; + + Nullable(this._value); + + T get value => _value; + } + + dynamic _nullableToJson(T? value) { + if (value is Nullable?) { + final objectValue = value?.value; + + if (objectValue is JsonSerializable) { + return objectValue.toJson(); + } + + return value?.value; + } + return null; + } + + T? _nullableFromJson(Object? value) { + throw Exception('From JSON is not supported.'); + } + ''')); + } + final uniqueDefinitions = definition.queries .map((e) => e.classes.map((e) => e)) .expand((e) => e) @@ -389,13 +543,22 @@ Spec generateLibrarySpec(LibraryDefinition definition) { final enums = uniqueDefinitions.whereType(); bodyDirectives.addAll(fragments.map(fragmentClassDefinitionToSpec)); - bodyDirectives.addAll( - classes.map((cDef) => classDefinitionToSpec(cDef, fragments, classes))); + bodyDirectives.addAll(classes.map((cDef) => classDefinitionToSpec( + cDef, + fragments, + classes, + options, + schemaMap, + ))); bodyDirectives.addAll(enums.map(enumDefinitionToSpec)); for (final queryDef in definition.queries) { if (queryDef.inputs.isNotEmpty && queryDef.generateHelpers) { - bodyDirectives.add(generateArgumentClassSpec(queryDef)); + bodyDirectives.add(generateArgumentClassSpec( + queryDef, + options, + schemaMap, + )); } if (queryDef.generateHelpers) { bodyDirectives.addAll(generateQueryClassSpec(queryDef)); @@ -417,18 +580,23 @@ String specToString(Spec spec) { /// a [QueryDefinition] into a buffer. void writeLibraryDefinitionToBuffer( StringBuffer buffer, - List ignoreForFile, + GeneratorOptions options, + SchemaMap schemaMap, LibraryDefinition definition, ) { buffer.writeln('// GENERATED CODE - DO NOT MODIFY BY HAND'); buffer.writeln('// @dart = 2.12'); - if (ignoreForFile.isNotEmpty) { + if (options.ignoreForFile.isNotEmpty) { buffer.writeln( - '// ignore_for_file: ${Set.from(ignoreForFile).join(', ')}', + '// ignore_for_file: ${Set.from(options.ignoreForFile).join(', ')}', ); } buffer.write('\n'); - buffer.write(specToString(generateLibrarySpec(definition))); + buffer.write(specToString(generateLibrarySpec( + definition, + options, + schemaMap, + ))); } /// Generate an empty file just exporting the library. This is used to avoid @@ -437,3 +605,8 @@ String writeLibraryForwarder(LibraryDefinition definition) => '''// GENERATED CODE - DO NOT MODIFY BY HAND export '${definition.basename}.dart'; '''; + +String writeNullableFile(LibraryDefinition definition) => + '''// GENERATED CODE - DO NOT MODIFY BY HAND +export '${definition.basename}.dart'; +'''; diff --git a/lib/schema/options.dart b/lib/schema/options.dart index ea8028f6..c7c811fc 100644 --- a/lib/schema/options.dart +++ b/lib/schema/options.dart @@ -143,6 +143,10 @@ class SchemaMap { @JsonKey(defaultValue: false) final bool appendTypeName; + /// The resolve type field used on this schema. + @JsonKey(defaultValue: false) + final bool toJsonExclude; + /// The naming scheme to be used. /// /// - [NamingScheme.pathedWithTypes]: default, where the names of @@ -162,6 +166,7 @@ class SchemaMap { this.queriesGlob, this.typeNameField = '__typename', this.appendTypeName = false, + this.toJsonExclude = false, this.namingScheme = NamingScheme.pathedWithTypes, }); diff --git a/lib/schema/options.g2.dart b/lib/schema/options.g2.dart index dbed97b7..4dbf3108 100644 --- a/lib/schema/options.g2.dart +++ b/lib/schema/options.g2.dart @@ -73,6 +73,7 @@ SchemaMap _$SchemaMapFromJson(Map json) { queriesGlob: json['queries_glob'] as String?, typeNameField: json['type_name_field'] as String? ?? '__typename', appendTypeName: json['append_type_name'] as bool? ?? false, + toJsonExclude: json['to_json_exclude'] as bool? ?? false, namingScheme: _$enumDecodeNullable( _$NamingSchemeEnumMap, json['naming_scheme'], unknownValue: NamingScheme.pathedWithTypes), @@ -85,6 +86,7 @@ Map _$SchemaMapToJson(SchemaMap instance) => { 'queries_glob': instance.queriesGlob, 'type_name_field': instance.typeNameField, 'append_type_name': instance.appendTypeName, + 'to_json_exclude': instance.toJsonExclude, 'naming_scheme': _$NamingSchemeEnumMap[instance.namingScheme], }; diff --git a/lib/visitor/generator_visitor.dart b/lib/visitor/generator_visitor.dart index 1b1bafae..89aff9bc 100644 --- a/lib/visitor/generator_visitor.dart +++ b/lib/visitor/generator_visitor.dart @@ -1,4 +1,5 @@ import 'package:artemis/generator.dart'; +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:artemis/generator/data/nullable.dart'; import 'package:artemis/generator/ephemeral_data.dart'; @@ -200,16 +201,13 @@ class GeneratorVisitor extends RecursiveVisitor { final inputName = QueryInputName(name: node.variable.name.value); if (inputName.namePrintable != inputName.name) { - jsonKeyAnnotation['name'] = '\'${inputName.name}\''; + jsonKeyAnnotation['name'] = inputName.name; } - var annotations = []; + var annotations = []; if (jsonKeyAnnotation.isNotEmpty) { - final jsonKey = jsonKeyAnnotation.entries - .map((e) => '${e.key}: ${e.value}') - .join(', '); - annotations.add('JsonKey($jsonKey)'); + annotations.add(JsonKeyAnnotation.fromMap(jsonKeyAnnotation)); } context.inputsClasses.add(QueryInput( diff --git a/pubspec.yaml b/pubspec.yaml index 67cbdad5..ce80d7f4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: artemis -version: 7.1.0-beta.0 +version: 7.1.1-beta.0 description: Build dart types from GraphQL schemas and queries (using Introspection Query). homepage: https://github.com/comigor/artemis diff --git a/test/generator/print_helpers_test.dart b/test/generator/print_helpers_test.dart index 5eb2ffcf..73864e70 100644 --- a/test/generator/print_helpers_test.dart +++ b/test/generator/print_helpers_test.dart @@ -1,6 +1,8 @@ +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/print_helpers.dart'; +import 'package:artemis/schema/options.dart'; import 'package:gql/language.dart'; import 'package:test/test.dart'; @@ -101,11 +103,11 @@ void main() { ClassProperty( type: TypeName(name: 'Type'), name: ClassPropertyName(name: 'name'), - annotations: ['override']), + annotations: [OverrideAnnotation()]), ClassProperty( type: TypeName(name: 'Type'), name: ClassPropertyName(name: 'name'), - annotations: ['Test']), + annotations: [StringAnnotation(name: 'Test')]), ]); final str = specToString(fragmentClassDefinitionToSpec(definition)); @@ -129,9 +131,12 @@ void main() { // throwsA(TypeMatcher())); expect( () => classDefinitionToSpec( - ClassDefinition(name: ClassName(name: ''), properties: []), - [], - []), + ClassDefinition(name: ClassName(name: ''), properties: []), + [], + [], + GeneratorOptions(), + SchemaMap(), + ), throwsA(TypeMatcher())); }); @@ -139,7 +144,13 @@ void main() { final definition = ClassDefinition(name: ClassName(name: 'AClass'), properties: []); - final str = specToString(classDefinitionToSpec(definition, [], [])); + final str = specToString(classDefinitionToSpec( + definition, + [], + [], + GeneratorOptions(), + SchemaMap(), + )); expect(str, '''@JsonSerializable(explicitToJson: true) class AClass extends JsonSerializable with EquatableMixin { @@ -161,7 +172,13 @@ class AClass extends JsonSerializable with EquatableMixin { properties: [], extension: ClassName(name: 'AnotherClass')); - final str = specToString(classDefinitionToSpec(definition, [], [])); + final str = specToString(classDefinitionToSpec( + definition, + [], + [], + GeneratorOptions(), + SchemaMap(), + )); expect(str, '''@JsonSerializable(explicitToJson: true) class AClass extends AnotherClass with EquatableMixin { @@ -190,7 +207,13 @@ class AClass extends AnotherClass with EquatableMixin { typeNameField: ClassPropertyName(name: '__typename'), ); - final str = specToString(classDefinitionToSpec(definition, [], [])); + final str = specToString(classDefinitionToSpec( + definition, + [], + [], + GeneratorOptions(), + SchemaMap(), + )); expect(str, r'''@JsonSerializable(explicitToJson: true) class AClass extends JsonSerializable with EquatableMixin { @@ -235,7 +258,13 @@ class AClass extends JsonSerializable with EquatableMixin { name: ClassPropertyName(name: 'anotherName')), ]); - final str = specToString(classDefinitionToSpec(definition, [], [])); + final str = specToString(classDefinitionToSpec( + definition, + [], + [], + GeneratorOptions(), + SchemaMap(), + )); expect(str, '''@JsonSerializable(explicitToJson: true) class AClass extends JsonSerializable with EquatableMixin { @@ -266,18 +295,27 @@ class AClass extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: 'AnnotatedProperty'), name: ClassPropertyName(name: 'name'), - annotations: ['Hey()']), + annotations: [StringAnnotation(name: 'Hey()')]), ClassProperty( type: TypeName(name: 'OverridenProperty'), name: ClassPropertyName(name: 'name'), - annotations: ['override']), + annotations: [OverrideAnnotation()]), ClassProperty( type: TypeName(name: 'AllAtOnce'), name: ClassPropertyName(name: 'name'), - annotations: ['override', 'Ho()']), + annotations: [ + OverrideAnnotation(), + StringAnnotation(name: 'Ho()') + ]), ]); - final str = specToString(classDefinitionToSpec(definition, [], [])); + final str = specToString(classDefinitionToSpec( + definition, + [], + [], + GeneratorOptions(), + SchemaMap(), + )); expect(str, '''@JsonSerializable(explicitToJson: true) class AClass extends JsonSerializable with EquatableMixin { @@ -313,15 +351,21 @@ class AClass extends JsonSerializable with EquatableMixin { properties: [], mixins: [FragmentName(name: 'FragmentMixin')]); - final str = specToString(classDefinitionToSpec(definition, [ - FragmentClassDefinition( - name: FragmentName(name: 'FragmentMixin'), - properties: [ - ClassProperty( - type: TypeName(name: 'Type'), - name: ClassPropertyName(name: 'name')), - ]) - ], [])); + final str = specToString(classDefinitionToSpec( + definition, + [ + FragmentClassDefinition( + name: FragmentName(name: 'FragmentMixin'), + properties: [ + ClassProperty( + type: TypeName(name: 'Type'), + name: ClassPropertyName(name: 'name')), + ]) + ], + [], + GeneratorOptions(), + SchemaMap(), + )); expect(str, '''@JsonSerializable(explicitToJson: true) class AClass extends JsonSerializable with EquatableMixin, FragmentMixin { @@ -352,22 +396,28 @@ class AClass extends JsonSerializable with EquatableMixin, FragmentMixin { isInput: true, ); - final str = specToString(classDefinitionToSpec(definition, [], [])); + final str = specToString(classDefinitionToSpec( + definition, + [], + [], + GeneratorOptions(), + SchemaMap(), + )); - expect(str, '''@JsonSerializable(explicitToJson: true) + expect(str, r'''@JsonSerializable(explicitToJson: true) class AClass extends JsonSerializable with EquatableMixin { AClass({this.name, required this.anotherName}); - factory AClass.fromJson(Map json) => _\$AClassFromJson(json); + factory AClass.fromJson(Map json) => _$AClassFromJson(json); - Type? name; + final Type? name; - late AnotherType anotherName; + final AnotherType anotherName; @override List get props => [name, anotherName]; @override - Map toJson() => _\$AClassToJson(this); + Map toJson() => _$AClassToJson(this); } '''); }); @@ -375,7 +425,12 @@ class AClass extends JsonSerializable with EquatableMixin { group('On generateQueryClassSpec', () { test('It will throw if basename is null or empty.', () { - expect(() => generateLibrarySpec(LibraryDefinition(basename: '')), + expect( + () => generateLibrarySpec( + LibraryDefinition(basename: ''), + GeneratorOptions(), + SchemaMap(), + ), throwsA(TypeMatcher())); }); @@ -416,8 +471,12 @@ class AClass extends JsonSerializable with EquatableMixin { test('It should generated an empty file by default.', () { final buffer = StringBuffer(); final definition = LibraryDefinition(basename: r'test_query.graphql'); - final ignoreForFile = []; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: []), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 @@ -433,9 +492,13 @@ part 'test_query.graphql.g.dart'; final buffer = StringBuffer(); final definition = LibraryDefinition( basename: r'test_query.graphql', customImports: ['some_file.dart']); - final ignoreForFile = []; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: []), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 @@ -461,9 +524,13 @@ part 'test_query.graphql.g.dart'; ) ], ); - final ignoreForFile = []; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: []), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 @@ -516,9 +583,13 @@ class TestQueryQuery extends GraphQLQuery { ], ), ]); - final ignoreForFile = []; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: []), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 @@ -586,7 +657,11 @@ class TestQueryQuery extends GraphQLQuery { ], ); - final str = specToString(generateArgumentClassSpec(definition)); + final str = specToString(generateArgumentClassSpec( + definition, + GeneratorOptions(), + SchemaMap(), + )); expect(str, '''@JsonSerializable(explicitToJson: true) class TestQueryArguments extends JsonSerializable with EquatableMixin { @@ -669,9 +744,13 @@ class TestQueryQuery extends GraphQLQuery { ], ), ]); - final ignoreForFile = []; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: []), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 @@ -704,9 +783,13 @@ enum SomeEnum { test('Should not add ignore_for_file when ignoreForFile is null', () { final buffer = StringBuffer(); final definition = LibraryDefinition(basename: r'test_query.graphql'); - final ignoreForFile = []; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: []), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 @@ -721,9 +804,13 @@ part 'test_query.graphql.g.dart'; test('Should not add ignore_for_file when ignoreForFile is empty', () { final buffer = StringBuffer(); final definition = LibraryDefinition(basename: r'test_query.graphql'); - final ignoreForFile = []; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: []), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 @@ -739,9 +826,13 @@ part 'test_query.graphql.g.dart'; () { final buffer = StringBuffer(); final definition = LibraryDefinition(basename: r'test_query.graphql'); - final ignoreForFile = ['my_rule_1', 'my_rule_2']; - writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition); + writeLibraryDefinitionToBuffer( + buffer, + GeneratorOptions(ignoreForFile: ['my_rule_1', 'my_rule_2']), + SchemaMap(), + definition, + ); expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND // @dart = 2.12 diff --git a/test/helpers.dart b/test/helpers.dart index e2265e7a..421ce255 100644 --- a/test/helpers.dart +++ b/test/helpers.dart @@ -16,6 +16,7 @@ Future testGenerator({ required String schema, String namingScheme = 'pathedWithTypes', bool appendTypeName = false, + bool toJsonExclude = false, bool generateHelpers = false, Map builderOptionsMap = const {}, Map sourceAssetsMap = const {}, @@ -32,6 +33,7 @@ Future testGenerator({ 'output': 'lib/query.graphql.dart', 'naming_scheme': namingScheme, 'append_type_name': appendTypeName, + 'to_json_exclude': toJsonExclude, } ], ...builderOptionsMap, diff --git a/test/query_generator/aliases/alias_on_leaves_test.dart b/test/query_generator/aliases/alias_on_leaves_test.dart index cd8a97be..10d20402 100644 --- a/test/query_generator/aliases/alias_on_leaves_test.dart +++ b/test/query_generator/aliases/alias_on_leaves_test.dart @@ -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:test/test.dart'; @@ -53,15 +54,9 @@ final LibraryDefinition libraryDefinition = operationName: r'some_query', classes: [ EnumDefinition(name: EnumName(name: r'MyEnum'), values: [ - EnumValueDefinition( - name: EnumValueName(name: r'A'), - ), - EnumValueDefinition( - name: EnumValueName(name: r'B'), - ), - EnumValueDefinition( - name: EnumValueName(name: r'ARTEMIS_UNKNOWN'), - ), + EnumValueDefinition(name: EnumValueName(name: r'A')), + EnumValueDefinition(name: EnumValueName(name: r'B')), + EnumValueDefinition(name: EnumValueName(name: r'ARTEMIS_UNKNOWN')) ]), ClassDefinition( name: ClassName(name: r'SomeQuery$_Response$_SomeObject'), @@ -70,7 +65,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'thisIsAnEnum'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], diff --git a/test/query_generator/append_type_name_test.dart b/test/query_generator/append_type_name_test.dart index b275b9cc..534447d3 100644 --- a/test/query_generator/append_type_name_test.dart +++ b/test/query_generator/append_type_name_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -46,7 +47,10 @@ void main() { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {}, @@ -62,7 +66,10 @@ void main() { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {}, @@ -161,7 +168,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {}, @@ -177,7 +187,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {}, @@ -274,7 +287,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], mixins: [FragmentName(name: r'QueryResponseMixin')], @@ -291,7 +307,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {}, @@ -307,7 +326,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ]) ], @@ -417,7 +439,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], extension: ClassName(name: r'Custom$_QueryRoot$_q'), @@ -434,7 +459,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], extension: ClassName(name: r'Custom$_QueryRoot$_q'), @@ -447,7 +475,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: { @@ -468,7 +499,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {}, @@ -613,7 +647,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], mixins: [FragmentName(name: r'QueryResponseMixin')], @@ -631,7 +668,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {}, @@ -647,7 +687,10 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin { ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ]) ], diff --git a/test/query_generator/ast_schema/multiple_schema_mappint_test.dart b/test/query_generator/ast_schema/multiple_schema_mappint_test.dart index fcca6ac2..3cdb54f9 100644 --- a/test/query_generator/ast_schema/multiple_schema_mappint_test.dart +++ b/test/query_generator/ast_schema/multiple_schema_mappint_test.dart @@ -1,4 +1,5 @@ import 'package:artemis/builder.dart'; +import 'package:artemis/generator/data/annotation.dart'; import 'package:build/build.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:artemis/generator/data/enum_value_definition.dart'; @@ -168,7 +169,9 @@ final LibraryDefinition libraryDefinitionA = type: TypeName(name: r'ArticleType', isNonNull: true), name: ClassPropertyName(name: r'articleType'), annotations: [ - r'JsonKey(unknownEnumValue: ArticleType.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'ArticleType.artemisUnknown')) ], isResolveType: false) ], @@ -233,14 +236,18 @@ final libraryDefinitionB = type: TypeName(name: r'Privacy', isNonNull: true), name: ClassPropertyName(name: r'privacy'), annotations: [ - r'JsonKey(unknownEnumValue: Privacy.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'Privacy.artemisUnknown')) ], isResolveType: false), ClassProperty( type: TypeName(name: r'Status', isNonNull: true), name: ClassPropertyName(name: r'status'), annotations: [ - r'JsonKey(unknownEnumValue: Status.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'Status.artemisUnknown')) ], isResolveType: false) ], @@ -269,7 +276,10 @@ final libraryDefinitionB = type: TypeName(name: r'NotificationType'), name: ClassPropertyName(name: r'type'), annotations: [ - r'JsonKey(unknownEnumValue: NotificationType.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: + r'NotificationType.artemisUnknown')) ], isResolveType: false), ClassProperty( @@ -456,9 +466,9 @@ class NotificationOptionInput extends JsonSerializable with EquatableMixin { _$NotificationOptionInputFromJson(json); @JsonKey(unknownEnumValue: NotificationType.artemisUnknown) - NotificationType? type; + final NotificationType? type; - bool? enabled; + final bool? enabled; @override List get props => [type, enabled]; diff --git a/test/query_generator/deprecated/deprecated_enum_value_test.dart b/test/query_generator/deprecated/deprecated_enum_value_test.dart index 6c197110..0af859e5 100644 --- a/test/query_generator/deprecated/deprecated_enum_value_test.dart +++ b/test/query_generator/deprecated/deprecated_enum_value_test.dart @@ -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:gql/language.dart'; @@ -42,41 +43,31 @@ const query = r''' final LibraryDefinition libraryDefinition = LibraryDefinition(basename: r'query.graphql', queries: [ QueryDefinition( - document: parseString(query), name: QueryName(name: r'SomeQuery$_QueryResponse'), - operationName: 'some_query', + operationName: r'some_query', classes: [ - EnumDefinition( - name: EnumName(name: r'StarWarsMovies'), - values: [ - EnumValueDefinition( - name: EnumValueName(name: 'NEW_HOPE'), + EnumDefinition(name: EnumName(name: r'StarWarsMovies'), values: [ + EnumValueDefinition( + name: EnumValueName(name: r'NEW_HOPE'), annotations: [ - r"Deprecated('deprecated movie')", - ], - ), - EnumValueDefinition( - name: EnumValueName(name: 'EMPIRE'), - ), - EnumValueDefinition( - name: EnumValueName(name: 'JEDI'), - ), - EnumValueDefinition( - name: EnumValueName(name: 'ARTEMIS_UNKNOWN'), - ), - ], - ), + StringAnnotation(name: r'''Deprecated('deprecated movie')''') + ]), + EnumValueDefinition(name: EnumValueName(name: r'EMPIRE')), + EnumValueDefinition(name: EnumValueName(name: r'JEDI')), + EnumValueDefinition(name: EnumValueName(name: r'ARTEMIS_UNKNOWN')) + ]), ClassDefinition( name: ClassName(name: r'SomeQuery$_QueryResponse'), properties: [ ClassProperty( type: TypeName(name: r'StarWarsMovies'), name: ClassPropertyName(name: r'someValue'), - // isOverride: false, - annotations: [ - r'JsonKey(unknownEnumValue: StarWarsMovies.artemisUnknown)', - ]) + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'StarWarsMovies.artemisUnknown')) + ], + isResolveType: false) ], factoryPossibilities: {}, typeNameField: ClassPropertyName(name: r'__typename'), diff --git a/test/query_generator/deprecated/deprecated_field_test.dart b/test/query_generator/deprecated/deprecated_field_test.dart index d72ac136..bbb8ba44 100644 --- a/test/query_generator/deprecated/deprecated_field_test.dart +++ b/test/query_generator/deprecated/deprecated_field_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -61,7 +62,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'deprecatedField'), - annotations: [r'''Deprecated('message 2')'''], + annotations: [ + StringAnnotation(name: r'''Deprecated('message 2')''') + ], isResolveType: false) ], factoryPossibilities: {}, @@ -77,7 +80,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'deprecatedField'), - annotations: [r'''Deprecated('message 2')'''], + annotations: [ + StringAnnotation(name: r'''Deprecated('message 2')''') + ], isResolveType: false) ], factoryPossibilities: {}, @@ -90,7 +95,9 @@ final LibraryDefinition libraryDefinition = type: TypeName( name: r'SomeQuery$_QueryResponse$_deprecatedObject'), name: ClassPropertyName(name: r'deprecatedObject'), - annotations: [r'''Deprecated('message')'''], + annotations: [ + StringAnnotation(name: r'''Deprecated('message')''') + ], isResolveType: false), ClassProperty( type: ListOfTypeName( diff --git a/test/query_generator/deprecated/deprecated_input_object_field_test.dart b/test/query_generator/deprecated/deprecated_input_object_field_test.dart index c5ce744e..3244b625 100644 --- a/test/query_generator/deprecated/deprecated_input_object_field_test.dart +++ b/test/query_generator/deprecated/deprecated_input_object_field_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -82,7 +83,10 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'd'), - annotations: [r'''Deprecated('deprecated input field')'''], + annotations: [ + StringAnnotation( + name: r'''Deprecated('deprecated input field')''') + ], isResolveType: false) ], factoryPossibilities: {}, @@ -146,10 +150,10 @@ class Input extends JsonSerializable with EquatableMixin { factory Input.fromJson(Map json) => _$InputFromJson(json); - late String s; + final String s; @Deprecated('deprecated input field') - String? d; + final String? d; @override List get props => [s, d]; @@ -165,7 +169,7 @@ class CustomArguments extends JsonSerializable with EquatableMixin { factory CustomArguments.fromJson(Map json) => _$CustomArgumentsFromJson(json); - late Input input; + final Input input; @override List get props => [input]; diff --git a/test/query_generator/deprecated/deprecated_interface_field_test.dart b/test/query_generator/deprecated/deprecated_interface_field_test.dart index c9d0a8f3..fbbdc8a1 100644 --- a/test/query_generator/deprecated/deprecated_interface_field_test.dart +++ b/test/query_generator/deprecated/deprecated_interface_field_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -119,7 +120,8 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'String'), name: ClassPropertyName(name: r'deprecatedField'), annotations: [ - r'''Deprecated('deprecated interface field')''' + StringAnnotation( + name: r'''Deprecated('deprecated interface field')''') ], isResolveType: false) ], diff --git a/test/query_generator/enums/enum_duplication_test.dart b/test/query_generator/enums/enum_duplication_test.dart index 9da091be..e5bbf9e9 100644 --- a/test/query_generator/enums/enum_duplication_test.dart +++ b/test/query_generator/enums/enum_duplication_test.dart @@ -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:test/test.dart'; @@ -74,7 +75,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], @@ -111,7 +114,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], diff --git a/test/query_generator/enums/enum_list_test.dart b/test/query_generator/enums/enum_list_test.dart index f5232b36..8f354444 100644 --- a/test/query_generator/enums/enum_list_test.dart +++ b/test/query_generator/enums/enum_list_test.dart @@ -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:test/test.dart'; @@ -61,7 +62,9 @@ final LibraryDefinition libraryDefinition = typeName: TypeName(name: r'MyEnum'), isNonNull: false), name: ClassPropertyName(name: r'le'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], diff --git a/test/query_generator/enums/filter_enum_test.dart b/test/query_generator/enums/filter_enum_test.dart index af915900..55258c97 100644 --- a/test/query_generator/enums/filter_enum_test.dart +++ b/test/query_generator/enums/filter_enum_test.dart @@ -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:test/test.dart'; @@ -102,7 +103,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], @@ -127,7 +130,10 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'_InputInputEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: $InputInputEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: + r'$InputInputEnum.artemisUnknown')) ], isResolveType: false) ], @@ -140,7 +146,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'input_enum', isNonNull: true), name: QueryInputName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: InputEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'InputEnum.artemisUnknown')) ]), QueryInput( type: TypeName(name: r'Input', isNonNull: true), @@ -197,7 +205,7 @@ class Input extends JsonSerializable with EquatableMixin { factory Input.fromJson(Map json) => _$InputFromJson(json); @JsonKey(unknownEnumValue: $InputInputEnum.artemisUnknown) - $InputInputEnum? e; + final $InputInputEnum? e; @override List get props => [e]; diff --git a/test/query_generator/enums/input_enum_list_test.dart b/test/query_generator/enums/input_enum_list_test.dart index b3a4d2d0..121bff5f 100644 --- a/test/query_generator/enums/input_enum_list_test.dart +++ b/test/query_generator/enums/input_enum_list_test.dart @@ -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:test/test.dart'; @@ -74,7 +75,10 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'ArticleType', isNonNull: true), name: ClassPropertyName(name: r'article_type'), annotations: [ - r'''JsonKey(name: 'article_type', unknownEnumValue: ArticleType.artemisUnknown)''' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + name: r'article_type', + unknownEnumValue: r'ArticleType.artemisUnknown')) ], isResolveType: false) ], @@ -104,7 +108,9 @@ final LibraryDefinition libraryDefinition = isNonNull: false), name: QueryInputName(name: r'article_type_in'), annotations: [ - r'JsonKey(unknownEnumValue: ArticleType.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'ArticleType.artemisUnknown')) ]) ], generateHelpers: true, diff --git a/test/query_generator/enums/input_enum_test.dart b/test/query_generator/enums/input_enum_test.dart index c5aed1f6..fa042ac5 100644 --- a/test/query_generator/enums/input_enum_test.dart +++ b/test/query_generator/enums/input_enum_test.dart @@ -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:test/test.dart'; @@ -58,7 +59,7 @@ const query = r''' '''; final LibraryDefinition libraryDefinition = - LibraryDefinition(basename: r'query.graphql', queries: [ +LibraryDefinition(basename: r'query.graphql', queries: [ QueryDefinition( name: QueryName(name: r'Custom$_QueryRoot'), operationName: r'custom', @@ -84,14 +85,18 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'my'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false), ClassProperty( type: TypeName(name: r'OtherEnum'), name: ClassPropertyName(name: r'other'), annotations: [ - r'JsonKey(unknownEnumValue: OtherEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'OtherEnum.artemisUnknown')) ], isResolveType: false) ], @@ -116,7 +121,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum', isNonNull: true), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], @@ -128,7 +135,9 @@ final LibraryDefinition libraryDefinition = QueryInput( type: TypeName(name: r'String', isNonNull: true), name: QueryInputName(name: r'_id'), - annotations: [r'''JsonKey(name: '_id')''']), + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_id')) + ]), QueryInput( type: TypeName(name: r'Input', isNonNull: true), name: QueryInputName(name: r'input')), @@ -136,7 +145,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'OtherEnum', isNonNull: true), name: QueryInputName(name: r'o'), annotations: [ - r'JsonKey(unknownEnumValue: OtherEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'OtherEnum.artemisUnknown')) ]) ], generateHelpers: true, @@ -196,7 +207,7 @@ class Input extends JsonSerializable with EquatableMixin { factory Input.fromJson(Map json) => _$InputFromJson(json); @JsonKey(unknownEnumValue: MyEnum.artemisUnknown) - late MyEnum e; + final MyEnum e; @override List get props => [e]; @@ -230,12 +241,12 @@ class CustomArguments extends JsonSerializable with EquatableMixin { _$CustomArgumentsFromJson(json); @JsonKey(name: '_id') - late String $id; + final String $id; - late Input input; + final Input input; @JsonKey(unknownEnumValue: OtherEnum.artemisUnknown) - late OtherEnum o; + final OtherEnum o; @override List get props => [$id, input, o]; diff --git a/test/query_generator/enums/kw_prefix_test.dart b/test/query_generator/enums/kw_prefix_test.dart index 2399f61c..5a6f94a1 100644 --- a/test/query_generator/enums/kw_prefix_test.dart +++ b/test/query_generator/enums/kw_prefix_test.dart @@ -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:test/test.dart'; @@ -94,7 +95,10 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'SQLOperator'), name: ClassPropertyName(name: r'operator'), annotations: [ - r'''JsonKey(name: 'operator', unknownEnumValue: SQLOperator.artemisUnknown)''' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + name: r'operator', + unknownEnumValue: r'SQLOperator.artemisUnknown')) ], isResolveType: false), ClassProperty( @@ -164,9 +168,9 @@ class ArticleTitleWhereConditions extends JsonSerializable with EquatableMixin { _$ArticleTitleWhereConditionsFromJson(json); @JsonKey(name: 'operator', unknownEnumValue: SQLOperator.artemisUnknown) - SQLOperator? kw$operator; + final SQLOperator? kw$operator; - String? value; + final String? value; @override List get props => [kw$operator, value]; diff --git a/test/query_generator/enums/query_enum_test.dart b/test/query_generator/enums/query_enum_test.dart index 7c05b22e..0f211674 100644 --- a/test/query_generator/enums/query_enum_test.dart +++ b/test/query_generator/enums/query_enum_test.dart @@ -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:test/test.dart'; @@ -63,7 +64,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], diff --git a/test/query_generator/interfaces/interface_fragment_glob_test.dart b/test/query_generator/interfaces/interface_fragment_glob_test.dart index b51dfa74..9cfc2c27 100644 --- a/test/query_generator/interfaces/interface_fragment_glob_test.dart +++ b/test/query_generator/interfaces/interface_fragment_glob_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -125,7 +126,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: { diff --git a/test/query_generator/interfaces/interface_possible_types_test.dart b/test/query_generator/interfaces/interface_possible_types_test.dart index a2094601..36ebd439 100644 --- a/test/query_generator/interfaces/interface_possible_types_test.dart +++ b/test/query_generator/interfaces/interface_possible_types_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -106,7 +107,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: { diff --git a/test/query_generator/interfaces/interface_test.dart b/test/query_generator/interfaces/interface_test.dart index 76c1aa35..6d7e66b8 100644 --- a/test/query_generator/interfaces/interface_test.dart +++ b/test/query_generator/interfaces/interface_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -116,7 +117,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: { diff --git a/test/query_generator/mutations_and_inputs/complex_input_objects_test.dart b/test/query_generator/mutations_and_inputs/complex_input_objects_test.dart index 197f4e0e..d4605e4c 100644 --- a/test/query_generator/mutations_and_inputs/complex_input_objects_test.dart +++ b/test/query_generator/mutations_and_inputs/complex_input_objects_test.dart @@ -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:test/test.dart'; @@ -92,7 +93,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false), ClassProperty( @@ -168,14 +171,14 @@ class ComplexInput extends JsonSerializable with EquatableMixin { factory ComplexInput.fromJson(Map json) => _$ComplexInputFromJson(json); - late String s; + final String s; @JsonKey(unknownEnumValue: MyEnum.artemisUnknown) - MyEnum? e; + final MyEnum? e; - List? ls; + final List? ls; - List?>? i; + final List?>? i; @override List get props => [s, e, ls, i]; @@ -200,7 +203,7 @@ class SomeQueryArguments extends JsonSerializable with EquatableMixin { factory SomeQueryArguments.fromJson(Map json) => _$SomeQueryArgumentsFromJson(json); - late ComplexInput filter; + final ComplexInput filter; @override List get props => [filter]; diff --git a/test/query_generator/mutations_and_inputs/custom_scalars_on_input_objects_test.dart b/test/query_generator/mutations_and_inputs/custom_scalars_on_input_objects_test.dart index 089c9708..1e3d7279 100644 --- a/test/query_generator/mutations_and_inputs/custom_scalars_on_input_objects_test.dart +++ b/test/query_generator/mutations_and_inputs/custom_scalars_on_input_objects_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -93,14 +94,20 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyUuid', isNonNull: true), name: ClassPropertyName(name: r'id'), annotations: [ - r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: r'fromGraphQLMyUuidToDartMyUuid', + toJson: r'fromDartMyUuidToGraphQLMyUuid')) ], isResolveType: false), ClassProperty( type: TypeName(name: r'MyUuid'), name: ClassPropertyName(name: r'idNullabe'), annotations: [ - r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: r'fromGraphQLMyUuidToDartMyUuidNullable', + toJson: r'fromDartMyUuidToGraphQLMyUuidNullable')) ], isResolveType: false) ], @@ -116,14 +123,21 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'MyUuid'), name: QueryInputName(name: r'previousId'), annotations: [ - r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: r'fromGraphQLMyUuidToDartMyUuidNullable', + toJson: r'fromDartMyUuidToGraphQLMyUuidNullable')) ]), QueryInput( type: ListOfTypeName( typeName: TypeName(name: r'MyUuid'), isNonNull: false), name: QueryInputName(name: r'listIds'), annotations: [ - r'JsonKey(fromJson: fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable, toJson: fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: + r'fromGraphQLListMyUuidToDartListMyUuidNullable', + toJson: r'fromDartListMyUuidToGraphQLListMyUuidNullable')) ]) ], generateHelpers: true, @@ -186,12 +200,12 @@ class Input extends JsonSerializable with EquatableMixin { @JsonKey( fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid) - late MyUuid id; + final MyUuid id; @JsonKey( - fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, - toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable) - MyUuid? idNullabe; + fromJson: fromGraphQLMyUuidToDartMyUuidNullable, + toJson: fromDartMyUuidToGraphQLMyUuidNullable) + final MyUuid? idNullabe; @override List get props => [id, idNullabe]; @@ -207,7 +221,7 @@ class CustomArguments extends JsonSerializable with EquatableMixin { factory CustomArguments.fromJson(Map json) => _$CustomArgumentsFromJson(json); - late Input input; + final Input input; @JsonKey( fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, diff --git a/test/query_generator/mutations_and_inputs/mutations_test.dart b/test/query_generator/mutations_and_inputs/mutations_test.dart index 63f0a547..129f7ab0 100644 --- a/test/query_generator/mutations_and_inputs/mutations_test.dart +++ b/test/query_generator/mutations_and_inputs/mutations_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -120,7 +121,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_s'), - annotations: [r'''JsonKey(name: '_s')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_s')) + ], isResolveType: false) ], factoryPossibilities: {}, @@ -133,7 +136,9 @@ final LibraryDefinition libraryDefinition = type: TypeName( name: r'$custom$_MutationRoot$_$MutationResponse'), name: ClassPropertyName(name: r'_mut'), - annotations: [r'''JsonKey(name: '_mut')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_mut')) + ], isResolveType: false) ], factoryPossibilities: {}, @@ -145,7 +150,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String', isNonNull: true), name: ClassPropertyName(name: r'_s'), - annotations: [r'''JsonKey(name: '_s')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_s')) + ], isResolveType: false) ], factoryPossibilities: {}, @@ -209,7 +216,7 @@ class Input extends JsonSerializable with EquatableMixin { factory Input.fromJson(Map json) => _$InputFromJson(json); - late String s; + final String s; @override List get props => [s]; @@ -259,7 +266,7 @@ class $Input extends JsonSerializable with EquatableMixin { factory $Input.fromJson(Map json) => _$$InputFromJson(json); @JsonKey(name: '_s') - late String $s; + final String $s; @override List get props => [$s]; @@ -275,7 +282,7 @@ class CustomArguments extends JsonSerializable with EquatableMixin { factory CustomArguments.fromJson(Map json) => _$CustomArgumentsFromJson(json); - late Input input; + final Input input; @override List get props => [input]; @@ -345,7 +352,7 @@ class $customArguments extends JsonSerializable with EquatableMixin { factory $customArguments.fromJson(Map json) => _$$customArgumentsFromJson(json); - late $Input input; + final $Input input; @override List get props => [input]; diff --git a/test/query_generator/mutations_and_inputs/non_nullable_list_inputs_test.dart b/test/query_generator/mutations_and_inputs/non_nullable_list_inputs_test.dart index 63d88931..e8eb2695 100644 --- a/test/query_generator/mutations_and_inputs/non_nullable_list_inputs_test.dart +++ b/test/query_generator/mutations_and_inputs/non_nullable_list_inputs_test.dart @@ -155,19 +155,19 @@ class SomeQueryArguments extends JsonSerializable with EquatableMixin { final int? i; - late int inn; + final int inn; final List? li; final List? linn; - late List lnni; + final List lnni; - late List lnninn; + final List lnninn; final List?>? matrix; - late List> matrixnn; + final List> matrixnn; @override List get props => [i, inn, li, linn, lnni, lnninn, matrix, matrixnn]; diff --git a/test/query_generator/naming/casing_conversion_test.dart b/test/query_generator/naming/casing_conversion_test.dart index 23870937..40d39879 100644 --- a/test/query_generator/naming/casing_conversion_test.dart +++ b/test/query_generator/naming/casing_conversion_test.dart @@ -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:test/test.dart'; @@ -113,25 +114,35 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'PascalCaseType'), name: ClassPropertyName(name: r'PascalCaseField'), - annotations: [r'''JsonKey(name: 'PascalCaseField')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'PascalCaseField')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'SnakeCaseType'), name: ClassPropertyName(name: r'snake_case_field'), - annotations: [r'''JsonKey(name: 'snake_case_field')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'snake_case_field')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'ScreamingSnakeCaseType'), name: ClassPropertyName(name: r'SCREAMING_SNAKE_CASE_FIELD'), annotations: [ - r'''JsonKey(name: 'SCREAMING_SNAKE_CASE_FIELD')''' + JsonKeyAnnotation( + jsonKey: + JsonKeyItem(name: r'SCREAMING_SNAKE_CASE_FIELD')) ], isResolveType: false), ClassProperty( type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], @@ -159,25 +170,35 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'PascalCaseTypeInput'), name: ClassPropertyName(name: r'PascalCaseField'), - annotations: [r'''JsonKey(name: 'PascalCaseField')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'PascalCaseField')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'SnakeCaseTypeInput'), name: ClassPropertyName(name: r'snake_case_field'), - annotations: [r'''JsonKey(name: 'snake_case_field')'''], + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem(name: r'snake_case_field')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'ScreamingSnakeCaseTypeInput'), name: ClassPropertyName(name: r'SCREAMING_SNAKE_CASE_FIELD'), annotations: [ - r'''JsonKey(name: 'SCREAMING_SNAKE_CASE_FIELD')''' + JsonKeyAnnotation( + jsonKey: + JsonKeyItem(name: r'SCREAMING_SNAKE_CASE_FIELD')) ], isResolveType: false), ClassProperty( type: TypeName(name: r'MyEnum'), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(unknownEnumValue: MyEnum.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) ], isResolveType: false) ], @@ -262,19 +283,19 @@ class Input extends JsonSerializable with EquatableMixin { factory Input.fromJson(Map json) => _$InputFromJson(json); - CamelCaseTypeInput? camelCaseField; + final CamelCaseTypeInput? camelCaseField; @JsonKey(name: 'PascalCaseField') - PascalCaseTypeInput? pascalCaseField; + final PascalCaseTypeInput? pascalCaseField; @JsonKey(name: 'snake_case_field') - SnakeCaseTypeInput? snakeCaseField; + final SnakeCaseTypeInput? snakeCaseField; @JsonKey(name: 'SCREAMING_SNAKE_CASE_FIELD') - ScreamingSnakeCaseTypeInput? screamingSnakeCaseField; + final ScreamingSnakeCaseTypeInput? screamingSnakeCaseField; @JsonKey(unknownEnumValue: MyEnum.artemisUnknown) - MyEnum? e; + final MyEnum? e; @override List get props => [ @@ -309,7 +330,7 @@ class SomeQueryArguments extends JsonSerializable with EquatableMixin { factory SomeQueryArguments.fromJson(Map json) => _$SomeQueryArgumentsFromJson(json); - late Input filter; + final Input filter; @override List get props => [filter]; diff --git a/test/query_generator/scalars/custom_scalars_test.dart b/test/query_generator/scalars/custom_scalars_test.dart index 64bacd66..d0eee4dc 100644 --- a/test/query_generator/scalars/custom_scalars_test.dart +++ b/test/query_generator/scalars/custom_scalars_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -138,7 +139,12 @@ final LibraryDefinition libraryDefinitionWithCustomParserFns = type: TypeName(name: r'MyDartUuid'), name: ClassPropertyName(name: r'a'), annotations: [ - r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyDartUuidNullable, toJson: fromDartMyDartUuidNullableToGraphQLMyUuidNullable)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: + r'fromGraphQLMyUuidNullableToDartMyDartUuidNullable', + toJson: + r'fromDartMyDartUuidNullableToGraphQLMyUuidNullable')) ], isResolveType: false) ], @@ -165,14 +171,22 @@ final LibraryDefinition libraryDefinitionWithCustomImports = type: TypeName(name: r'MyUuid'), name: ClassPropertyName(name: r'a'), annotations: [ - r'JsonKey(fromJson: fromGraphQLMyUuidNullableToDartMyUuidNullable, toJson: fromDartMyUuidNullableToGraphQLMyUuidNullable)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: + r'fromGraphQLMyUuidNullableToDartMyUuidNullable', + toJson: + r'fromDartMyUuidNullableToGraphQLMyUuidNullable')) ], isResolveType: false), ClassProperty( type: TypeName(name: r'MyUuid', isNonNull: true), name: ClassPropertyName(name: r'b'), annotations: [ - r'JsonKey(fromJson: fromGraphQLMyUuidToDartMyUuid, toJson: fromDartMyUuidToGraphQLMyUuid)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: r'fromGraphQLMyUuidToDartMyUuid', + toJson: r'fromDartMyUuidToGraphQLMyUuid')) ], isResolveType: false), ClassProperty( @@ -181,7 +195,10 @@ final LibraryDefinition libraryDefinitionWithCustomImports = isNonNull: true), name: ClassPropertyName(name: r'c'), annotations: [ - r'JsonKey(fromJson: fromGraphQLListMyUuidToDartListMyUuid, toJson: fromDartListMyUuidToGraphQLListMyUuid)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: r'fromGraphQLListMyUuidToDartListMyUuid', + toJson: r'fromDartListMyUuidToGraphQLListMyUuid')) ], isResolveType: false), ClassProperty( @@ -189,7 +206,12 @@ final LibraryDefinition libraryDefinitionWithCustomImports = typeName: TypeName(name: r'MyUuid'), isNonNull: false), name: ClassPropertyName(name: r'd'), annotations: [ - r'JsonKey(fromJson: fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable, toJson: fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: + r'fromGraphQLListNullableMyUuidNullableToDartListNullableMyUuidNullable', + toJson: + r'fromDartListNullableMyUuidNullableToGraphQLListNullableMyUuidNullable')) ], isResolveType: false), ClassProperty( @@ -197,7 +219,12 @@ final LibraryDefinition libraryDefinitionWithCustomImports = typeName: TypeName(name: r'MyUuid'), isNonNull: true), name: ClassPropertyName(name: r'e'), annotations: [ - r'JsonKey(fromJson: fromGraphQLListMyUuidNullableToDartListMyUuidNullable, toJson: fromDartListMyUuidNullableToGraphQLListMyUuidNullable)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: + r'fromGraphQLListMyUuidNullableToDartListMyUuidNullable', + toJson: + r'fromDartListMyUuidNullableToGraphQLListMyUuidNullable')) ], isResolveType: false), ClassProperty( @@ -206,7 +233,12 @@ final LibraryDefinition libraryDefinitionWithCustomImports = isNonNull: false), name: ClassPropertyName(name: r'f'), annotations: [ - r'JsonKey(fromJson: fromGraphQLListNullableMyUuidToDartListNullableMyUuid, toJson: fromDartListNullableMyUuidToGraphQLListNullableMyUuid)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + fromJson: + r'fromGraphQLListNullableMyUuidToDartListNullableMyUuid', + toJson: + r'fromDartListNullableMyUuidToGraphQLListNullableMyUuid')) ], isResolveType: false) ], diff --git a/test/query_generator/subscription_test.dart b/test/query_generator/subscription_test.dart index 5cc4e0e6..4e85a001 100644 --- a/test/query_generator/subscription_test.dart +++ b/test/query_generator/subscription_test.dart @@ -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:test/test.dart'; @@ -105,7 +106,9 @@ final LibraryDefinition libraryDefinition = type: TypeName(name: r'UserType', isNonNull: true), name: ClassPropertyName(name: r'userType'), annotations: [ - r'JsonKey(unknownEnumValue: UserType.artemisUnknown)' + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'UserType.artemisUnknown')) ], isResolveType: false) ], diff --git a/test/query_generator/to_json_excluded_test.dart b/test/query_generator/to_json_excluded_test.dart new file mode 100644 index 00000000..f5327a2d --- /dev/null +++ b/test/query_generator/to_json_excluded_test.dart @@ -0,0 +1,306 @@ +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:test/test.dart'; + +import '../helpers.dart'; + +void main() { + group('To JSON exclude on complex input objects', () { + test( + 'On complex input objects', + () async => testGenerator( + toJsonExclude: true, + query: r''' + query some_query($filter: ComplexInput) { + o(filter: $filter) { + s + } + }''', + schema: r''' + schema { + query: QueryRoot + } + + type QueryRoot { + o(filter: ComplexInput): SomeObject + } + + input ComplexInput { + s: String! + e: MyEnum + ls: [String] + i: [[Int]] + } + + type SomeObject { + s: String + } + + enum MyEnum { + value1 + value2 + } + ''', + libraryDefinition: libraryDefinition, + generatedFile: generatedFile, + generateHelpers: true, + ), + ); + }); +} + +final LibraryDefinition libraryDefinition = + LibraryDefinition(basename: r'query.graphql', queries: [ + QueryDefinition( + name: QueryName(name: r'SomeQuery$_QueryRoot'), + operationName: r'some_query', + classes: [ + EnumDefinition(name: EnumName(name: r'MyEnum'), values: [ + EnumValueDefinition(name: EnumValueName(name: r'value1')), + EnumValueDefinition(name: EnumValueName(name: r'value2')), + EnumValueDefinition(name: EnumValueName(name: r'ARTEMIS_UNKNOWN')) + ]), + ClassDefinition( + name: ClassName(name: r'SomeQuery$_QueryRoot$_SomeObject'), + properties: [ + ClassProperty( + type: TypeName(name: r'String'), + name: ClassPropertyName(name: r's'), + isResolveType: false) + ], + factoryPossibilities: {}, + typeNameField: ClassPropertyName(name: r'__typename'), + isInput: false), + ClassDefinition( + name: ClassName(name: r'SomeQuery$_QueryRoot'), + properties: [ + ClassProperty( + type: TypeName(name: r'SomeQuery$_QueryRoot$_SomeObject'), + name: ClassPropertyName(name: r'o'), + isResolveType: false) + ], + factoryPossibilities: {}, + typeNameField: ClassPropertyName(name: r'__typename'), + isInput: false), + ClassDefinition( + name: ClassName(name: r'ComplexInput'), + properties: [ + ClassProperty( + type: TypeName(name: r'String', isNonNull: true), + name: ClassPropertyName(name: r's'), + isResolveType: false), + ClassProperty( + type: TypeName(name: r'MyEnum'), + name: ClassPropertyName(name: r'e'), + annotations: [ + JsonKeyAnnotation( + jsonKey: JsonKeyItem( + unknownEnumValue: r'MyEnum.artemisUnknown')) + ], + isResolveType: false), + ClassProperty( + type: ListOfTypeName( + typeName: TypeName(name: r'String'), isNonNull: false), + name: ClassPropertyName(name: r'ls'), + isResolveType: false), + ClassProperty( + type: ListOfTypeName( + typeName: ListOfTypeName( + typeName: TypeName(name: r'int'), isNonNull: false), + isNonNull: false), + name: ClassPropertyName(name: r'i'), + isResolveType: false) + ], + factoryPossibilities: {}, + typeNameField: ClassPropertyName(name: r'__typename'), + isInput: true) + ], + inputs: [ + QueryInput( + type: TypeName(name: r'ComplexInput'), + name: QueryInputName(name: r'filter')) + ], + generateHelpers: true, + suffix: r'Query') +]); + +const generatedFile = r'''// GENERATED CODE - DO NOT MODIFY BY HAND +// @dart = 2.12 + +import 'package:artemis/artemis.dart'; +import 'package:json_annotation/json_annotation.dart'; +import 'package:equatable/equatable.dart'; +import 'package:gql/ast.dart'; +part 'query.graphql.g.dart'; + +class Nullable { + final T _value; + + Nullable(this._value); + + T get value => _value; +} + +dynamic _nullableToJson(Nullable value) { + return value; +} + +Nullable? _nullableFromJson(T value) { + return Nullable(value); +} + +@JsonSerializable(explicitToJson: true) +class SomeQuery$QueryRoot$SomeObject extends JsonSerializable + with EquatableMixin { + SomeQuery$QueryRoot$SomeObject(); + + factory SomeQuery$QueryRoot$SomeObject.fromJson(Map json) => + _$SomeQuery$QueryRoot$SomeObjectFromJson(json); + + String? s; + + @override + List get props => [s]; + @override + Map toJson() => _$SomeQuery$QueryRoot$SomeObjectToJson(this); +} + +@JsonSerializable(explicitToJson: true) +class SomeQuery$QueryRoot extends JsonSerializable with EquatableMixin { + SomeQuery$QueryRoot(); + + factory SomeQuery$QueryRoot.fromJson(Map json) => + _$SomeQuery$QueryRootFromJson(json); + + SomeQuery$QueryRoot$SomeObject? o; + + @override + List get props => [o]; + @override + Map toJson() => _$SomeQuery$QueryRootToJson(this); +} + +@JsonSerializable(explicitToJson: true) +class ComplexInput extends JsonSerializable with EquatableMixin { + ComplexInput({required this.s, this.e, this.ls, this.i}); + + factory ComplexInput.fromJson(Map json) => + _$ComplexInputFromJson(json); + + final String s; + + @JsonKey( + fromJson: _nullableFromJson, + toJson: _nullableToJson, + unknownEnumValue: MyEnum.artemisUnknown) + @JsonKey(unknownEnumValue: MyEnum.artemisUnknown) + final Nullable? e; + + @JsonKey(fromJson: _nullableFromJson, toJson: _nullableToJson) + final Nullable?>? ls; + + @JsonKey(fromJson: _nullableFromJson, toJson: _nullableToJson) + final Nullable?>?>? i; + + @override + List get props => [s, e, ls, i]; + @override + Map toJson() => _excludeNullable(_$ComplexInputToJson(this)); + @override + Map _excludeNullable(Map json) { + if (e == null) json.remove('e'); + if (ls == null) json.remove('ls'); + if (i == null) json.remove('i'); + + return json; + } +} + +enum MyEnum { + @JsonValue('value1') + value1, + @JsonValue('value2') + value2, + @JsonValue('ARTEMIS_UNKNOWN') + artemisUnknown, +} + +@JsonSerializable(explicitToJson: true) +class SomeQueryArguments extends JsonSerializable with EquatableMixin { + SomeQueryArguments({this.filter}); + + @override + factory SomeQueryArguments.fromJson(Map json) => + _$SomeQueryArgumentsFromJson(json); + + @JsonKey(fromJson: _nullableFromJson, toJson: _nullableToJson) + final Nullable? filter; + + @override + List get props => [filter]; + @override + Map toJson() => + _excludeNullable(_$SomeQueryArgumentsToJson(this)); + @override + Map _excludeNullable(Map json) { + if (filter == null) json.remove('filter'); + + return json; + } +} + +final SOME_QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [ + OperationDefinitionNode( + type: OperationType.query, + name: NameNode(value: 'some_query'), + variableDefinitions: [ + VariableDefinitionNode( + variable: VariableNode(name: NameNode(value: 'filter')), + type: NamedTypeNode( + name: NameNode(value: 'ComplexInput'), isNonNull: false), + defaultValue: DefaultValueNode(value: null), + directives: []) + ], + directives: [], + selectionSet: SelectionSetNode(selections: [ + FieldNode( + name: NameNode(value: 'o'), + alias: null, + arguments: [ + ArgumentNode( + name: NameNode(value: 'filter'), + value: VariableNode(name: NameNode(value: 'filter'))) + ], + directives: [], + selectionSet: SelectionSetNode(selections: [ + FieldNode( + name: NameNode(value: 's'), + alias: null, + arguments: [], + directives: [], + selectionSet: null) + ])) + ])) +]); + +class SomeQueryQuery + extends GraphQLQuery { + SomeQueryQuery({required this.variables}); + + @override + final DocumentNode document = SOME_QUERY_QUERY_DOCUMENT; + + @override + final String operationName = 'some_query'; + + @override + final SomeQueryArguments variables; + + @override + List get props => [document, operationName, variables]; + @override + SomeQuery$QueryRoot parse(Map json) => + SomeQuery$QueryRoot.fromJson(json); +} +'''; diff --git a/test/query_generator/union/union_types_test.dart b/test/query_generator/union/union_types_test.dart index a548d8ce..aad4380c 100644 --- a/test/query_generator/union/union_types_test.dart +++ b/test/query_generator/union/union_types_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -89,32 +90,44 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_'), - annotations: [r'''JsonKey(name: '_')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_a'), - annotations: [r'''JsonKey(name: '_a')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_a')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_a_a'), - annotations: [r'''JsonKey(name: '_a_a')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_a_a')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_a_a_'), - annotations: [r'''JsonKey(name: '_a_a_')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_a_a_')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_new'), - annotations: [r'''JsonKey(name: '_new')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_new')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], extension: ClassName(name: r'SomeQuery$_SomeObject$_SomeUnion'), @@ -131,37 +144,51 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_'), - annotations: [r'''JsonKey(name: '_')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_b'), - annotations: [r'''JsonKey(name: '_b')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_b')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_b_b'), - annotations: [r'''JsonKey(name: '_b_b')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_b_b')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'_b_b_'), - annotations: [r'''JsonKey(name: '_b_b_')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'_b_b_')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'new'), - annotations: [r'''JsonKey(name: 'new')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'new')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'IN'), - annotations: [r'''JsonKey(name: 'IN')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'IN')) + ], isResolveType: false), ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], extension: ClassName(name: r'SomeQuery$_SomeObject$_SomeUnion'), @@ -174,7 +201,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: { diff --git a/test/query_generator/union/union_with_nested_types_test.dart b/test/query_generator/union/union_with_nested_types_test.dart index 7d8c3f58..83c8e9f9 100644 --- a/test/query_generator/union/union_with_nested_types_test.dart +++ b/test/query_generator/union/union_with_nested_types_test.dart @@ -1,3 +1,4 @@ +import 'package:artemis/generator/data/annotation.dart'; import 'package:artemis/generator/data/data.dart'; import 'package:test/test.dart'; @@ -167,7 +168,9 @@ final LibraryDefinition libraryDefinition = ClassProperty( type: TypeName(name: r'String'), name: ClassPropertyName(name: r'__typename'), - annotations: [r'''JsonKey(name: '__typename')'''], + annotations: [ + JsonKeyAnnotation(jsonKey: JsonKeyItem(name: r'__typename')) + ], isResolveType: true) ], factoryPossibilities: {