Apollo GraphQl 分页 - 限制和跳过

Posted

技术标签:

【中文标题】Apollo GraphQl 分页 - 限制和跳过【英文标题】:Apollo GraphQl pagination - limit and skip 【发布时间】:2020-01-30 21:59:46 【问题描述】:

我想用skiplimit参数实现简单的分页

在文档中 (https://graphql.org/learn/pagination/) 我看到了:

我们可以做类似friends(first:2 after:$friendId)之类的事情来询问 在我们获取的最后一个朋友之后的接下来的两个。

但如果我在查询中传递 first 参数,我会得到错误:

"message": "未知参数 \"first\" 类型的字段 \"documents\" \"查询\".",

我的查询类型定义:

  extend type Query 
   documents(search: String, paragraphSize: Int, filters: DocumentFilters, sort: DocumentSort): [Document]

问题:我应该在我的文档端点中添加参数first并在服务器端自己处理所有限制/跳过/等,还是我错过了一些东西,GraphQL 自己处理所有限制等?

【问题讨论】:

【参考方案1】:

从您的 typeDef 中没有参数说明 first 或 skip。它不是已经存在的参数,它必须被定义

像这样的

documents(search: String, paragraphSize: Int, filters: DocumentFilters, sort: DocumentSort, skip: Int, limit: Int): [Document]

如果您愿意,可以进行另一个输入来保存所有这些值

【讨论】:

以上是关于Apollo GraphQl 分页 - 限制和跳过的主要内容,如果未能解决你的问题,请参考以下文章