amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Retrieve all connected objects and array of objects in a single query

Open indrajitb opened this issue 5 years ago • 1 comments

Note: If your question is regarding the AWS Amplify Console service, please log it in the AWS Amplify Console repository

Which Category is your question related to?

Code generation by Amplify

Amplify CLI Version 4.41.2

You can use amplify -v to check the amplify cli version on your system

What AWS Services are you utilizing? Amplify, AppSync

Provide additional details e.g. code snippets

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 array of objects.

I would be highly glad for any of your inputs.

The functionality I want: I have a Model name 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:

export const getPosts = /* GraphQL */
query GetPosts(
 $type: TableName
    $createdAtUpdatedAt: ModelPostByTimeCompositeKeyConditionInput
    $sortDirection: ModelSortDirection
    $filter: ModelPostFilterInput
    $limit: Int
    $nextToken: String
) {
getPosts(
type: $type
      createdAtUpdatedAt: $createdAtUpdatedAt
      sortDirection: $sortDirection
      filter: $filter
      limit: $limit
      nextToken: $nextToken
){
items {
        type
        id
        userId
        user {
            ...all fiedls of User
         }

        **comments {
          nextToken
        }**
}
...other fields
}
}

I am unable to get a query generated to retrieve all Posts with all the fields from the Comment model within.

I went through a few documentations for AWS Amplify, AppSync at [https://docs.amplify.aws/], but seem unable to get the required query generated.

One note - I can see that if I fetch the Comments, I see the Post which has this Comment returned.

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! 
}

What am I missing to get the required functionality that I'm looking for?

Thank you.

indrajitb avatar Apr 20 '21 06:04 indrajitb

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[bot] avatar May 24 '21 20:05 github-actions[bot]