Skip to content
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

@connection should accept partial sort key #5488

Closed
duwerq opened this issue Oct 5, 2020 · 3 comments
Closed

@connection should accept partial sort key #5488

duwerq opened this issue Oct 5, 2020 · 3 comments

Comments

@duwerq
Copy link

duwerq commented Oct 5, 2020

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

@ammarkarachi
Copy link
Contributor

@duwerq we don't support partial sort keys yet, closing in favor of aws-amplify/amplify-category-api#262

@xitanggg
Copy link

xitanggg commented Nov 23, 2020

@ammarkarachi Sorry, I think there are two separate issues here. Issue mentioned in aws-amplify/amplify-category-api#262 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.

@github-actions
Copy link

This issue 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 *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants