Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppSync GraphQL query with nextToken

My schema.graphql file auto generated below query in graphql/queries.js file by running amplify push command.

Here is the schema file that generates query automatically.

schema.graphql

type User @model {
  id: String!
  uuid: String!
  following: [String]
  follower: [String]
  posts: [Post] @connection(name: "Userposts", sortField: "createdAt")
}

type Post 
  @model
  @auth(
    rules: [
      { allow: owner },
      { allow: groups, groups: ["Admin"] }
    ]
  ) {
  id: ID!
  author: User! @connection(name: "Userposts")
  status: Status!
  draft: Boolean
  content: AWSJSON!
  loved: [String]
  comments: [Comment] @connection(name: "PostComments", sortField: "createdAt")
  createdAt: AWSDateTime
    updatedAt: AWSDateTime
}

enum Status {
  PRIVATE
  PUBLIC
}

Here's the queries generated by schema graphql.

queries.js

export const getUser = `query GetUser($id: ID!) {
  getUser(id: $id) {
    id
    uuid
    following
    follower
    posts(limit: 10, sortDirection: DESC) {
      items {
        id
        privacy
        draft
        content
        loved
        createdAt
        updatedAt
      }
      nextToken
    }
    notifications {
      items {
        id
        content
        category
        link
        createdAt
      }
      nextToken
    }
  }
}
`;

I added (limit: 10, sortDirection: DESC) to posts to get the 10 latest posts from the user but can't figure out how to pass nextToken value to get another 10 posts after the first query.

How do I pass nextToken value to the posts so I can get next 10 posts?

like image 897
Skate to Eat Avatar asked Jun 07 '19 06:06

Skate to Eat


1 Answers

Assuming you are using the graphqlOperation helper method:

const newData = await API.graphql(graphqlOperation(listUser, { nextToken }));

Here, nextToken from previous request is passed as an argument using object shorthand notation.

like image 189
J. Hesters Avatar answered Sep 17 '22 15:09

J. Hesters