'Retrieve all connected objects and array of objects in a single query
I'm having trouble in bringing nested array of objects from a single query generated by AWS Amplify CLI. I can see I get a single nested object from a single query, but not an array of objects.
I would be highly glad for any of your inputs.
The functionality I want: I have a Model named Post which contains, among others, a list of Comments. I would like to retrieve all posts along with all comments associated with the posts, by not supplying the postId, so esentiially I would like to have a query like getPosts({limit: 50}).
The issue: I don't get the Comments for Posts from the query that I'm issuing. I see a single object, like User, properly returned from the query. In fact, in the query that is generated by Amplify, I see the following query generated for the post model:
comments {
nextToken
}
I am unable to get a query generated for Post with all the fields from the nested Comment model.
I followed the discussion at One to Many Relationship not showing in the object Amplify Schema definition. But, unfortunately I've not been able to get the result I want.
I went through a few documentations for AWS Amplify, AppSync on the AWS site, but seem unable to fix the issue.
One note - I can see that if I fetch the Comments, I see the parent Post which has this Comment.
I'm sharing the GraphQL schema that I've:
type Post
@model
@auth(rules: [{ allow: owner, operations: [create, read, update, delete], identityField: "sub" }])
@key(
name: "byTime"
fields: ["type", "createdAt", "updatedAt"]
queryField: "getPosts"
)
@key(
name: "byUser"
fields: ["type", "userId"]
queryField: "getPostsByUser"
) {
type: TableName!
id: ID!
userId: String
user: User! @connection(name: "UserPost")
createdAt: AWSDateTime!
updatedAt: AWSDateTime!
title: String
content: String
files: [S3Object]
comments: [Comment!]
@connection(
keyName: "byPostId"
fields: ["id", "updatedAt"]
name: "PostComments"
)
likes: [User]
likeCount: String
}
type Comment
@model
@auth(rules: [{ allow: owner, operations: [create, read, update, delete], identityField: "sub" }])
@key(
name: "byTime"
fields: ["type", "createdAt", "updatedAt"]
queryField: "getComments"
)
@key(
name: "byPostId"
fields: ["postId", "updatedAt"]
queryField: "getCommentsByPostId"
){
type: TableName!
id: ID!
postId: ID!
userId: String
content: String
createdAt: AWSDateTime!
updatedAt: AWSDateTime!
post: Post @connection(fields: ["postId"])
likes: [User]
likeCount: String
user: User @connection
}
enum TableName {
User
Profile
Post
Comment
}
type User
@model
@key(
name: "byUserEmail"
fields: ["type", "email"]
queryField: "getUserByEmail"
)
@auth(
rules: [
{ allow: owner, operations: [read, create, delete, update], identityField: "sub" }
]
) {
id: ID!
type: TableName!
name: String!
email: ID!
createdAt: String
profile: Profile @connection(name: "UserProfile")
profileCreated: Boolean
posts: [Post] @connection(name: "UserPost")
}
type Profile
@model
@key(
name: "byUserId"
fields: ["type", "userId"]
queryField: "getProfileByUserId"
)
@key(
name: "byUserEmail"
fields: ["type", "userEmail"]
queryField: "getProfileByUserEmail"
)
@auth(
rules: [
{ allow: owner, operations: [create, read, update, delete], identityField: "sub" }
]
) {
id: ID!
type: TableName!
userId: ID!
role: Role!
classRoom: String
... other fields
user: User @connection(name: "UserProfile")
}
enum Role {
TeachingStaff
Student
NonTeachingStaff
Alumni
}
type S3Object {
bucket: String!
region: String!
key: String!
}
Thank you in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|