简体   繁体   English

GraphQL 中基于 Cursor 的分页命名约定

[英]Cursor Based Pagination Naming Convention in GraphQL

In the GraphQL API, I often see naming conventions such as NQ and MQ as parameters used in cursor.在GraphQL API中,我经常看到NQ、MQ等命名约定作为cursor中使用的参数。 This is an example, shown below,这是一个示例,如下所示,

 "data": {
    "items": {
      "totalCount": 351,
      "pageInfo": {
        "hasNextPage": true,
        "hasPreviousPage": false,
        "endCursor": "Mw",
        "startCursor": "MQ"
      },
      "edges": [
        {
          "cursor": "MQ",
          "node": {
            "id": "UGxhY2UtMzUy",
            "displayName": "Redbeard"
          }
        },
        {
          "cursor": "Mg",
          "node": {
            "id": "UGxhY2UtMzUx",
            "displayName": "Frey of Riverrun"
          }
        },
        {
          "cursor": "Mw",
          "node": {
            "id": "QmlsbGVyLTI=",
            "displayName": "Something Else"
          }
        }
      ]
    }
  }
}

Source: https://dev.to/tymate/first-dive-into-graphql-ruby-nak资料来源: https://dev.to/tymate/first-dive-into-graphql-ruby-nak

Other examples include this rails example: https://www.2n.pl/blog/graphql-pagination-in-rails其他示例包括此导轨示例: https://www.2n.pl/blog/graphql-pagination-in-rails

What are these naming conventions and how would you for example paginate?这些命名约定是什么?例如,您将如何分页?

The Relay Server Specification defines how pagination should be done in order to be compatible with the Relay GraphQL Client .中继服务器规范定义了应如何进行分页以与中继 GraphQL 客户端兼容。 While it is not the only way how pagination can be done, it has evolved as a standard - at least in examples, since it can be easily referenced.虽然它不是完成分页的唯一方法,但它已经发展成为一种标准——至少在示例中,因为它可以很容易地引用。

The section on connections gives more info about how cursors work: 关于连接的部分提供了有关游标如何工作的更多信息:

Each edge gets a cursor value.每条边都有一个 cursor 值。 This value is - what they call - an opaque value, meaning it should not be interpreted by the server.这个值 - 他们称之为 - 一个不透明的值,这意味着它不应该被服务器解释。 It is a reference/a pointer that only the server can interpret .它是一个只有服务器才能解释的引用/指针。 So, if you have a query that gets a bunch of values:因此,如果您有一个获取一堆值的查询:

edges: [
  { cursor: "abc", node: {...} },
  { cursor: "def", node: {...} },
  { cursor: "ghi", node: {...} },
  { cursor: "jkl", node: {...} },
  { cursor: "mno", node: {...} }
]

You can request the next page by looking at the cursor of the last element mno and pass it into the query.您可以通过查看最后一个元素 mno 的mno来请求下一页并将其传递到查询中。

query {
  manyQuery(first: 5, after: "mno") {
    edges {
      cursor
      node {...}
    }
  }
}

This will give you the next 5 nodes.这将为您提供接下来的 5 个节点。 See also this section on graphql.org .另请参阅graphql.org 上的此部分

So to answer your question: The string can potentially contain anything that the server can use to reference one of your nodes.因此,回答您的问题:该字符串可能包含服务器可以用来引用您的节点之一的任何内容。 Eg an id in the database.例如数据库中的 id。 To remove the temptation to pass in an arbitrary value from the API user this string is often encoded into the base64 format.为了消除从 API 用户传递任意值的诱惑,此字符串通常被编码为 base64 格式。 The value should be meaningless to the client and only be used to be passed around back to the server.该值应该对客户端没有意义,并且仅用于传递回服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM