Skip to content

Commit d8bb5d1

Browse files
committed
fix(amplify-codegen): swift-codegen add has-many associatedWithFields for CPK use case
1 parent cf7f167 commit d8bb5d1

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ extension Post {
7373
model.fields(
7474
.field(post.id, is: .required, ofType: .string),
7575
.field(post.title, is: .required, ofType: .string),
76-
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedWith: Comment.keys.postCommentsId),
76+
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedWithFields: [Comment.keys.postCommentsId, Comment.keys.postCommentsTitle]),
7777
.field(post.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
7878
.field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
7979
)

packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,13 @@ export class AppSyncSwiftVisitor<
615615
// connected field
616616
if (connectionInfo) {
617617
if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY) {
618-
return `.hasMany(${name}, is: ${isRequired}, ofType: ${typeName}, associatedWith: ${this.getModelName(
619-
connectionInfo.connectedModel,
620-
)}.keys.${this.getFieldName(connectionInfo.associatedWith)})`;
618+
let connectedModelName = this.getModelName(connectionInfo.connectedModel);
619+
const associatedWithAttrStr = this.isCustomPKEnabled()
620+
? `associatedWithFields: [${connectionInfo.associatedWithFields
621+
.map(target => `${connectedModelName}.keys.${this.getFieldName(target)}`)
622+
.join(', ')}]`
623+
: `associatedWith: ${connectedModelName}.keys.${this.getFieldName(connectionInfo.associatedWith)}`;
624+
return `.hasMany(${name}, is: ${isRequired}, ofType: ${typeName}, ${associatedWithAttrStr})`;
621625
}
622626
if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
623627
const targetNameAttrStr = this.isCustomPKEnabled()

0 commit comments

Comments
 (0)