New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(amplify-codegen): swift - add has-many associatedFields for CPK use case #538
base: main
Are you sure you want to change the base?
Conversation
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## main #538 +/- ##
=======================================
Coverage 85.69% 85.70%
=======================================
Files 148 148
Lines 7380 7385 +5
Branches 1962 1965 +3
=======================================
+ Hits 6324 6329 +5
Misses 959 959
Partials 97 97
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
5b7f190
to
c4234fc
Compare
c4234fc
to
d8bb5d1
Compare
… for CPK use case
d8bb5d1
to
24e4aed
Compare
model.fields( | ||
.field(post.id, is: .required, ofType: .string), | ||
.field(post.title, is: .required, ofType: .string), | ||
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedFields: [Comment.keys.post]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With CPK enabled, the bidirectional use case will now only generated associatedFields
, which means the library changes must be merged and released first. Comment.keys.post
is correct since, the reference field post
on Comment can be used to resolve its targetNames, from the Comment schema
.belongsTo(comment.post, is: .optional, ofType: Post.self, targetNames: [\\"postCommentsId\\", \\"postCommentsTitle\\"]),
cf38966
to
1cbf6b1
Compare
model.fields( | ||
.field(post.postId, is: .required, ofType: .string), | ||
.field(post.title, is: .required, ofType: .string), | ||
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedFields: [Comment.keys.postId]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look accurate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened issue to track this: #539
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR for the fix #542
@@ -73,7 +309,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]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks correct
Table of contents
data-dev-preview
fix: CPK uni-directional has-many lazy list load amplify-swift#2730v1
- fix(v1): CPK has-many has-one associatedFields amplify-swift#2734main
- fix: CPK has-many has-one associatedFields amplify-swift#2735Tagged release
tagged-release-without-e2e-tests/cpk-has-many
Description of changes
This PR is a bug fix for CPK feature to add the foreign key fields of a has-many relationship as the "associatedFields" of the field. For uni-directional has-many relationships, with the parent having a CPK use case:
associatedWith
field which points to the primary key of the Post, ie.The issue here is that it's missing the information about the entire CPK of Post, the library only knows that the comment is associated with the post by the field
Comment4.keys.post4CommentsPostId
on Comment.By replacing
associatedWith
withassociatedFields
hasMany schema information to include all the foreign key composite key, we support CPK uni-directional has-many use cases. The final generated schema information will appear like this:This is useful for querying for Comments by using
associatedFields
, done by the library's lazy loading list functionality. For more details on the library issue, see aws-amplify/amplify-swift#2730Since this codegen change is behind the CPK feature flag and generates "associatedFields" on the hasMany, this means the library needs to define and release first, followed by the codegen changes. Since CPK feature is available for both
v1
andmain
(v2) of Amplify Swift library. We need to introduce the changes tov1
,main
, and not justdata-dev-preview
(lazy reference + custom selecion set feature branch) of the library.Issue #, if available
Description of how you validated changes
Checklist
yarn test
passesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.