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
feat(graphql-connection-transformer): allow null key-based connections #5153
feat(graphql-connection-transformer): allow null key-based connections #5153
Conversation
Codecov Report
@@ Coverage Diff @@
## master aws-amplify/amplify-cli#5153 +/- ##
=======================================
Coverage 57.30% 57.30%
=======================================
Files 474 474
Lines 21669 21669
Branches 4306 4306
=======================================
Hits 12418 12418
Misses 8370 8370
Partials 881 881
Continue to review full report at Codecov.
|
Is there anything blocking review of this pull request? |
It'd be lovely to see this working. |
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.
@RossWilliams Thank you for the PR. This change does not support the case for composite sort key. If you look at the schema below
type Model1 @key(name: "byNameIdAndSort", fields: ["name", "id", "sort"]) {
id: ID!
sort: Int!
name: String!
}
type Model3 @model {
id: ID!
model1Id: ID
model1Sort: Int
connectionSK: String
model1Name: String
connectionsWithCompositeKey: [Model1]
@connection(
keyName: "byNameIdAndSort"
fields: ["model1Name", "model1Id", "model1Sort"]
)
}
The following mutation would not throw any error
mutation createModel3WithBrokenConnection {
createModel3(input: { model1Sort: 10, id: "2", model1Id: "1" }) {
connectionsWithCompositeKey {
items {
id
name
sort
}
}
}
}
This cause the requestTemplate to throw error. To fix this we could add an default value for fields that are NULL
in the transformer and assign it.
Remove non-null check from key-based connections. Add early-return to request mapping templates when a specified key is null. Future work needed to allow pk values to be integers in connections.
82f78b7
to
3352eea
Compare
@yuth I've failed to replicate or understand the problem you mentioned. Your example includes a missing pk for a connection. The resolver template does not throw an error and correctly returns early with an empty result, as there are no valid connected items. I've added a commit with tests to match your above described schema. In the test the mutation and subsequent request templates do not throw errors. I also show when a sort key is missing from a composite sort key, and that an item is returned in a mutation when all keys are provided. Can you try explaining the issue you found in more detail? Which request template do you see throwing an error? Maybe there is a misunderstanding of the expected behaviour? Did you mean to set a null composite sort key value, although this is also handled by the PR? |
Any updates on this? I am forced to make some of my fields non-nullable. |
@xitanggg Sorry I’ve been busy. This weekend I’ll rebase this branch to main and see if that gets it a response |
Thanks a lot Ross. Would love it see it work, but no worries. It is not an urgent issue, so take your time. |
Any progress on this? |
@RossWilliams @yuth any progress here? A lot of us are waiting on this functionality. |
Also waiting for this functionality, has the test case @yuth mentioned been solved? Can i help maybe? (first time contributor) |
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
Thanks for the approval. Can you share when will this likely be merged and released? We are currently developing a feature where we are forced to fill a random value to make the connection key field non-nullable. This workaround works but is not perfect because the random value would unnecessarily trigger a connection query whereas the null key wouldn't normally. Would love to know if we might be able to ditch the workaround before rolling this feature to production. Thanks. |
@RossWilliams I am excited about this as this fixes aws-amplify/amplify-category-api#262 Could you please let me know how I can pull this in my local amplify environment using npm? Also, when will this be released? Thanks in advance |
@arkllc I’m not involved in releases. |
hi guys, I have upgrade to BUT I can successfully generate new graphql file using is this release reliable or got published accidentally? |
It is part of |
hi all -- i'm on 4.43.0 and still facing this issue. any ideas what could be going on if this has been merged in? |
For anyone running into this issue (on v4.43.0), here's a workaround (pretty hackey but will suffice for now): replace all of your
|
I don't have any issue with null key-based connection anymore, it works great in my end. Thanks again for the merge. The purpose of this merge is to let you use User @model {
id: String!
schoolID: String #Previously, it must be non-nullable using `String!`
connectSchool: School @connection(fields: ["schoolID"])
} From your code, it seems that you are trying to ignore the graphQL If after all, some data fields are not required, you can simply make it nullable by removing the ! in your schema. |
@RossWilliams Is this fix supposed to fix aws-amplify/amplify-category-api#262 too? If yes, I am on 4.50.0 but somehow I still am not able to create data with null secondary keys. appreciate it in advance, if you could please help clarify |
feat(graphql-connection-transformer): allow null key-based connections
Issue #, if available: #5152 #5129 #3275
Description of changes:
Allow connections to @key defined indexes to include nullable connection attributes.
Legacy @connection directive allowed use of nullable fields. When @connection with @key was added, code was put in place to prevent using a nullable scalar as a connection key. I traced the origin of this code and could find no justification for it and removed the check.
Further, when a request is made using attributes which are null, DynamoDB can return errors due to wrong types being provided in key condition values. There is no reason to make a request to DynamoDB when the pk is empty, or a specified field is null. Code is added to early return a null value or an empty list of items.
This PR also fixes an issue found when making a query with an Int type sort field whose value is null. Previously the NONE_INT_VALUE was wrapped in quotes, making it a string. This caused a DynamoDB mismatched type error.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.