I am new to using amplify with GraphQL. I was setting up my DB schema and auto-generating the functions after running amplify push.
Goals I want to achieve but do not know how to are
- I would like to be able to get user with all connected information (with one to one and one to many relationships) in the returned object from getUser
- I would like to still be able to get userByUserName and see all connected one-to-many relationships as well
When calling the API generated function to get the user,
let user = await API.graphql(graphqlOperation(getUser,{id:userId}))
I am getting a user object back, however, it looks like this - even though I am definitely sure that data is set up correctly in the database.
buttons: {nextToken: null} -- WANT THIS TO INCLUDE ACTUAL INFORMATION ABOUT BUTTONS CONNECTED TO THIS USER
createdAt: "2020-09-02T23:41:12.278Z"
customStyles: {id: "e3d1bbef-ec6f-4a6d-9b5d-e693e890d4e0", bgColor: "F9FF9F", bgBtnColor: "FFFFFF", bgBtnHoverColor: "000000", textColor: "000000", …}
defaultStyles: null
email: "[email protected]"
firstName: "Nata"
id: "d683a6bb-383e-4cf1-943a-05b3da4e5cc3"
lastName: "Vache"
socialNetwork: {nextToken: null} -- WANT THIS TO INCLUDE ACTUAL INFORMATION ABOUT SOCIAL NETWORKS CONNECTED TO THIS USER, THE SAME WAY AS FOR EXAMPLE customStyles IS SHOWN.
updatedAt: "2020-09-02T23:41:12.278Z"
userName: "Nata568"
type User @model @key(name: "byUserName", fields: ["userName"], queryField: "userByUserName"){
id: ID!
firstName: String!
lastName: String!
userName: String!
email: String!
socialNetwork: [UserSocialNetwork] @connection(keyName: "UserSocialNetworkUser", fields: ["id"])
buttons: [Button] @connection(keyName: "ButtonUser", fields: ["id"])
defaultStyles: DefaultStyle @connection
customStyles: CustomStyle @connection
}
type UserSocialNetwork @model @key(name: "UserSocialNetworkUser", fields: ["userID", "id"], queryField:"userSocialNetworkByUserID") {
id: ID!
socialNetworkUsername: String!
userID: ID!
supportedSocialNetwork: SupportedSocialNetwork! @connection
}
type SupportedSocialNetwork @model {
id: ID!
name: String!
address: String!
}
type Button @model @key(name: "ButtonUser", fields: ["userID", "id"], queryField: "buttonByUserID") {
id: ID!
name: String!
address: String!
image: String
userID: ID!
}
This schema does not include all my model definitions - customStyles, defaultStyles, and the rest but they are one to one relationship, which is working perfectly fine. I am having issues with one-to-many relationships, such as User to UserSocialNetwork and User to Buttons.
I have read lots of resources about this on AWS Amplify Docs, have gone through examples but still have not found anything that I could work with that would allow me to get the information from connections on getUser call and also give me the ability to get the user by username. Any input would be appreciated!!!
General comment: If username is unique you could use that as the id instead of creating the extra index. If it isn't there will be problems with this schema since it can't do a getOperation but instead will do a query which might return multiple answers.
(The resolver in Appsync wants to use a
dynamoDB.get
by default (& design) but using an index would be adynamoDB.query
which results in a lot issues)Anyway using your schema I can get it to work just fine when using the id
This being my query
Here is one where I made email the id.
Also works
Bonus using your index
This was the schema I used
Maybe I have misunderstood something?