Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@connect - one to many - nextToken usage #6886

Closed
codeinaire opened this issue Mar 16, 2021 · 8 comments
Closed

@connect - one to many - nextToken usage #6886

codeinaire opened this issue Mar 16, 2021 · 8 comments
Assignees
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 pending-response Issue is pending response from the issue author question General question

Comments

@codeinaire
Copy link

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

Which Category is your question related to?
API GraphQL

Amplify CLI Version

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

What AWS Services are you utilizing?
Lambda, AppSync, Cloudfront, DynamoDB.

Provide additional details e.g. code snippets

I set up a one-to-many relationship between a couple of model. It works fine. I'm able to query for the one model record and from that get the many model records. However, I set a limit of 10 for the many model records but it's not obvious to me how to use the nextToken value to retrieve the other many model records.

I've tried using the list query for the many model and the list query for the one model but I get the invalid start key error. How can I use the nextToken value to get the other many model records?

@codeinaire codeinaire added the question General question label Mar 16, 2021
@renebrandel
Copy link
Contributor

hi @codeinaire - I'm having trouble fully understanding the issue. Can you share more of your query itself? A sample schema & query would add sufficient context.

Here's just some additional info on pagination: https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/js#paginating-queries

@codeinaire
Copy link
Author

codeinaire commented Mar 16, 2021

This is the relevant part of the schema.

type PageForms
  @model
  @auth(
    rules: [
      { allow: private, operations: [update, read] }
      { allow: private, provider: iam, operations: [create, read, update] }
    ]
  ) {
  id: ID!
  pageId: String
  mappingDatas: [MappingData]
    @connection(keyName: "byPageForms", fields: ["id"], limit: 10)
}

type MappingData
  @model
  @auth(
    rules: [
      { allow: private, operations: [update, read] }
      { allow: private, provider: iam, operations: [create, read, update] }
    ]
  )
  @key(
    fields: ["archived", "formCreatedTime"]
    name: "ByArchived"
    queryField: "listMappingDataByArchived"
  )
  @key(
    name: "ByFormIdAndArchive"
    fields: ["formId", "archived", "formCreatedTime"]
    queryField: "listFormsByPageIdAndArchive"
  )
  @key(name: "byPageForms", fields: ["pageFormsID", "formId"]) {
  id: ID!
  formName: String
  formId: String
  pageId: String
  mappingInfo: MappingInfo
  rawLeadFields: [RawLeadField]
  status: Status
  formCreatedTime: String
  deliveryStatus: Int
  errors: Int
  campaignName: String
  campaignId: String
  archived: ArchivedStatus
  pageFormsID: ID
  leads: [Lead] @connection(keyName: "byMappingData", fields: ["id"])
}

This is the relevant queries.

let mappingDatas: any
      const pageForms: any = await API.graphql(
        graphqlOperation(listPageFormss, {
          filter: {
            pageId: {
              eq: pageId
            }
          }
        })
      )
      const pageForm: any = await API.graphql(
        graphqlOperation(getPageForms, {
          id: pageForms.data.listPageFormss.items[0].id
        })
      )

This is the relevant result of pageForm

{
      ...
      pageId: 10
      mappingDatas: {
            items: Array(10) [ {}, {}, {},  ],
            nextToken: "<a token>"
      }
}

I want to be able to get more of the mappingDatas by using nextToken, but it's not clear how to do that. I understand how to use nextToken to get more records in a regular list query, but this nextToken is nested in a record from a get query, so how do I use it to get more of the mappingDatas records?

I hope that clears it up.
​​​​

@codeinaire
Copy link
Author

BTW @renebrandel the only reason I'm doing this is because of this issue. But if I can't do pagination with the nextToken from in the nested record and I can't use a composite key, what options do I have left!? TBH it's kind of frustrating.

p.s. I posted the issue I linked using my work github account. I should've posted this issue using the same account but forgot I was logged in on my personal account.

@kaustavghosh06 kaustavghosh06 added the graphql-transformer-v1 Issue related to GraphQL Transformer v1 label Mar 22, 2021
@renebrandel
Copy link
Contributor

Hi - can you share the listPageFormss GraphQL query here? If you can give us the query, it'll be easier to explain it. Basically nested nextToken can be passed into that query as an input.

@codeinaire
Copy link
Author

codeinaire commented Mar 22, 2021

Great! Here's the query:

export const listPageFormss = /* GraphQL */ `
  query ListPageFormss(
    $filter: ModelPageFormsFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listPageFormss(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        pageId
        createdAt
        updatedAt
        mappingDatas {
          nextToken
        }
      }
      nextToken
    }
  }
`;

And, just in case, here's how it's used:

const pageForms: any = await API.graphql(
        graphqlOperation(listPageFormss, {
          filter: {
            pageId: {
              eq: pageId
            }
          }
        })
      )

@yuth
Copy link
Contributor

yuth commented Mar 23, 2021

@codeinaire the mappingDatas field can take an argument called nextToken and when you pass that it would use that value to paginate the mapping data. So the updated query would look somewhat like this

export const listPageFormss = /* GraphQL */ `
  query ListPageFormss(
    $filter: ModelPageFormsFilterInput
    $limit: Int
    $nextToken: String
    $mappingDataNextToken: String
  ) {
    listPageFormss(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        pageId
        createdAt
        updatedAt
        mappingDatas (nextToken: $mappingDataNextToken){
          items {
            id
            formName
            formId
            pageId
            # Other fields that you're interested in
          }
          nextToken
        }
      }
      nextToken
    }
  }

When trying to get the next page of mappingData pass the you will need to pass the nextToken from mappingDatas of each listPageFormss to mappingDataNextToken

@yuth yuth added the pending-response Issue is pending response from the issue author label Mar 23, 2021
@codeinaire
Copy link
Author

@yuth it appears to be working. Thanks for your help!

BTW, is this anywhere in the docs? I couldn't find anything like this and not sure if it's not there or I just didn't look in the right places.

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 pending-response Issue is pending response from the issue author question General question
Projects
None yet
Development

No branches or pull requests

4 participants