我在我的中继分页容器组件中订阅了一条新记录。当它从后端检索新记录时,我以这种方式将其插入连接中(在 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!
}