在您的 graphql 模式中的连接中有边和节点的原因是什么? [英] What is the reason for having edges and nodes in a connection in your graphql schema?

查看:16
本文介绍了在您的 graphql 模式中的连接中有边和节点的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解实现 中继游标连接规范的更复杂的 graphql api

如果您查看下面我在 github graphql api explorer 上运行的查询

{
  repository(owner: "getsmarter", name: "moodle-api") {
    id
    issues(first:2 ) {
      edges {
        node {
          id
          body
        }
      }
      nodes {
        body
      }
      pageInfo {
        endCursor
        hasNextPage
        hasPreviousPage
        startCursor
      }
      totalCount
    } 
  }
}

注意它有字段edgesnodes.

为什么 github 在他们的 api 中有一个额外的字段叫做节点?为什么他们不直接使用边缘字段,因为您可以从边缘获取相同的数据?这只是为了方便吗?

Why does github have an additional field called nodes in their api? Why don’t they just use the edges field since you can get the same data from edges? Is this just for convenience?

推荐答案

如果我们查看公共连接实现的一般结构,您通常会看到以下内容:TypeA -> TypeAToTypeBConnection(通常是 TypeA 上的一个字段,其名称类似于 typeBConnection) -> TypeAToTypeBEdge(通常是名称为 edges 的连接上的名称字段)-> TypeB(通常是字段边上的名称,名称为 node)

If we look at the general structure of the common connection implementation you typically have the following: TypeA -> TypeAToTypeBConnection (usually a field on TypeA with a name like typeBConnection) -> TypeAToTypeBEdge (usually field of name on connection with name edges) -> TypeB (usually field name on an edge with name node)

A -> connection -> edges -> B

连接类型通常包含包含特定于整个连接的信息的字段,通常是寻呼信息、总数等.

Connection types will normally have fields containing information which is specific to the entire connection which is typically paging information, total counts, etc.

边缘类型通常具有包含特定于该连接但并非所有节点通用的信息的字段.在这种情况下,最常见的字段是 cursor,它表示连接中的节点位置",它不是全局唯一的 ID,而是一种返回连接中该位置的方法.

Edge types normally have fields which have information which is specific to that connection but not common to all nodes. The most common field in this case is cursor which represents the nodes ‘location’ in the connection which is not a globally unique ID but a way to return to that location in the connection.

节点类型通常只是连接的类型,不包含连接特定信息

Node type is normally just the type which the connection goes too which contains no connection specific information

在 github 的 API 的情况下,Edge 类型具有普遍实现的 cursor 字段,以后可以在该连接中用作参考.在不需要游标的情况下,它们还有一个绕过 edge 类型的字段.这就是为什么您会直接从连接类型中看到 edgesnodes 字段.

In the case of github’s API the Edge type has the commonly implemented cursor field which can be used as a reference within that connection later. They also have a field which bypasses the edge type in the case you don't need the cursors. This is why you see both edges and nodes fields directly off the connection type.

要查看这些游标字段,您可以发送以下查询以了解我在说什么:

To see these cursor fields you can send the following query to see what I am talking about:

{
  repository(owner: "getsmarter", name: "moodle-api") {
    issues(first:2 ) {
      edges {
        cursor
        node {
          id
        }
      }
    }
  }
}

有关这种连接方式的更多详细信息,请查看此处:https://facebook.github.io/relay/graphql/connections.htm

For more detail on this style of connection take a look here: https://facebook.github.io/relay/graphql/connections.htm

编辑 - 附加回复:允许在连接处同时访问边类型和节点类型的目的至少有两个我能想到的原因.首先,为了方便那些在用例不需要游标时使用 API 的人.其次,可能存在一种情况,根据发送的查询,他们甚至可能不需要生成游标.第二个可能是 CPU 时间的最小节省,并且可能会带来更多的麻烦.

EDIT - Additional response: The purpose of allowing access to both an edge type and a node type right at the connection could be for, at least, 2 reasons which I can think of. Firstly, for the convenience of those using the API when their use case does not require cursors. Second, there might be a case in which, depending on the query sent, they may not need to ever even generate cursors. The second would likely be minimal savings in CPU time and would probably be more trouble than it is worth.

我过去在 GraphQL 端点中实现了游标,一旦你了解了如何,它们的实际生成并不是那么困难.这只是将几个关键信息序列化的问题.还可能值得注意的是,同时提供 (A->conn->edge->BA->conn->Bcode>) 一旦你已经创建了 Edge 类型.

Having implemented cursors in a GraphQL endpoint myself in the past, once you get over the how, the actual generation of them is not really all that difficult. It is simply a matter of serializing a few key pieces of information. It also might be worth noting, it is pretty trivial to provide both (A->conn->edge->B and A->conn->B) once you have already created the Edge type.

因为我不在 Github 工作,所以我无法告诉你确切的意图是什么.但是,我绝对认为这是第一个原因……只是开发人员方便.

As I do not work for Github, I can’t tell you what exact intention was. However, I would most definitely think it is the first reason… simply developer convenience.

这篇关于在您的 graphql 模式中的连接中有边和节点的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆