[英]What is the correct way to insert an edge into relay connection?
我在中继paginationContainer组件中订阅了新记录。 当它从后端检索新记录时,我以这种方式将其插入连接(在requestSubscription
的updater
选项中):
const newEdge = ConnectionHandler.createEdge(
store,
connection,
newPostNode,
'posts'
)
ConnectionHandler.insertEdgeBefore(connection, newEdge)
当我在console.log(props.posts.edges)
看到新插入的边缘时,它可以正常工作。 但是,正如我期望的那样,这个新插入的边缘的cursor
参数是undefined
而它的node
参数是一条记录本身。
我认为这是不正确的,因为连接中的每个边缘都必须有一个cursor
。
将新边插入中继连接以便它包括cursor
的正确方法是什么?
每个边缘必须有一个游标,并且通常应由服务器提供。 这意味着订阅或变异返回可能是边缘一部分的节点,也应提供游标。
有两种主要方法。 如果只能以一种方式对连接进行排序(从而保证同一节点始终具有相同的游标),则可以返回整个边缘类型:
type PostEdge {
node: Post!
cursor: String!
}
type MyMutationPayload {
edge: PostEdge!
}
type Mutation {
newPost(input: NewPostInput!): MyMutationPayload!
}
如果可以通过多种方式对连接进行排序(例如,按标题,出版日期,作者),则光标会因排序选项而异,这意味着您无法返回边本身,而是可以返回必要的信息来构建客户端更新程序中的边缘:
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.