连接中的不透明光标是否应该在不同的场控制中保持稳定?

dkn*_*oll 3 graphql relayjs

RANGE_ADD突变需要edgeName,以便它可以插入新的边缘到客户端连接.作为其查询的一部分,它还包括cursor.

问题是,当服务器生成边缘响应时,服务器无法知道客户端可能在哪个args应用于连接.

这是否意味着cursor应该稳定?

小智 10

通常,当连接使用不同的参数时,游标不需要相同.例如,如果我这样做:

{
  namedFriends: friends(orderby:NAME first:5) {
    edges { cursor, node { id } }
  }
  favoriteFriends: friends(orderby:FAVORITE first:5) {
    edges { cursor, node { id } }
  }
}
Run Code Online (Sandbox Code Playgroud)

可能会使用不同的后端来为这两个连接提供服务,因为我们可能会为这两个连接提供不同的后端; 因此,对于同一个朋友,游标可能不同,因为他们可能需要为不同的后端编码不同的信息.

但是,这在执行变异时会很棘手:

mutation M {
  addFriend($input) {
    newFriendsEdge {
      { cursor, node { id } } // Which cursor is this?
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,如果突变将从连接返回边缘,则字段接受连接所执行的相同非分页参数是有用的.所以在上面的例子中,我们会这样做:

mutation M {
  addFriend($input) {
    newNamedFriendsEdge: newFriendsEdge(orderby:NAME) {
      { cursor, node { id } } // Cursor for namedFriends
    }
    newFavoriteFriendsEdge: newFriendsEdge(orderby:FAVORITE) {
      { cursor, node { id } } // Cursor for favoriteFriends
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,实现newFriendsEdge(orderby:FAVORITE)favoriteFriends: friends(orderby:FAVORITE first:5)共享公共代码以生成游标.

请注意,虽然游标不需要相同,但如果它们是,则可以作为服务器的实现细节.通常,游标只是节点的ID,这是发生这种情况的常见方式.实际上,在这些情况下,如果连接上的参数影响游标,我们会从变异的边缘字段中省略它; 所以如果orderby没有影响光标,那么:

mutation M {
  addFriend($input) {
    newFriendsEdge {
      { cursor, node { id } } // orderby didn't exist on newFriendsEdge, so this cursor must apply to both.
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我们突变的常见模式.如果您遇到任何问题,请告诉我; 我们在开发突变的返回边缘模式时考虑了"参数改变游标"的情况,以确保有一个可能的解决方案(这是我们提出关于边缘字段的观点的想法),但它没有在实践中出现这么多,所以如果你遇到棘手肯定让我知道,我们可以而且应该重新审视这些假设/要求!