【问题标题】:Graphql pulling data for paginationGraphql 拉取数据进行分页
【发布时间】:2018-12-07 03:57:43
【问题描述】:

我有以下架构

User {
    username: String!
    name: String!
    posts(page: Int!): [Post]
}

Post {
    title: String!
    description: String
}

在查询中

type Query {
    user(username: String!): User
}

在解析器中我定义了Query -> useruser -> posts 当我提取我使用的数据时:

{
    user(username: 'pewpewlasers'){
        username
        name
        posts(page: 1){
            title
            description
        }
    }
}

效果很好。现在我的问题是如何在无限滚动的情况下有效地拉第二页(最后加载更多按钮)。我可以使用:

{
    user(username: 'pewpewlasers'){
        username
        name
        posts(page: 2){
            title
            description
        }
    }
}

但是再次吸引用户似乎没有必要,我只需要发布更多内容。是否有内置方式 graphql 处理这个?还是我必须定义一个单独的posts(page: Int!, username: String!) 查询

【问题讨论】:

    标签: graphql apollo apollo-server graphql-tools


    【解决方案1】:

    有几种方法可以解决这个问题:

    1. 创建一个中间类型,特别是UserRelations 目的是为这些类型的 查询,而无需通过用户。这是最简单的方法, 我以前也做过,但是弄乱数据感觉很讨厌 以这种方式建模。
    2. 这样当您解析User 时,您可以检查哪些字段是 被请求。如果您只要求不需要的字段 加载用户(即仅关系),返回一个虚拟对象而不是获取数据。 这涉及更多,因为这意味着查看您的字段中的 AST 解析器,但熟悉这一点是使用 GraphQL 进行大多数性能优化的关键。

    【讨论】:

      猜你喜欢
      • 2018-10-22
      • 1970-01-01
      • 2011-05-28
      • 2015-10-30
      • 2019-12-31
      • 2019-09-20
      • 2019-06-09
      • 2015-05-02
      • 2020-06-28
      相关资源
      最近更新 更多