带有nextToken的AppSync GraphQL查询

时间:2019-06-07 06:37:27

标签: graphql aws-amplify aws-appsync

我的schema.graphql文件是通过运行graphql/queries.js命令在amplify push文件中的查询下面自动生成的。

这里是自动生成查询的架构文件。

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
}

这是架构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
    }
  }
}
`;

我在(limit: 10, sortDirection: DESC)上添加了posts,以从用户那里获得10篇最新帖子,但是在第一次查询之后,我不知道如何通过nextToken值来获取另外10篇帖子。

如何将nextToken的值传递给帖子,以便获得下10个帖子?

2 个答案:

答案 0 :(得分:0)

假设您正在使用graphqlOperation辅助方法:

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

此处,来自上一个请求的nextToken使用object shorthand notation作为参数传递。

答案 1 :(得分:0)

不确定您是否仍在进行此工作,或者不确定最终将要解决的是什么,但是我认为以下内容也应适用:

作为放大代码生成过程的一部分,它应该创建一个名为“ graphql”的文件夹,在其中放置一个schema.json,subscriptons.graphql,query.graphql和mutations.graphql。在此文件夹中,您可以添加一个名为所需文件的自定义文件。我通常将其命名为customqueries.graphql。它将接收这些查询或变异或您在代码生成过程中定义的任何内容。因此,在该文件中,您可以定义一个GetUserPostsCustom查询,该查询接受帖子的nextToken,例如:

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

,然后在调用api(如果调用GetUserPostsCustom)时,应该有一个传递nextToken的选项。至少我相信这在为Swift生成代码时有效。不确定javascript。