-
Notifications
You must be signed in to change notification settings - Fork 816
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-appsync-model-plugin): pass targetName for hasOne relationships #6031
Conversation
Codecov Report
@@ Coverage Diff @@
## master #6031 +/- ##
========================================
Coverage 58.21% 58.21%
========================================
Files 440 440
Lines 20160 20161 +1
Branches 3988 3783 -205
========================================
+ Hits 11736 11737 +1
- Misses 7609 7629 +20
+ Partials 815 795 -20
Continue to review full report at Codecov.
|
@AaronZyLee Can you review? |
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.
LGTM but I am still investigating and looking out for a general solution for other SDKs. And what I am concerned for this solution is that teamID
is not optional when you create a Project
, not only in modelgen but also in CreateProjectInput
in appsync. When refering to A has one B, it means that A does not necessarily need B but B is dependent on A. If we change the teamID
to an optional field, it will throw the following error by transfomer:
InvalidDirectiveError: All fields provided to an @connection must be non-null scalar or enum fields.
The reason is that when referring to an identifier in @connection(fields:[])
the param should not be nullable.
On the other hand, I am not sure our document has a good guide on how to design a one-to-one schema. Why do we need an identifier in a HAS_ONE
relation? Do we have any scenario that using project.teamID
is better than using project.team.id
?
In the HAS_MANY
and BELONGS_TO
bi-way connection(one to many), however, it is really necessary to use an identifier. Say post has many comments, and in comment schema we add a postID
as well as a @key to define postID
as a GSI, from which the post can have a query to get all comments for post.comments
.
In conclusion, I think a better approach is that we do not use identifier, aka teamID
from the example schema, in a one to one relation. Overall, the following schema should be an expected one-to-one schema in our guide, which causes less trouble. And one thing to mention is that the following pattern is also used in the Amplify Admin UI.
type Project @model {
id: ID!
name: String
team: Team @connection
}
type Team @model {
id: ID!
name: String!
}
@AaronZyLee Thanks for reviewing, will loop back here to continue the conversation regarding your comment. Can we merge this in first? I believe your approval doesn't have write access yet. @attilah / @ammarkarachi or someone with write access can you also approve to merge this in? |
I also noticed this behavior and believe a non optional team ID is correct. A team can exist without a project, but a project cannot exist without a team. you should never have orphaned projects, and always associated with a team. If you want to have a orphaned project, you may as the developer create a dummy placeholder team to associate your orphaned projects.
I believe we need to support both explicit fields using the key directive and fields parameter in connections and implicit fields using the older connection directive (without the |
LGTM! |
This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs. Looking for a help forum? We recommend joining the Amplify Community Discord server |
Issue #, if available:
aws-amplify/amplify-swift#920
Description of changes:
This adds the targetName for hasOne relationships, closely mirroring the same logic applied for belongsTo.
Testing done
schema
Generated swift schema class without this change:
Generated schema with this change:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.