8
votes

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?

2
do you mind also sharing your schema.graphql? - Yik San Chan
Added the schema! - Ohsik

2 Answers

5
votes

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.

2
votes

not sure if you're still working on this or what your solution may have ended up being, but I think the following should work as well:

as part of the amplify codegen process, it should create a folder called "graphql", where it will place a schema.json, subscriptons.graphql, queries.graphql, and mutations.graphql. In this folder, you can add a custom file called anything you want. I usually call mine customqueries.graphql. and it will pick up these queries or mutations or whatever you define in the codegen process. So in that file you could define a GetUserPostsCustom query that takes in the nextToken for the posts, like so:

query GetUserPostsCustom($id: ID!, $nextToken: String) {
  getUser(id: $id) {
    posts(nextToken: $nextToken){
      items{
        id
        title
      }
      nextToken
    }
  }
}

and then when you call your api if you call the GetUserPostsCustom, there should be an option to pass in the nextToken. At least I believe this works when generating code for swift. not sure about javascript.