简体   繁体   English

将边插入继电器连接的正确方法是什么?

[英]What is the correct way to insert an edge into relay connection?

I have a subscription for a new records in my relay paginationContainer component. 我在中继paginationContainer组件中订阅了新记录。 When it retrieves a new record from backend I insert it into connection this way (inside an updater option of requestSubscription ): 当它从后端检索新记录时,我以这种方式将其插入连接(在requestSubscriptionupdater选项中):

const newEdge = ConnectionHandler.createEdge(
  store,
  connection,
  newPostNode,
  'posts'
)
ConnectionHandler.insertEdgeBefore(connection, newEdge)

It works correctly as i see a new inserted edge in console.log(props.posts.edges) . 当我在console.log(props.posts.edges)看到新插入的边缘时,它可以正常工作。 However a cursor parameter of this new inserted edge is undefined while it's node parameter is a record itself, as i expect. 但是,正如我期望的那样,这个新插入的边缘的cursor参数是undefined而它的node参数是一条记录本身。

I think it is not correct as each edge in connection must have a cursor . 我认为这是不正确的,因为连接中的每个边缘都必须有一个cursor

What is the correct way to insert a new edge into Relay connection so it includes a cursor ? 将新边插入中继连接以便它包括cursor的正确方法是什么?

Every edge must have a cursor, and it should normally be provided by the server. 每个边缘必须有一个游标,并且通常应由服务器提供。 Which means a subscription or mutation returning a node that could be part of an edge, should also provide the cursor. 这意味着订阅或变异返回可能是边缘一部分的节点,也应提供游标。

There are two main approaches for this. 有两种主要方法。 If it's only possible for a connection to be sorted in one way (thereby guaranteeing the same node always has the same cursor), you can just return the entire edge type: 如果只能以一种方式对连接进行排序(从而保证同一节点始终具有相同的游标),则可以返回整个边缘类型:

type PostEdge {
    node: Post!
    cursor: String!
}

type MyMutationPayload {
    edge: PostEdge!    
}

type Mutation {
    newPost(input: NewPostInput!): MyMutationPayload!
}

If a connection can be sorted in multiple ways (eg by title, publication date, author), then the cursor is vary per sort option, which means you can't return the edge itself, but you can instead return the necessary information to construct the edges in the client updater: 如果可以通过多种方式对连接进行排序(例如,按标题,出版日期,作者),则光标会因排序选项而异,这意味着您无法返回边本身,而是可以返回必要的信息来构建客户端更新程序中的边缘:

enum PostSort {
    NEWEST_FIRST
    AUTHOR_AZ
    TITLE_AZ 
}

type PostCursors {
    sortKey: PostSort!
    cursor: String!
}

type MyMutationPayload {
    node: Post!
    cursors: [PostCursors!]!
}

type Mutation {
    newPost(input: NewPostInput!): MyMutationPayload!
}

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

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