Skip to content

Commit

Permalink
Merge pull request #81 from narumincho/78-unontype-add-name-parameter
Browse files Browse the repository at this point in the history
union type required typename__
  • Loading branch information
narumincho authored May 23, 2024
2 parents f959b1c + ffbf18a commit 8fc88dd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 28 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,32 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- uses: dart-lang/setup-dart@v1
- uses: denoland/setup-deno@v1

- run: dart test
working-directory: packages/narumincho_json
- run: dart pub publish --dry-run
working-directory: packages/narumincho_json

- run: dart test
working-directory: packages/narumincho_util
- run: dart pub publish --dry-run
working-directory: packages/narumincho_util

- run: dart test
working-directory: packages/simple_dart_code_gen
- run: dart pub publish --dry-run
working-directory: packages/simple_dart_code_gen

- run: deno run -A ./packages/simple_graphql_client_gen_test_server/main.ts &
- run: dart test
working-directory: packages/simple_graphql_client_gen
- run: pkill -f 'deno run -A ./packages/simple_graphql_client_gen_test_server/main.ts'
- run: dart pub publish --dry-run
working-directory: packages/simple_graphql_client_gen

- name: Analyze Dart
uses: ValentinVignal/action-dart-analyze@v0.16
uses: ValentinVignal/action-dart-analyze@v0.17
13 changes: 5 additions & 8 deletions packages/simple_graphql_client_gen/lib/query_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,11 @@ ClassDeclaration _graphQLUnionTypeQueryClass(
name: type.name,
documentationComments: type.documentationComments,
fields: IList([
Field(
name: 'name',
documentationComments:
'この構造の型につける型の名前. ※同じ名前で違う構造にするとエラーになるので注意! ※Nameという名前の型が定義されていた場合は...想定外',
const Field(
name: 'typeName__',
documentationComments: 'この構造の型につける型の名前. ※同じ名前で違う構造にするとエラーになるので注意!',
type: wellknown_type.String,
parameterPattern: ParameterPatternNamedWithDefault(
ExprStringLiteral(IList([StringLiteralItemNormal(type.name)])),
),
parameterPattern: ParameterPatternPositional(),
),
...union.possibleTypes.map(
(possibleType) => Field(
Expand Down Expand Up @@ -308,7 +305,7 @@ ClassDeclaration _graphQLUnionTypeQueryClass(
useResultAnnotation: true,
parameters: IListConst([]),
returnType: wellknown_type.String,
statements: IListConst([StatementReturn(ExprVariable('name'))]),
statements: IListConst([StatementReturn(ExprVariable('typeName__'))]),
),
Method(
methodType: MethodType.override,
Expand Down
2 changes: 1 addition & 1 deletion packages/simple_graphql_client_gen/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
source: hosted
version: "2.1.1"
collection:
dependency: transitive
dependency: "direct main"
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
Expand Down
1 change: 1 addition & 0 deletions packages/simple_graphql_client_gen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version: "0.9.2"
environment:
sdk: ">=3.2.0 <4.0.0"
dependencies:
collection: ^1.17.0
fast_immutable_collections: ">=10.0.0 <11.0.0"
http: ">=0.13.6 <2.0.0"
meta: ">=1.9.1 <2.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/simple_graphql_client_gen/test/codegen_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const IMap<String, GraphQLRootObject> _apiMap = IMapConst({
union: query.Query_union(
id: Variable('id'),
query.AccountOrNote(
'AccountOrNote',
account: query.Account(
'AccountInUnionA',
id: query.Account_id(),
Expand All @@ -58,7 +59,7 @@ const IMap<String, GraphQLRootObject> _apiMap = IMapConst({
'Note',
description: query.Note_description(),
subNotes: query.Note_subNotes(
query.Note('Note', description: query.Note_description()),
query.Note('Note2', description: query.Note_description()),
),
),
),
Expand Down
68 changes: 64 additions & 4 deletions packages/simple_graphql_client_gen/test/graphql/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,66 @@ final class AccountInUnionA implements AccountOrNote {
}
}

/// ノート
@immutable
final class Note2 {
/// ノート
const Note2({
required this.description,
});

/// 説明文
final type.ID description;

/// `Note2` を複製する
@useResult
Note2 copyWith({
type.ID? description,
}) {
return Note2(description: (description ?? this.description));
}

/// `Note2` のフィールドを変更したものを新しく返す
@useResult
Note2 updateFields({
type.ID Function(type.ID prevDescription)? description,
}) {
return Note2(
description: ((description == null)
? this.description
: description(this.description)));
}

@override
@useResult
int get hashCode {
return description.hashCode;
}

@override
@useResult
bool operator ==(
Object other,
) {
return ((other is Note2) && (description == other.description));
}

@override
@useResult
String toString() {
return 'Note2(description: ${description}, )';
}

/// JsonValue から Note2を生成する. 失敗した場合はエラーが発生する
static Note2 fromJsonValue(
narumincho_json.JsonValue value,
) {
return Note2(
description:
type.ID.fromJsonValue(value.getObjectValueOrThrow('description')));
}
}

/// ノート
@immutable
final class Note implements AccountOrNote {
Expand All @@ -575,13 +635,13 @@ final class Note implements AccountOrNote {
final type.ID description;

/// 子ノート
final IList<Note> subNotes;
final IList<Note2> subNotes;

/// `Note` を複製する
@useResult
Note copyWith({
type.ID? description,
IList<Note>? subNotes,
IList<Note2>? subNotes,
}) {
return Note(
description: (description ?? this.description),
Expand All @@ -593,7 +653,7 @@ final class Note implements AccountOrNote {
@useResult
Note updateFields({
type.ID Function(type.ID prevDescription)? description,
IList<Note> Function(IList<Note> prevSubNotes)? subNotes,
IList<Note2> Function(IList<Note2> prevSubNotes)? subNotes,
}) {
return Note(
description: ((description == null)
Expand Down Expand Up @@ -635,7 +695,7 @@ final class Note implements AccountOrNote {
description:
type.ID.fromJsonValue(value.getObjectValueOrThrow('description')),
subNotes: value.getObjectValueOrThrow('subNotes').asArrayOrThrow((v) {
return Note.fromJsonValue(v);
return Note2.fromJsonValue(v);
}),
);
}
Expand Down
24 changes: 12 additions & 12 deletions packages/simple_graphql_client_gen/test/graphql/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -670,14 +670,14 @@ final class Account_name implements Account_Field {

@immutable
final class AccountOrNote implements query_string.GraphQLObjectType {
const AccountOrNote({
this.name = 'AccountOrNote',
const AccountOrNote(
this.typeName__, {
required this.account,
required this.note,
});

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

/// よくあるアカウントの型
final Account account;
Expand All @@ -688,12 +688,12 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
/// `AccountOrNote` を複製する
@useResult
AccountOrNote copyWith({
String? name,
String? typeName__,
Account? account,
Note? note,
}) {
return AccountOrNote(
name: (name ?? this.name),
(typeName__ ?? this.typeName__),
account: (account ?? this.account),
note: (note ?? this.note),
);
Expand All @@ -702,12 +702,12 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
/// `AccountOrNote` のフィールドを変更したものを新しく返す
@useResult
AccountOrNote updateFields({
String Function(String prevName)? name,
String Function(String prevTypeName__)? typeName__,
Account Function(Account prevAccount)? account,
Note Function(Note prevNote)? note,
}) {
return AccountOrNote(
name: ((name == null) ? this.name : name(this.name)),
((typeName__ == null) ? this.typeName__ : typeName__(this.typeName__)),
account: ((account == null) ? this.account : account(this.account)),
note: ((note == null) ? this.note : note(this.note)),
);
Expand All @@ -717,7 +717,7 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
@useResult
int get hashCode {
return Object.hash(
name,
typeName__,
account,
note,
);
Expand All @@ -728,15 +728,15 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
bool operator ==(
Object other,
) {
return ((((other is AccountOrNote) && (name == other.name)) &&
return ((((other is AccountOrNote) && (typeName__ == other.typeName__)) &&
(account == other.account)) &&
(note == other.note));
}

@override
@useResult
String toString() {
return 'AccountOrNote(name: ${name}, account: ${account}, note: ${note}, )';
return 'AccountOrNote(${typeName__}, account: ${account}, note: ${note}, )';
}

@override
Expand Down Expand Up @@ -766,7 +766,7 @@ final class AccountOrNote implements query_string.GraphQLObjectType {
@override
@useResult
String getTypeName() {
return name;
return typeName__;
}

@override
Expand Down

0 comments on commit 8fc88dd

Please sign in to comment.