1

I have no clue how to fetch nested data on AppSync with React. These are the models.

type BlrPost @model {
  id: ID!
  parentId: String!
  parent: BlrParent @connection(name: "ParentPost")
  title: String
  contents: String
  tags: [String]
  ask: Float
  urgentFlg: Boolean
  Bids: [BlrBid] @connection(name: "PostBids")
  createdAt: AWSDateTime
  updatedAt: AWSDateTime
}

type BlrBid @model {
  id: ID!
  price: Float!
  duration: Int!
  Post: BlrPost @connection(name: "PostBids")
  consultation: BlrConsultation @connection(name: "BidConsultation")
  createdAt: AWSDateTime
  updatedAt: AWSDateTime
}

I want to fetch Bids data details in BlrPost with below code.

const postsData = await API.graphql(
  graphqlOperation(listBlrPosts, {
    sortDirection: 'ASC'
  })
);

However, I can fetch nextToken only.

...
Bids:
nextToken: null
__proto__: Object
...

I can fetch other data. And also, if I try this on AWS console directly, I can get Bids data like this.

enter image description here

1 Answer 1

1

I resolved it. All I have to do is adding information that I need on listBlrPosts in graphql/queries. Like this.

[Before]

export const listBlrPosts = /* GraphQL */ `
  query ListBlrPosts(
    $filter: ModelBlrPostFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listBlrPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        parentId
        parent {
          id
          cognitoId
          nickname
          email
          phone
          zip
          imgUrlS3
          createdAt
          updatedAt
        }
        title
        contents
        tags
        ask
        urgentFlg
        Bids {
          nextToken
        }
        createdAt
        updatedAt
      }
      nextToken
    }
  }
`

[After]

export const listBlrPosts = /* GraphQL */ `
  query ListBlrPosts(
    $filter: ModelBlrPostFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listBlrPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        parentId
        parent {
          id
          cognitoId
          nickname
          email
          phone
          zip
          imgUrlS3
          createdAt
          updatedAt
        }
        title
        contents
        tags
        ask
        urgentFlg
        Bids {
          items {
            id
            price
            duration
            createdAt
            updatedAt
          }
        }
        createdAt
        updatedAt
      }
      nextToken
    }
  }
`
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but isn't nextToken there for a reason so you can get paginated data for your nested array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.