Feature Request / Bug
Consider the following example:
type User
@model
{
id: ID!
userSchoolId: ID!
userTags: [UserTags]
@connection(
keyName: "UserTagsBySchoolByUserByCategoryByTag"
fields: ["userSchoolId", "id"]
)
}
type UserTags
@model
@key(fields: ["categoryId", "tagId", "userId"])
@key(
name: "UserTagsBySchoolByUserByCategoryByTag"
fields: ["schoolId", "userId", "categoryId", "tagId"]
) {
categoryId: ID!
tagId: ID!
schoolId: ID!
userId: ID!
}
UserTags creates a GSI with a combined sort key. This allows for multiple access patterns. Get all UserTags by School. Get all UserTags for User. Get all UserTags for User by Category.
But if you run amplify api gql-compile on the example above, it would produce the following error:
## InvalidDirectiveError: Invalid @connection directive usersTags. fields does not accept partial sort key
For the User to get all their tags though, all the sort keys aren't necessary. A User only needs a schoolId and their id to get their tags. The current workaround for this situation is:
type User
@model
{
id: ID!
userSchoolId: ID!
userTags: [UserTags]
@connection(
keyName: "UserTagsBySchoolByUserByCategoryByTag"
fields: ["userSchoolId", "id", "id", "id"]
)
}
type UserTags
@model
@key(fields: ["categoryId", "tagId", "userId"])
@key(
name: "UserTagsBySchoolByUserByCategoryByTag"
fields: ["schoolId", "userId", "categoryId", "tagId"]
) {
categoryId: ID!
tagId: ID!
schoolId: ID!
userId: ID!
}
Then modify User.userTags.req.vtl and remove user.id being passed as categoryId and tagId in the DB query
@duwerq we don't support partial sort keys yet, closing in favor of #5468
@ammarkarachi Sorry, I think there are two separate issues here. Issue mentioned in #5468 is that when using @connection, all fields must be non-nullable. What this means is that using @duwerq 's previous example, because User use @connection with fields ["userSchoolId", "id"], both userSchoolId and id need to be non-nullable or use a ! at the end. This is a bug and it is addressed by this pending pull request #5153 , because a user might not be affiliated with any school and forcing to assign a school id to a user is buggy.
type User
@model
{
id: ID!
userSchoolId: ID!
userTags: [UserTags]
@connection(
keyName: "UserTagsBySchoolByUserByCategoryByTag"
fields: ["userSchoolId", "id"]
)
}
However, the issue with "fields does not accept partial sort key" is another bug and is totally different. User and UserTags is a 1 to many relationships. Therefore, User is creating a connection with UserTags with fields userSchoolId and id. It always and can only provide partial sort key because it is a 1 to many relationships. User doesn't know categoryId or tagId. Given the current situation, we can't query UserTags within User, but we can query UserTags with fields userSchoolId and id (see the bug here?) A workaround is to separate the two queries.
I request to reopen this issue, as the two are totally different.