diff --git a/codegen/end_to_end_test_tristate/build.yaml b/codegen/end_to_end_test_tristate/build.yaml index 34f5af92..06b2243a 100644 --- a/codegen/end_to_end_test_tristate/build.yaml +++ b/codegen/end_to_end_test_tristate/build.yaml @@ -12,16 +12,21 @@ targets: options: schema: end_to_end_test_tristate|lib/graphql/schema.graphql custom_serializers: - - import: 'package:end_to_end_test_tristate/date_serializer.dart' + - import: "package:end_to_end_test_tristate/date_serializer.dart" name: DateSerializer - - import: 'package:end_to_end_test_tristate/custom_field_serializer.dart' + - import: "package:end_to_end_test_tristate/custom_field_serializer.dart" name: CustomFieldSerializer + - import: "package:end_to_end_test_tristate/custom_date_serializer.dart" + name: CustomDateSerializer type_overrides: - Date: + DateTime: name: DateTime CustomField: name: CustomField - import: 'package:end_to_end_test_tristate/custom_field.dart' + import: "package:end_to_end_test_tristate/custom_field.dart" + Date: + name: CustomDate + import: "package:end_to_end_test_tristate/custom_date.dart" gql_build|schema_builder: enabled: true options: @@ -29,11 +34,14 @@ targets: enum_fallbacks: LengthUnit: METER type_overrides: - Date: + DateTime: name: DateTime CustomField: name: CustomField - import: 'package:end_to_end_test_tristate/custom_field.dart' + import: "package:end_to_end_test_tristate/custom_field.dart" + Date: + name: CustomDate + import: "package:end_to_end_test_tristate/custom_date.dart" gql_build|data_builder: enabled: true options: @@ -42,11 +50,14 @@ targets: when: true maybeWhen: true type_overrides: - Date: + DateTime: name: DateTime CustomField: name: CustomField - import: 'package:end_to_end_test_tristate/custom_field.dart' + import: "package:end_to_end_test_tristate/custom_field.dart" + Date: + name: CustomDate + import: "package:end_to_end_test_tristate/custom_date.dart" gql_build|var_builder: enabled: true options: @@ -54,8 +65,11 @@ targets: tristate_optionals: true vars_create_factories: true type_overrides: - Date: + DateTime: name: DateTime CustomField: name: CustomField - import: 'package:end_to_end_test_tristate/custom_field.dart' + import: "package:end_to_end_test_tristate/custom_field.dart" + Date: + name: CustomDate + import: "package:end_to_end_test_tristate/custom_date.dart" diff --git a/codegen/end_to_end_test_tristate/lib/custom_date.dart b/codegen/end_to_end_test_tristate/lib/custom_date.dart new file mode 100644 index 00000000..52f308d5 --- /dev/null +++ b/codegen/end_to_end_test_tristate/lib/custom_date.dart @@ -0,0 +1,19 @@ +class CustomDate extends DateTime { + CustomDate( + super.year, + super.month, + super.day, + ); + + static CustomDate parse(String formattedString) { + final DateTime dateTime = DateTime.parse(formattedString); + return CustomDate(dateTime.year, dateTime.month, dateTime.day); + } + + @override + CustomDate toLocal() { + final DateTime localDateTime = super.toLocal(); + return CustomDate( + localDateTime.year, localDateTime.month, localDateTime.day); + } +} diff --git a/codegen/end_to_end_test_tristate/lib/custom_date_serializer.dart b/codegen/end_to_end_test_tristate/lib/custom_date_serializer.dart new file mode 100644 index 00000000..51f1349e --- /dev/null +++ b/codegen/end_to_end_test_tristate/lib/custom_date_serializer.dart @@ -0,0 +1,30 @@ +import 'package:built_value/serializer.dart'; +import 'package:end_to_end_test_tristate/custom_date.dart'; + +class CustomDateSerializer implements PrimitiveSerializer { + @override + CustomDate deserialize( + Serializers serializers, + Object serialized, { + FullType specifiedType = FullType.unspecified, + }) { + assert(serialized is String, + "CustomDateSerializer expected 'String' but got ${serialized.runtimeType}"); + return CustomDate.parse(serialized as String).toLocal(); + } + + @override + String serialize( + Serializers serializers, + CustomDate date, { + FullType specifiedType = FullType.unspecified, + }) { + return date.toUtc().toIso8601String().split('T').first; + } + + @override + Iterable get types => [CustomDate]; + + @override + String get wireName => 'Date'; +} diff --git a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.ast.gql.dart b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.ast.gql.dart index 3b75b47a..57843f51 100644 --- a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.ast.gql.dart +++ b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.ast.gql.dart @@ -247,7 +247,7 @@ const Mutation = _i1.ObjectTypeDefinitionNode( name: _i1.NameNode(value: 'createdAt'), directives: [], type: _i1.NamedTypeNode( - name: _i1.NameNode(value: 'Date'), + name: _i1.NameNode(value: 'DateTime'), isNonNull: false, ), defaultValue: null, @@ -790,7 +790,7 @@ const Review = _i1.ObjectTypeDefinitionNode( directives: [], args: [], type: _i1.NamedTypeNode( - name: _i1.NameNode(value: 'Date'), + name: _i1.NameNode(value: 'DateTime'), isNonNull: false, ), ), @@ -800,7 +800,7 @@ const Review = _i1.ObjectTypeDefinitionNode( args: [], type: _i1.ListTypeNode( type: _i1.NamedTypeNode( - name: _i1.NameNode(value: 'Date'), + name: _i1.NameNode(value: 'DateTime'), isNonNull: true, ), isNonNull: true, @@ -856,13 +856,22 @@ const ReviewInput = _i1.InputObjectTypeDefinitionNode( directives: [], type: _i1.ListTypeNode( type: _i1.NamedTypeNode( - name: _i1.NameNode(value: 'Date'), + name: _i1.NameNode(value: 'DateTime'), isNonNull: false, ), isNonNull: false, ), defaultValue: null, ), + _i1.InputValueDefinitionNode( + name: _i1.NameNode(value: 'createdDay'), + directives: [], + type: _i1.NamedTypeNode( + name: _i1.NameNode(value: 'Date'), + isNonNull: true, + ), + defaultValue: null, + ), ], ); const CustomFieldInput = _i1.InputObjectTypeDefinitionNode( @@ -1133,6 +1142,10 @@ const SearchResult = _i1.UnionTypeDefinitionNode( ), ], ); +const DateTime = _i1.ScalarTypeDefinitionNode( + name: _i1.NameNode(value: 'DateTime'), + directives: [], +); const Date = _i1.ScalarTypeDefinitionNode( name: _i1.NameNode(value: 'Date'), directives: [], @@ -1174,6 +1187,7 @@ const document = _i1.DocumentNode(definitions: [ PostLikesInput, PostFavoritesInput, SearchResult, + DateTime, Date, ISODate, CustomField, diff --git a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.dart b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.dart index a4d268dc..2065dcd6 100644 --- a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.dart +++ b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.dart @@ -5,11 +5,12 @@ import 'package:built_collection/built_collection.dart'; import 'package:built_value/built_value.dart'; import 'package:built_value/serializer.dart'; -import 'package:end_to_end_test_tristate/custom_field.dart' as _i2; +import 'package:end_to_end_test_tristate/custom_date.dart' as _i2; +import 'package:end_to_end_test_tristate/custom_field.dart' as _i3; import 'package:end_to_end_test_tristate/graphql/__generated__/serializers.gql.dart' - as _i3; -import 'package:gql_code_builder/src/serializers/default_scalar_serializer.dart' as _i4; +import 'package:gql_code_builder/src/serializers/default_scalar_serializer.dart' + as _i5; import 'package:gql_tristate_value/gql_tristate_value.dart' as _i1; part 'schema.schema.gql.g.dart'; @@ -64,13 +65,14 @@ abstract class GReviewInput _i1.Value get commentary; _i1.Value get favorite_color; _i1.Value> get seenOn; - Map toJson() => (_i3.serializers.serializeWith( + _i2.CustomDate get createdDay; + Map toJson() => (_i4.serializers.serializeWith( GReviewInput.serializer, this, ) as Map); static GReviewInput? fromJson(Map json) => - _i3.serializers.deserializeWith( + _i4.serializers.deserializeWith( GReviewInput.serializer, json, ); @@ -111,6 +113,9 @@ final class GReviewInputSerializer extends StructuredSerializer { result.add(serializers.serialize(_$value, specifiedType: const FullType(BuiltList, [FullType(DateTime)]))); } + result.add('createdDay'); + result.add(serializers.serialize(object.createdDay, + specifiedType: const FullType(_i2.CustomDate))); return result; } @@ -148,6 +153,11 @@ final class GReviewInputSerializer extends StructuredSerializer { as BuiltList; builder.seenOn = _i1.PresentValue(_$fieldValue); break; + case 'createdDay': + var _$fieldValue = serializers.deserialize(value, + specifiedType: const FullType(_i2.CustomDate)) as _i2.CustomDate; + builder.createdDay = _$fieldValue; + break; } } return builder.build(); @@ -166,14 +176,14 @@ abstract class GCustomFieldInput b..customField = const _i1.AbsentValue(); String get id; - _i1.Value<_i2.CustomField> get customField; - Map toJson() => (_i3.serializers.serializeWith( + _i1.Value<_i3.CustomField> get customField; + Map toJson() => (_i4.serializers.serializeWith( GCustomFieldInput.serializer, this, ) as Map); static GCustomFieldInput? fromJson(Map json) => - _i3.serializers.deserializeWith( + _i4.serializers.deserializeWith( GCustomFieldInput.serializer, json, ); @@ -202,7 +212,7 @@ final class GCustomFieldInputSerializer if (_$customFieldvalue case _i1.PresentValue(value: final _$value)) { result.add('customField'); result.add(serializers.serialize(_$value, - specifiedType: const FullType(_i2.CustomField))); + specifiedType: const FullType(_i3.CustomField))); } return result; } @@ -226,8 +236,8 @@ final class GCustomFieldInputSerializer break; case 'customField': var _$fieldValue = serializers.deserialize(value, - specifiedType: const FullType(_i2.CustomField)) - as _i2.CustomField; + specifiedType: const FullType(_i3.CustomField)) + as _i3.CustomField; builder.customField = _i1.PresentValue(_$fieldValue); break; } @@ -245,13 +255,13 @@ abstract class GColorInput implements Built { int get red; int get green; int get blue; - Map toJson() => (_i3.serializers.serializeWith( + Map toJson() => (_i4.serializers.serializeWith( GColorInput.serializer, this, ) as Map); static GColorInput? fromJson(Map json) => - _i3.serializers.deserializeWith( + _i4.serializers.deserializeWith( GColorInput.serializer, json, ); @@ -324,13 +334,13 @@ abstract class GPostLikesInput _$GPostLikesInput; String get id; - Map toJson() => (_i3.serializers.serializeWith( + Map toJson() => (_i4.serializers.serializeWith( GPostLikesInput.serializer, this, ) as Map); static GPostLikesInput? fromJson(Map json) => - _i3.serializers.deserializeWith( + _i4.serializers.deserializeWith( GPostLikesInput.serializer, json, ); @@ -390,13 +400,13 @@ abstract class GPostFavoritesInput _$GPostFavoritesInput; String get id; - Map toJson() => (_i3.serializers.serializeWith( + Map toJson() => (_i4.serializers.serializeWith( GPostFavoritesInput.serializer, this, ) as Map); static GPostFavoritesInput? fromJson(Map json) => - _i3.serializers.deserializeWith( + _i4.serializers.deserializeWith( GPostFavoritesInput.serializer, json, ); @@ -459,7 +469,7 @@ abstract class GISODate implements Built { String get value; @BuiltValueSerializer(custom: true) static Serializer get serializer => - _i4.DefaultScalarSerializer( + _i5.DefaultScalarSerializer( (Object serialized) => GISODate((serialized as String?))); } @@ -471,7 +481,7 @@ abstract class GJson implements Built { String get value; @BuiltValueSerializer(custom: true) - static Serializer get serializer => _i4.DefaultScalarSerializer( + static Serializer get serializer => _i5.DefaultScalarSerializer( (Object serialized) => GJson((serialized as String?))); } diff --git a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.g.dart b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.g.dart index 8368dc16..b7979beb 100644 --- a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.g.dart +++ b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/schema.schema.gql.g.dart @@ -104,6 +104,8 @@ class _$GReviewInput extends GReviewInput { final _i1.Value favorite_color; @override final _i1.Value> seenOn; + @override + final _i2.CustomDate createdDay; factory _$GReviewInput([void Function(GReviewInputBuilder)? updates]) => (new GReviewInputBuilder()..update(updates))._build(); @@ -112,7 +114,8 @@ class _$GReviewInput extends GReviewInput { {required this.stars, required this.commentary, required this.favorite_color, - required this.seenOn}) + required this.seenOn, + required this.createdDay}) : super._() { BuiltValueNullFieldError.checkNotNull(stars, r'GReviewInput', 'stars'); BuiltValueNullFieldError.checkNotNull( @@ -120,6 +123,8 @@ class _$GReviewInput extends GReviewInput { BuiltValueNullFieldError.checkNotNull( favorite_color, r'GReviewInput', 'favorite_color'); BuiltValueNullFieldError.checkNotNull(seenOn, r'GReviewInput', 'seenOn'); + BuiltValueNullFieldError.checkNotNull( + createdDay, r'GReviewInput', 'createdDay'); } @override @@ -136,7 +141,8 @@ class _$GReviewInput extends GReviewInput { stars == other.stars && commentary == other.commentary && favorite_color == other.favorite_color && - seenOn == other.seenOn; + seenOn == other.seenOn && + createdDay == other.createdDay; } @override @@ -146,6 +152,7 @@ class _$GReviewInput extends GReviewInput { _$hash = $jc(_$hash, commentary.hashCode); _$hash = $jc(_$hash, favorite_color.hashCode); _$hash = $jc(_$hash, seenOn.hashCode); + _$hash = $jc(_$hash, createdDay.hashCode); _$hash = $jf(_$hash); return _$hash; } @@ -156,7 +163,8 @@ class _$GReviewInput extends GReviewInput { ..add('stars', stars) ..add('commentary', commentary) ..add('favorite_color', favorite_color) - ..add('seenOn', seenOn)) + ..add('seenOn', seenOn) + ..add('createdDay', createdDay)) .toString(); } } @@ -184,6 +192,10 @@ class GReviewInputBuilder set seenOn(_i1.Value>? seenOn) => _$this._seenOn = seenOn; + _i2.CustomDate? _createdDay; + _i2.CustomDate? get createdDay => _$this._createdDay; + set createdDay(_i2.CustomDate? createdDay) => _$this._createdDay = createdDay; + GReviewInputBuilder() { GReviewInput._initializeBuilder(this); } @@ -195,6 +207,7 @@ class GReviewInputBuilder _commentary = $v.commentary; _favorite_color = $v.favorite_color; _seenOn = $v.seenOn; + _createdDay = $v.createdDay; _$v = null; } return this; @@ -224,7 +237,9 @@ class GReviewInputBuilder favorite_color: BuiltValueNullFieldError.checkNotNull( favorite_color, r'GReviewInput', 'favorite_color'), seenOn: BuiltValueNullFieldError.checkNotNull( - seenOn, r'GReviewInput', 'seenOn')); + seenOn, r'GReviewInput', 'seenOn'), + createdDay: BuiltValueNullFieldError.checkNotNull( + createdDay, r'GReviewInput', 'createdDay')); replace(_$result); return _$result; } @@ -234,7 +249,7 @@ class _$GCustomFieldInput extends GCustomFieldInput { @override final String id; @override - final _i1.Value<_i2.CustomField> customField; + final _i1.Value<_i3.CustomField> customField; factory _$GCustomFieldInput( [void Function(GCustomFieldInputBuilder)? updates]) => @@ -289,9 +304,9 @@ class GCustomFieldInputBuilder String? get id => _$this._id; set id(String? id) => _$this._id = id; - _i1.Value<_i2.CustomField>? _customField; - _i1.Value<_i2.CustomField>? get customField => _$this._customField; - set customField(_i1.Value<_i2.CustomField>? customField) => + _i1.Value<_i3.CustomField>? _customField; + _i1.Value<_i3.CustomField>? get customField => _$this._customField; + set customField(_i1.Value<_i3.CustomField>? customField) => _$this._customField = customField; GCustomFieldInputBuilder() { diff --git a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/serializers.gql.dart b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/serializers.gql.dart index 15090df8..227f8ae5 100644 --- a/codegen/end_to_end_test_tristate/lib/graphql/__generated__/serializers.gql.dart +++ b/codegen/end_to_end_test_tristate/lib/graphql/__generated__/serializers.gql.dart @@ -23,6 +23,9 @@ import 'package:end_to_end_test_tristate/aliases/__generated__/aliased_hero.req. show GAliasedHero; import 'package:end_to_end_test_tristate/aliases/__generated__/aliased_hero.var.gql.dart' show GAliasedHeroVars; +import 'package:end_to_end_test_tristate/custom_date.dart'; +import 'package:end_to_end_test_tristate/custom_date_serializer.dart' + show CustomDateSerializer; import 'package:end_to_end_test_tristate/custom_field.dart'; import 'package:end_to_end_test_tristate/custom_field_serializer.dart' show CustomFieldSerializer; @@ -154,6 +157,7 @@ final SerializersBuilder _serializersBuilder = _$serializers.toBuilder() ..add(OperationSerializer()) ..add(DateSerializer()) ..add(CustomFieldSerializer()) + ..add(CustomDateSerializer()) ..add(GHeroForEpisodeData_hero.serializer) ..add(GheroFieldsFragmentData.serializer) ..add(GheroFieldsFragmentData__asHuman_friends.serializer) diff --git a/codegen/end_to_end_test_tristate/lib/graphql/schema.graphql b/codegen/end_to_end_test_tristate/lib/graphql/schema.graphql index 671605a7..67cc84f5 100644 --- a/codegen/end_to_end_test_tristate/lib/graphql/schema.graphql +++ b/codegen/end_to_end_test_tristate/lib/graphql/schema.graphql @@ -8,7 +8,6 @@ schema { directive @extends on OBJECT | INTERFACE - # The query type, represents all of the entry points into our object graph type Query { hero(episode: Episode): Character @@ -23,7 +22,11 @@ type Query { # The mutation type, represents all updates we can make to our data type Mutation { - createReview(episode: Episode, review: ReviewInput!, createdAt: Date): Review + createReview( + episode: Episode + review: ReviewInput! + createdAt: DateTime + ): Review createCustomField(input: CustomFieldInput!): CustomField } @@ -140,9 +143,9 @@ type Review { # Comment about the movie commentary: String # Date review created - createdAt: Date + createdAt: DateTime # Date episode seen - seenOn: [Date!]! + seenOn: [DateTime!]! # A custom field custom: [CustomField!]! } @@ -156,7 +159,9 @@ input ReviewInput { # Favorite color, optional favorite_color: ColorInput # Date viewed - seenOn: [Date] + seenOn: [DateTime] + # Use of a custom scalar. + createdDay: Date! } input CustomFieldInput { @@ -206,7 +211,9 @@ input PostFavoritesInput { union SearchResult = Human | Droid | Starship +scalar DateTime scalar Date + scalar ISODate scalar CustomField diff --git a/codegen/end_to_end_test_tristate/lib/scalars/__generated__/review_with_date.ast.gql.dart b/codegen/end_to_end_test_tristate/lib/scalars/__generated__/review_with_date.ast.gql.dart index d3b70e19..f19bfdf2 100644 --- a/codegen/end_to_end_test_tristate/lib/scalars/__generated__/review_with_date.ast.gql.dart +++ b/codegen/end_to_end_test_tristate/lib/scalars/__generated__/review_with_date.ast.gql.dart @@ -29,7 +29,7 @@ const ReviewWithDate = _i1.OperationDefinitionNode( _i1.VariableDefinitionNode( variable: _i1.VariableNode(name: _i1.NameNode(value: 'createdAt')), type: _i1.NamedTypeNode( - name: _i1.NameNode(value: 'Date'), + name: _i1.NameNode(value: 'DateTime'), isNonNull: false, ), defaultValue: _i1.DefaultValueNode(value: null), diff --git a/codegen/end_to_end_test_tristate/lib/scalars/review_with_date.graphql b/codegen/end_to_end_test_tristate/lib/scalars/review_with_date.graphql index 5cad0ddd..2fd250e1 100644 --- a/codegen/end_to_end_test_tristate/lib/scalars/review_with_date.graphql +++ b/codegen/end_to_end_test_tristate/lib/scalars/review_with_date.graphql @@ -1,7 +1,7 @@ mutation ReviewWithDate( $episode: Episode $review: ReviewInput! - $createdAt: Date + $createdAt: DateTime ) { createReview(episode: $episode, review: $review, createdAt: $createdAt) { episode diff --git a/codegen/gql_code_builder/lib/src/tristate_optionals.dart b/codegen/gql_code_builder/lib/src/tristate_optionals.dart index c4d01d99..9abdb3f3 100644 --- a/codegen/gql_code_builder/lib/src/tristate_optionals.dart +++ b/codegen/gql_code_builder/lib/src/tristate_optionals.dart @@ -181,13 +181,10 @@ String _generateFieldDeserializers( type = (type as TypeReference).types.first; } final fullType = _generateFullType(type as TypeReference, allocator); - - /// remove the leading `G` from the type name - /// TODO refactor this - final originalSymbolName = type.symbol.substring(1); - - final typeDefNode = - getTypeDefinitionNode(schemaSource.document, originalSymbolName); + final typeDefNode = getTypeDefinitionNode( + schemaSource.document, + _originalSymbolName(typeOverrides, type), + ); //TODO this feels flaky, find a better way final isBuilder = type.url != null && @@ -198,7 +195,6 @@ String _generateFieldDeserializers( const fieldNameVariableName = "_\$fieldValue"; final fieldNameExpr = CodeExpression(Code(fieldNameVariableName)); - var base = """ case '${_getWireName(field)}': var ${fieldNameVariableName} = serializers.deserialize( @@ -220,6 +216,26 @@ break; """; }).join(); +String _originalSymbolName( + Map typeOverrides, TypeReference type) { + String? originalSymbolName; + + /// If symbol exists in [typeOverrides] then the original symbol from Schema file is the key of [typeOverrides]. + /// And [type.symbol] is the type of the Dart class that handles Schema's scalar. + for (final entry in typeOverrides.entries) { + if (entry.value.symbol == type.symbol) { + originalSymbolName = entry.key; + break; + } + } + + /// Removes the leading `G` from the type name. + /// It still can not work if you original type's symbol starts with the 'G' letter. + originalSymbolName ??= + type.symbol[0] == "G" ? type.symbol.substring(1) : type.symbol; + return originalSymbolName; +} + String _getWireName(Method m) { final wireNameExpr = m.annotations .map((annotation) {