Skip to content

Commit 7a8321b

Browse files
authored
Merge pull request #326 from aws-amplify/fix-hasone-belongsto
fix: set associatedWith to correct field with hasOne/hasMany and belongsTo
2 parents f0742a5 + 4f41416 commit 7a8321b

14 files changed

+1265
-140
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,10 @@ describe('AppSyncModelVisitor', () => {
853853
expect(visitor.models.Human.fields[2].directives[0].arguments.fields[0]).toEqual('governmentID');
854854
expect(visitor.models.Human.fields[2].directives[0].arguments.indexName).toEqual('byHuman');
855855
expect(visitor.models.PetFriend).toBeDefined();
856-
expect(visitor.models.PetFriend.fields.length).toEqual(7);
857-
expect(visitor.models.PetFriend.fields[4].directives[0].name).toEqual('belongsTo');
858-
expect(visitor.models.PetFriend.fields[4].directives[0].arguments.fields.length).toEqual(1);
859-
expect(visitor.models.PetFriend.fields[4].directives[0].arguments.fields[0]).toEqual('animalID');
856+
expect(visitor.models.PetFriend.fields.length).toEqual(5);
857+
expect(visitor.models.PetFriend.fields[2].directives[0].name).toEqual('belongsTo');
858+
expect(visitor.models.PetFriend.fields[2].directives[0].arguments.fields.length).toEqual(1);
859+
expect(visitor.models.PetFriend.fields[2].directives[0].arguments.fields[0]).toEqual('animalID');
860860
expect(visitor.models.Animal.fields.length).toEqual(5);
861861
expect(visitor.models.Animal.fields[2].type).toEqual('PetFriend');
862862
expect(visitor.models.Animal.fields[2].directives.length).toEqual(1);
@@ -876,7 +876,7 @@ describe('AppSyncModelVisitor', () => {
876876
expect(visitor.models.ModelA.fields[1].directives[0].arguments.indexName).toEqual('byModelA');
877877

878878
expect(visitor.models.Models).toBeDefined();
879-
expect(visitor.models.Models.fields.length).toEqual(7);
879+
expect(visitor.models.Models.fields.length).toEqual(5);
880880

881881
const modelA = visitor.models.Models.fields.find(f => f.name === 'modelA');
882882
expect(modelA).toBeDefined();

packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-dart-visitor.test.ts.snap

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ class Project extends Model {
630630
key: Project.TEAM,
631631
isRequired: false,
632632
ofModelName: (Team).toString(),
633-
associatedKey: Team.ID
633+
associatedKey: Team.PROJECT
634634
));
635635

636636
modelSchemaDefinition.addField(ModelFieldDefinition.field(
@@ -936,7 +936,7 @@ class Project2 extends Model {
936936
key: Project2.TEAM,
937937
isRequired: false,
938938
ofModelName: (Team2).toString(),
939-
associatedKey: Team2.ID
939+
associatedKey: Team2.PROJECT
940940
));
941941

942942
modelSchemaDefinition.addField(ModelFieldDefinition.field(
@@ -1536,7 +1536,7 @@ class Post extends Model {
15361536
key: Post.COMMENTS,
15371537
isRequired: false,
15381538
ofModelName: (Comment).toString(),
1539-
associatedKey: Comment.POSTCOMMENTSID
1539+
associatedKey: Comment.ID
15401540
));
15411541
});
15421542
}
@@ -1583,7 +1583,6 @@ class Comment extends Model {
15831583
static const classType = const _CommentModelType();
15841584
final String id;
15851585
final String? _content;
1586-
final String? _postCommentsId;
15871586

15881587
@override
15891588
getInstanceType() => classType;
@@ -1601,17 +1600,12 @@ class Comment extends Model {
16011600
}
16021601
}
16031602

1604-
String? get postCommentsId {
1605-
return _postCommentsId;
1606-
}
1603+
const Comment._internal({required this.id, required content}): _content = content;
16071604

1608-
const Comment._internal({required this.id, required content, postCommentsId}): _content = content, _postCommentsId = postCommentsId;
1609-
1610-
factory Comment({String? id, required String content, String? postCommentsId}) {
1605+
factory Comment({String? id, required String content}) {
16111606
return Comment._internal(
16121607
id: id == null ? UUID.getUUID() : id,
1613-
content: content,
1614-
postCommentsId: postCommentsId);
1608+
content: content);
16151609
}
16161610

16171611
bool equals(Object other) {
@@ -1623,8 +1617,7 @@ class Comment extends Model {
16231617
if (identical(other, this)) return true;
16241618
return other is Comment &&
16251619
id == other.id &&
1626-
_content == other._content &&
1627-
_postCommentsId == other._postCommentsId;
1620+
_content == other._content;
16281621
}
16291622

16301623
@override
@@ -1636,32 +1629,28 @@ class Comment extends Model {
16361629

16371630
buffer.write(\\"Comment {\\");
16381631
buffer.write(\\"id=\\" + \\"$id\\" + \\", \\");
1639-
buffer.write(\\"content=\\" + \\"$_content\\" + \\", \\");
1640-
buffer.write(\\"postCommentsId=\\" + \\"$_postCommentsId\\");
1632+
buffer.write(\\"content=\\" + \\"$_content\\");
16411633
buffer.write(\\"}\\");
16421634

16431635
return buffer.toString();
16441636
}
16451637

1646-
Comment copyWith({String? id, String? content, String? postCommentsId}) {
1638+
Comment copyWith({String? id, String? content}) {
16471639
return Comment(
16481640
id: id ?? this.id,
1649-
content: content ?? this.content,
1650-
postCommentsId: postCommentsId ?? this.postCommentsId);
1641+
content: content ?? this.content);
16511642
}
16521643

16531644
Comment.fromJson(Map<String, dynamic> json)
16541645
: id = json['id'],
1655-
_content = json['content'],
1656-
_postCommentsId = json['postCommentsId'];
1646+
_content = json['content'];
16571647

16581648
Map<String, dynamic> toJson() => {
1659-
'id': id, 'content': _content, 'postCommentsId': _postCommentsId
1649+
'id': id, 'content': _content
16601650
};
16611651

16621652
static final QueryField ID = QueryField(fieldName: \\"comment.id\\");
16631653
static final QueryField CONTENT = QueryField(fieldName: \\"content\\");
1664-
static final QueryField POSTCOMMENTSID = QueryField(fieldName: \\"postCommentsId\\");
16651654
static var schema = Model.defineSchema(define: (ModelSchemaDefinition modelSchemaDefinition) {
16661655
modelSchemaDefinition.name = \\"Comment\\";
16671656
modelSchemaDefinition.pluralName = \\"Comments\\";
@@ -1673,12 +1662,6 @@ class Comment extends Model {
16731662
isRequired: true,
16741663
ofType: ModelFieldType(ModelFieldTypeEnum.string)
16751664
));
1676-
1677-
modelSchemaDefinition.addField(ModelFieldDefinition.field(
1678-
key: Comment.POSTCOMMENTSID,
1679-
isRequired: false,
1680-
ofType: ModelFieldType(ModelFieldTypeEnum.string)
1681-
));
16821665
});
16831666
}
16841667

0 commit comments

Comments
 (0)