GraphQL:突变运行时未触发订阅 [英] GraphQL: Subscription not firing when mutation run

查看:51
本文介绍了GraphQL:突变运行时未触发订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在Graphcool上测试订阅,希望对它们的工作方式进行一些澄清.

我在评论帖子中有一对多的关系:

模式

  type帖子{标题:弦!评论:[评论!]!@relation(名称:"PostsOnComments")createdAt:DateTime!displaysrc:字符串!id:ID!喜欢:整数UpdatedAt:日期时间!}输入评论{createdAt:DateTime!id:ID!帖子:帖子@relation(名称:"PostsOnComments")文字:字串!UpdatedAt:日期时间!用户:字符串!}  

我在Graphcool中运行的订阅如下:

  subscription CreatedDeletedComments {注释(筛选: {variant_in:[已创建,已删除]}){突变节点{ID用户文本}}}  

如果我在React应用程序中运行以下命令,则会触发创建的通知:

 返回this.props.client.mutate({变异:gql变异createComment($ id:ID,$ textVal:String !, $ userVal:String!){createComments(postsId:$ id,文本:$ textVal,用户:$ userVal){ID文本用户}}`,变量:{"id":postID,"textVal":textVal,"userVal":userVal},//forceFetch:是的,}) 

但是,如果我运行以下命令,则不会触发任何已删除的通知:

 返回this.props.client.mutate({变异:gql变异removeComment($ id:ID !, $ cid:ID!){removeFromPostsOnComments(postsPostsId:$ id,commentsCommentsId:$ cid){postsPosts {IDdisplaysrc喜欢注释 {ID文本用户}}}}`,变量:{"id":postID,"cid":commentID},//forceFetch:是的,}) 

我在这里俯瞰什么?

解决方案

带有订阅

  subscription CreatedDeletedComments {注释(筛选: {variant_in:[已创建,已删除]}){突变节点{ID用户文本}}} 

您正在订阅正在创建或删除的评论节点.但是,使用 removeFromPostsOnComments 突变,您不会删除任何注释节点.相反,您只删除帖子和评论之间的连接.

您可以调整您的变异请求以完全删除评论,而不必将其与帖子断开连接:

 返回this.props.client.mutate({变异:gql变异removeComment($ cid:ID!){deleteComment(id:$ cid){ID}}`,变量:{"cid":commentID},//forceFetch:是的,}) 

如果您不想完全删除评论,但仍想将其隐藏在应用程序中,则可以将布尔字段 deleted 用作软删除标记.

然后,您可以订阅 UPDATED 注释,而不是 DELETED 注释,并检查是否删除了 deleted 字段.请参阅文档,以获取有关如何使用 updatedFields 进行此操作的更多信息.

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

So, I'm testing subscriptions on Graphcool and would appreciate some clarification on how exactly they work.

I have a one to many relationship from Posts on Comments:

Schema

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!
}

The subscription I run in Graphcool is as follows:

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

If I run the following in my React app, a created notification is fired:

    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,
    })

But if I run the following, no deleted notification is fired:

    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,
    })

What am I overlooking here?

解决方案

With the subscription

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

you are subscribing to comment nodes being created or deleted. However, with the mutation removeFromPostsOnComments, you are not deleting any comment nodes. Instead, you are only deleting the connection between a post and a comment.

You can adjust your mutation request to delete the comment entirely instead of disconnecting it from the post:

return this.props.client.mutate({
  mutation: gql`
    mutation removeComment ($cid: ID!) {
      deleteComment(id: $cid) {
        id
      }
    }
  `,
  variables: {
    "cid": commentID
   },
  // forceFetch: true,
})

If you don't want to delete the comment entirely but still want to hide it in your app, you could have a boolean field deleted that acts as a soft deletion marker.

Then you could subscribe to UPDATED comments instead of DELETED comments and check if the field deleted was updated. Refer to the docs for more information on how to do that with updatedFields.

Subscriptions for relations is also already part of our roadmap.

这篇关于GraphQL:突变运行时未触发订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆