How do i use a Global Secondary Index in DynamoDB to get the 'id' of a user in my table?

可紊 提交于 2020-04-07 08:45:06

问题


I'm using AWS Amplify on my react native app

I want to do something seemingly so simple, but i am a bit lost as to how to do it: I have a table with user information already in it. Here's the Schema:

type Users @model {
  id: ID!
  userName: String
  firstname: String
  weblink: String
  email: String
  mobileNum: String
 .
 .
 .
}


//**Here's my current Query.js**
export const getUsers = `query GetUsers($id: ID!) {
  getUsers(id: $id) {
    id
    userName
    firstname
    weblink
    email
    .
    .
    .
  }
}
`;

This table is populated in DynamoDB when i check my AWS console. What i need is to be able to get the id from the table using the userName (not vice versa). The id is generated when i createUser() and it's used throughout my app to get all my user's information. However when a user signs in on a new phone, this id isn't available anymore. So when they sign in via Cognito, i do know the userName and all i need to do is retrieve this id. Because there's only one unique userName, it should only return one id

Here's what i'm thinking so far: use a GSI (global secondary index). So change my schema to:

type Users @model
    @key(
       name: "ByUsername"
       fields: ["userName"]
       queryField: "getIdFromUserName"
    )  
{
  id: ID!
  userName: String
  firstname: String
  weblink: String
  email: String
  mobileNum: String
 .
 .
 .
}

Then call in my app:

const data = await API.graphql(graphqlOperation(getIdFromUserName, { userName }));

5 questions:

1) Is there a simpler way than GSI?

2) Is that how you add the GSI? Or is it more robust to do it in the AWS console?

3) What should my Query.js then look like?

4) Do i need to make a custom resolver, or is this sufficient?

5) Am i missing anything else, or can i just

amplify push  ?

来源:https://stackoverflow.com/questions/60929031/how-do-i-use-a-global-secondary-index-in-dynamodb-to-get-the-id-of-a-user-in-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!