Skip to content

Commit 8fc88dd

Browse files
authoredMay 23, 2024
Merge pull request #81 from narumincho/78-unontype-add-name-parameter
union type required typename__
2 parents f959b1c + ffbf18a commit 8fc88dd

File tree

7 files changed

+100
-28
lines changed

7 files changed

+100
-28
lines changed
 

‎.github/workflows/pull_request.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,32 @@ jobs:
99
runs-on: ubuntu-22.04
1010
steps:
1111
- name: checkout repository
12-
uses: actions/checkout@v3
12+
uses: actions/checkout@v4
1313

1414
- uses: dart-lang/setup-dart@v1
1515
- uses: denoland/setup-deno@v1
16+
1617
- run: dart test
1718
working-directory: packages/narumincho_json
19+
- run: dart pub publish --dry-run
20+
working-directory: packages/narumincho_json
21+
1822
- run: dart test
1923
working-directory: packages/narumincho_util
24+
- run: dart pub publish --dry-run
25+
working-directory: packages/narumincho_util
26+
2027
- run: dart test
2128
working-directory: packages/simple_dart_code_gen
29+
- run: dart pub publish --dry-run
30+
working-directory: packages/simple_dart_code_gen
31+
2232
- run: deno run -A ./packages/simple_graphql_client_gen_test_server/main.ts &
2333
- run: dart test
2434
working-directory: packages/simple_graphql_client_gen
2535
- run: pkill -f 'deno run -A ./packages/simple_graphql_client_gen_test_server/main.ts'
36+
- run: dart pub publish --dry-run
37+
working-directory: packages/simple_graphql_client_gen
38+
2639
- name: Analyze Dart
27-
uses: ValentinVignal/action-dart-analyze@v0.16
40+
uses: ValentinVignal/action-dart-analyze@v0.17

‎packages/simple_graphql_client_gen/lib/query_gen.dart

+5-8
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,11 @@ ClassDeclaration _graphQLUnionTypeQueryClass(
230230
name: type.name,
231231
documentationComments: type.documentationComments,
232232
fields: IList([
233-
Field(
234-
name: 'name',
235-
documentationComments:
236-
'この構造の型につける型の名前. ※同じ名前で違う構造にするとエラーになるので注意! ※Nameという名前の型が定義されていた場合は...想定外',
233+
const Field(
234+
name: 'typeName__',
235+
documentationComments: 'この構造の型につける型の名前. ※同じ名前で違う構造にするとエラーになるので注意!',
237236
type: wellknown_type.String,
238-
parameterPattern: ParameterPatternNamedWithDefault(
239-
ExprStringLiteral(IList([StringLiteralItemNormal(type.name)])),
240-
),
237+
parameterPattern: ParameterPatternPositional(),
241238
),
242239
...union.possibleTypes.map(
243240
(possibleType) => Field(
@@ -308,7 +305,7 @@ ClassDeclaration _graphQLUnionTypeQueryClass(
308305
useResultAnnotation: true,
309306
parameters: IListConst([]),
310307
returnType: wellknown_type.String,
311-
statements: IListConst([StatementReturn(ExprVariable('name'))]),
308+
statements: IListConst([StatementReturn(ExprVariable('typeName__'))]),
312309
),
313310
Method(
314311
methodType: MethodType.override,

‎packages/simple_graphql_client_gen/pubspec.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ packages:
4747
source: hosted
4848
version: "2.1.1"
4949
collection:
50-
dependency: transitive
50+
dependency: "direct main"
5151
description:
5252
name: collection
5353
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a

‎packages/simple_graphql_client_gen/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version: "0.9.2"
55
environment:
66
sdk: ">=3.2.0 <4.0.0"
77
dependencies:
8+
collection: ^1.17.0
89
fast_immutable_collections: ">=10.0.0 <11.0.0"
910
http: ">=0.13.6 <2.0.0"
1011
meta: ">=1.9.1 <2.0.0"

‎packages/simple_graphql_client_gen/test/codegen_api.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const IMap<String, GraphQLRootObject> _apiMap = IMapConst({
4949
union: query.Query_union(
5050
id: Variable('id'),
5151
query.AccountOrNote(
52+
'AccountOrNote',
5253
account: query.Account(
5354
'AccountInUnionA',
5455
id: query.Account_id(),
@@ -58,7 +59,7 @@ const IMap<String, GraphQLRootObject> _apiMap = IMapConst({
5859
'Note',
5960
description: query.Note_description(),
6061
subNotes: query.Note_subNotes(
61-
query.Note('Note', description: query.Note_description()),
62+
query.Note('Note2', description: query.Note_description()),
6263
),
6364
),
6465
),

‎packages/simple_graphql_client_gen/test/graphql/api.dart

+64-4
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,66 @@ final class AccountInUnionA implements AccountOrNote {
562562
}
563563
}
564564

565+
/// ノート
566+
@immutable
567+
final class Note2 {
568+
/// ノート
569+
const Note2({
570+
required this.description,
571+
});
572+
573+
/// 説明文
574+
final type.ID description;
575+
576+
/// `Note2` を複製する
577+
@useResult
578+
Note2 copyWith({
579+
type.ID? description,
580+
}) {
581+
return Note2(description: (description ?? this.description));
582+
}
583+
584+
/// `Note2` のフィールドを変更したものを新しく返す
585+
@useResult
586+
Note2 updateFields({
587+
type.ID Function(type.ID prevDescription)? description,
588+
}) {
589+
return Note2(
590+
description: ((description == null)
591+
? this.description
592+
: description(this.description)));
593+
}
594+
595+
@override
596+
@useResult
597+
int get hashCode {
598+
return description.hashCode;
599+
}
600+
601+
@override
602+
@useResult
603+
bool operator ==(
604+
Object other,
605+
) {
606+
return ((other is Note2) && (description == other.description));
607+
}
608+
609+
@override
610+
@useResult
611+
String toString() {
612+
return 'Note2(description: ${description}, )';
613+
}
614+
615+
/// JsonValue から Note2を生成する. 失敗した場合はエラーが発生する
616+
static Note2 fromJsonValue(
617+
narumincho_json.JsonValue value,
618+
) {
619+
return Note2(
620+
description:
621+
type.ID.fromJsonValue(value.getObjectValueOrThrow('description')));
622+
}
623+
}
624+
565625
/// ノート
566626
@immutable
567627
final class Note implements AccountOrNote {
@@ -575,13 +635,13 @@ final class Note implements AccountOrNote {
575635
final type.ID description;
576636

577637
/// 子ノート
578-
final IList<Note> subNotes;
638+
final IList<Note2> subNotes;
579639

580640
/// `Note` を複製する
581641
@useResult
582642
Note copyWith({
583643
type.ID? description,
584-
IList<Note>? subNotes,
644+
IList<Note2>? subNotes,
585645
}) {
586646
return Note(
587647
description: (description ?? this.description),
@@ -593,7 +653,7 @@ final class Note implements AccountOrNote {
593653
@useResult
594654
Note updateFields({
595655
type.ID Function(type.ID prevDescription)? description,
596-
IList<Note> Function(IList<Note> prevSubNotes)? subNotes,
656+
IList<Note2> Function(IList<Note2> prevSubNotes)? subNotes,
597657
}) {
598658
return Note(
599659
description: ((description == null)
@@ -635,7 +695,7 @@ final class Note implements AccountOrNote {
635695
description:
636696
type.ID.fromJsonValue(value.getObjectValueOrThrow('description')),
637697
subNotes: value.getObjectValueOrThrow('subNotes').asArrayOrThrow((v) {
638-
return Note.fromJsonValue(v);
698+
return Note2.fromJsonValue(v);
639699
}),
640700
);
641701
}

‎packages/simple_graphql_client_gen/test/graphql/query.dart

+12-12
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,14 @@ final class Account_name implements Account_Field {
670670

671671
@immutable
672672
final class AccountOrNote implements query_string.GraphQLObjectType {
673-
const AccountOrNote({
674-
this.name = 'AccountOrNote',
673+
const AccountOrNote(
674+
this.typeName__, {
675675
required this.account,
676676
required this.note,
677677
});
678678

679-
/// この構造の型につける型の名前. ※同じ名前で違う構造にするとエラーになるので注意! ※Nameという名前の型が定義されていた場合は...想定外
680-
final String name;
679+
/// この構造の型につける型の名前. ※同じ名前で違う構造にするとエラーになるので注意!
680+
final String typeName__;
681681

682682
/// よくあるアカウントの型
683683
final Account account;
@@ -688,12 +688,12 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
688688
/// `AccountOrNote` を複製する
689689
@useResult
690690
AccountOrNote copyWith({
691-
String? name,
691+
String? typeName__,
692692
Account? account,
693693
Note? note,
694694
}) {
695695
return AccountOrNote(
696-
name: (name ?? this.name),
696+
(typeName__ ?? this.typeName__),
697697
account: (account ?? this.account),
698698
note: (note ?? this.note),
699699
);
@@ -702,12 +702,12 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
702702
/// `AccountOrNote` のフィールドを変更したものを新しく返す
703703
@useResult
704704
AccountOrNote updateFields({
705-
String Function(String prevName)? name,
705+
String Function(String prevTypeName__)? typeName__,
706706
Account Function(Account prevAccount)? account,
707707
Note Function(Note prevNote)? note,
708708
}) {
709709
return AccountOrNote(
710-
name: ((name == null) ? this.name : name(this.name)),
710+
((typeName__ == null) ? this.typeName__ : typeName__(this.typeName__)),
711711
account: ((account == null) ? this.account : account(this.account)),
712712
note: ((note == null) ? this.note : note(this.note)),
713713
);
@@ -717,7 +717,7 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
717717
@useResult
718718
int get hashCode {
719719
return Object.hash(
720-
name,
720+
typeName__,
721721
account,
722722
note,
723723
);
@@ -728,15 +728,15 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
728728
bool operator ==(
729729
Object other,
730730
) {
731-
return ((((other is AccountOrNote) && (name == other.name)) &&
731+
return ((((other is AccountOrNote) && (typeName__ == other.typeName__)) &&
732732
(account == other.account)) &&
733733
(note == other.note));
734734
}
735735

736736
@override
737737
@useResult
738738
String toString() {
739-
return 'AccountOrNote(name: ${name}, account: ${account}, note: ${note}, )';
739+
return 'AccountOrNote(${typeName__}, account: ${account}, note: ${note}, )';
740740
}
741741

742742
@override
@@ -766,7 +766,7 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
766766
@override
767767
@useResult
768768
String getTypeName() {
769-
return name;
769+
return typeName__;
770770
}
771771

772772
@override

0 commit comments

Comments
 (0)
Failed to load comments.