2017-04-05 24 views
2

因此,我正在測試Graphcool上的訂閱,並希望對它們的工作原理做一些說明。GraphQL:當突變運行時不會觸發訂閱

我從帖子上的評論一個一對多的關係:

架構

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 { 
 
\t Comments(
 
    filter: { 
 
     mutation_in: [CREATED, DELETED] 
 
    } 
 
) { 
 
    mutation 
 
    node { 
 
     id 
 
     user 
 
     text 
 
    } 
 
    } 
 
}

如果我跑在我作出反應的應用程序下面,創建的通知被解僱:

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

我在這裏忽略了什麼?

回答

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執行此操作的更多信息,請參閱the docs

訂購關係也已part of our roadmap

+0

完美。我使用deleteComments唯一的問題是與我的用戶界面,被動緩存相關,而不是更新以反映更改。因爲我的root_query(如Apollo devTools中描述的),我如何重構updateQueries(請參閱我更新的問題)以處理'this.props.client.mutate'(我是從onClick事件觸發的)是'allPostses'? – TheoG

+0

你能爲此發佈一個單獨的問題嗎?一般來說,推薦使用'update',而不是現在使用'updateQueries'。 – marktani

+0

當然。提交了新問題。 – TheoG