11

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?

Skate to Eat
  • 2,554
  • 5
  • 21
  • 53

2 Answers2

6

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.

J. Hesters
  • 13,117
  • 31
  • 133
  • 249
  • 1
    Yes, but I 'm using `const data = await API.graphql(graphqlOperation(queries.getUser, {id: username}))` to get all user data which include journals with nextToken. I want to know if it's possible to call another `const data = await API.graphql(graphqlOperation(queries.getUser, {id: username}))` with nextToken. – Skate to Eat Jun 10 '19 at 00:30
  • @Ohsik The short answer is yes you can call more queries with `nextToken` it will automatically be `null` if there is nothing more to query. If that doesn't answer your question, please try to elaborate more and I might help you. – J. Hesters Jun 10 '19 at 08:26
  • Thank you for the comment. How do I call `getUser` with `nextToken` since it only takes `id` as a parameter(`getUser(id: $id)`). There are more than 10 posts to query and it should not be `null`. Please let me know if it's still not clear. – Skate to Eat Jun 13 '19 at 05:33
  • I think I got it. If you used AWS Amplify to generate your queries, you will have a file located at (in React) `src/graphql/queries.js`. In it you will find `getUser` as well as `listUsers`. `getUser` (or `get` queries in general) are for getting **single** instances. `list` are for getting multiple. In `listUsers` you should see that it takes three argements: `$filter: ModelUserFilterInput`, `$limit: Int` and `$nextToken: String`. That's what you are looking for. Hope this helps. – J. Hesters Jun 13 '19 at 06:17
  • Yes, you got it! so I just can't use `getUser` to get next 10 posts. I have to use `listUers`. I was hoping there are ways to use `getUser` :( – Skate to Eat Jun 13 '19 at 07:10
  • @Ohsik Why? What feature does `getUser` have that `listUser` doesn't? – J. Hesters Jun 13 '19 at 07:24
  • For my use case, ‘getUser’ gets me 10 posts from that specific user and I can use ‘sortDirection’ to get latest 10 posts. However, ‘listPost’ doesn’t take user and sort direction parameter which makes it harder to get 10 latest posts from by user id. – Skate to Eat Jun 13 '19 at 07:27
  • @Ohsik, yes it can. You can use its `filter` parameter to get the user's posts along with `limit` to get more than 10 posts. And you might need to use local secondary indexes. – J. Hesters Jun 13 '19 at 11:01
  • @J.Hesters if you want to get all posts from a user, you have to use getUser, because listPosts won't have a filter for the user, or is there? To my understanding the filter input won't contain any id for the connection – AndreasEK Nov 28 '19 at 23:04
3

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.

Julien S.
  • 214
  • 3
  • 9