2

因此,我正在 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,
    })

我在这里俯瞰什么?

4

1 回答 1

2

随着订阅

subscription CreatedDeletedComments {
    Comments(
    filter: {
      mutation_in: [CREATED, DELETED]
    }
  ) {
    mutation
    node {
      id
      user
      text
    }
  }
}

您正在订阅正在创建或删除的评论节点。但是,使用 mutation 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.

订阅关系也已经是我们路线图的一部分

于 2017-04-06T09:40:41.393 回答