Skip to content

Commit

Permalink
fix(amplify-codegen): swift-codegen add has-many associatedWithFields…
Browse files Browse the repository at this point in the history
… for CPK use case
  • Loading branch information
lawmicha committed Feb 2, 2023
1 parent cf7f167 commit 24e4aed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension Post {
model.fields(
.field(post.id, is: .required, ofType: .string),
.field(post.title, is: .required, ofType: .string),
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedWith: Comment.keys.postCommentsId),
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedFields: [Comment.keys.postCommentsId, Comment.keys.postCommentsTitle]),
.field(post.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
.field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,13 @@ export class AppSyncSwiftVisitor<
// connected field
if (connectionInfo) {
if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY) {
return `.hasMany(${name}, is: ${isRequired}, ofType: ${typeName}, associatedWith: ${this.getModelName(
connectionInfo.connectedModel,
)}.keys.${this.getFieldName(connectionInfo.associatedWith)})`;
let connectedModelName = this.getModelName(connectionInfo.connectedModel);
const associatedWithAttrStr = this.isCustomPKEnabled()
? `associatedFields: [${connectionInfo.associatedWithFields
.map(target => `${connectedModelName}.keys.${this.getFieldName(target)}`)
.join(', ')}]`
: `associatedWith: ${connectedModelName}.keys.${this.getFieldName(connectionInfo.associatedWith)}`;
return `.hasMany(${name}, is: ${isRequired}, ofType: ${typeName}, ${associatedWithAttrStr})`;
}
if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
const targetNameAttrStr = this.isCustomPKEnabled()
Expand Down

0 comments on commit 24e4aed

Please sign in to comment.