Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/d18e68b3266825eb3d1bcfb662b87a47 to your computer and use it in GitHub Desktop.
Save kenmori/d18e68b3266825eb3d1bcfb662b87a47 to your computer and use it in GitHub Desktop.
apollo x react implements to run mutation with subscription Data when messages was published in updateQueryCallback

implements to run mutation with subscription Data when messages was published in updateQueryCallback

  useEffect(() => {
    const updateQueryCallBack = (prev, arg) => {
      if (arg.subscriptionData.data.messagePublished.id === prev.messages.nodes[0].id) return prev;
      const obj = {
        messages: {
          nodes: [arg.subscriptionData.data.messagePublished, ...prev.messages.nodes],
          __typename: 'MessageConnection',
        },
      };
      client.mutate({ mutation: readMessageMutation, variables: { messageId: arg.subscriptionData.data.messagePublished.id }, refetchQueries: () => [{ query: unreadMessagesQuery }] }).then(() => {});//here
      return obj;
    };
    const subscribe = subscribeToMore({
      document: subscriptionMessage,
      variables: { id: messageThreadId },
      updateQuery: updateQueryCallBack,
    });

    return () => subscribe();
  }, [subscribeToMore, messageThreadId, client]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment