Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/a64b80abc83ab4642b23ed2defa9fe2f to your computer and use it in GitHub Desktop.
Save kenmori/a64b80abc83ab4642b23ed2defa9fe2f to your computer and use it in GitHub Desktop.
[react-apollo]How to call any number of multiple mutations after getting query result (not Batch)

react-apolloでclient.queryの結果をみて、そのidの数だけmutationを実行する

In the message read function, I had to update "already read" for the number of messages that have been made in the message details page. Certainly I want to be able to use Batch and update at one time, I can't find the way right now, so I feel better

メッセージの既読機能で、メッセージ画面詳細で任意の数だけ行われているメッセージ分の「既読」更新をしなければならなかった. 確かにBatchをつかって、1回で更新できたらいいけど

[react-apollo]How to call any number of multiple mutations after getting query result (not Batch)

const MessageDetail = ( props ) => {
  useEffect(() => {
    (async () => {
      const { data } = await props.client.query({ query: messageQuery, variables: { id: props.match.params.id }});
      const shouldCallMutationForDoneRead = data.messages.nodes.map( e => e.id);
      shouldCallMutationForDoneRead.forEach( id => {
        props.client.mutate({ mutation: readMessageMutation, variables: { messageId: id }, refetchQueries: ["unreadMessagesQuery"]}).then( d => {
          console.log(d, "done mutation");
        });
      });
    })()
  },[]);
  .
  .
  .
  .
  
}

TODO

・ error handling ・ await mutation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment