分页

概览

了解 Altium 365 API 中基于游标的分页机制,涵盖理解连接响应、遍历集合以及获取条目总数。本文介绍请求第一页及翻阅结果时使用的查询参数和分页元数据。继续阅读,了解如何在实践中应用这些分页模式。

- 了解 'nodes'、'pageInfo' 和 'totalCount' 如何构成分页响应。
- 学习如何获取第一页,并使用游标向前或向后翻页。
- 了解如何在不获取集合条目的情况下获取其条目总数。
使用 AI 深入了解:

Altium 365 API 遵循 Relay 连接规范,使用基于游标的分页机制。集合查询返回一个 connection 类型,其中包含节点列表、分页元数据,以及用于在结果中向前或向后导航的游标。

连接结构

分页响应具有以下结构:

query {
  someCollection {
    nodes {        # the items on this page
      ...
    }
    pageInfo {     # pagination metadata
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
    totalCount     # total number of items (all pages)
  }
}

获取分页数据

使用 first 和 after 参数向前遍历集合中的各页:

query {
  desProjects(first: 20, after: "{cursor}") {
    nodes {
      id
      name
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}
  • first – 要返回的条目数(每页大小)

  • after – 标记上一页末尾的游标;首次请求时省略

pageInfo 中的 endCursor 将成为下一页的 after 值。持续获取,直到 hasNextPage 为 false。

要向后翻页,请使用 last 和 before,并配合 startCursor。

首次请求

获取第一页时,省略 after 参数:

query {
  desProjects(first: 20) {
    nodes {
      id
      name
      updatedAt
    }
    pageInfo {
      hasNextPage
      endCursor
    }
    totalCount
  }
}

在开始分页之前,使用 totalCount 确定预计的总页数。

仅计数而不获取条目

如果只需要总数,可以完全省略 nodes:

query {
  desProjects {
    totalCount
  }
}

 

AI-LocalizedAI 翻译
如您发现任何问题,请选中相关文本/图片,并按 Ctrl + Enter 键向我们提交反馈。
Contents
Contents