所以,我正在测试Graphcool上的订阅,并希望对它们的工作原理进行一些澄清。
我在帖子评论中有一对多的关系:
模式
type Posts {
caption: String!
comments: [Comments!]! @relation(name: "PostsOnComments")
createdAt: DateTime!
displaysrc: String!
id: ID!
likes: Int
updatedAt: DateTime!
}
type Comments {
createdAt: DateTime!
id: ID!
posts: Posts @relation(name: "PostsOnComments")
text: String!
updatedAt: DateTime!
user: String!
}
我在Graphcool中运行的订阅如下:
subscription CreatedDeletedComments {
Comments(
filter: {
mutation_in: [CREATED, DELETED]
}
) {
mutation
node {
id
user
text
}
}
}
如果我在我的React应用程序中运行以下命令,则会触发创建的通知:
return this.props.client.mutate({
mutation: gql`
mutation createComment ($id: ID, $textVal: String!, $userVal: String!) {
createComments (postsId: $id, text: $textVal, user: $userVal){
id
text
user
}
}
`,
variables: {
"id": postID,
"textVal": textVal,
"userVal": userVal
},
// forceFetch: true,
})
但如果我运行以下命令,则不会触发删除的通知:
return this.props.client.mutate({
mutation: gql`
mutation removeComment ($id: ID!, $cid: ID!) {
removeFromPostsOnComments (postsPostsId: $id, commentsCommentsId: $cid){
postsPosts {
id
displaysrc
likes
comments {
id
text
user
}
}
}
}
`,
variables: {
"id": postID,
"cid": commentID
},
// forceFetch: true,
})
我在这里俯瞰什么?
答案 0 :(得分:2)
订阅
subscription CreatedDeletedComments {
Comments(
filter: {
mutation_in: [CREATED, DELETED]
}
) {
mutation
node {
id
user
text
}
}
}
您正在订阅正在创建或删除的评论节点。但是,使用变异removeFromPostsOnComments
,您不会删除任何注释节点。相反,您只是删除帖子和评论之间的连接。
您可以调整您的变异请求以完全删除评论,而不是将其与帖子断开连接:
return this.props.client.mutate({
mutation: gql`
mutation removeComment ($cid: ID!) {
deleteComment(id: $cid) {
id
}
}
`,
variables: {
"cid": commentID
},
// forceFetch: true,
})
如果您不想完全删除评论但仍希望在应用中隐藏评论,则可以使用布尔字段deleted
作为软删除标记。
然后,您可以订阅UPDATED
条评论而不是DELETED
条评论,并检查字段deleted
是否已更新。有关如何使用updatedFields
。
关系的订阅也已part of our roadmap。