Search code examples
apolloapollo-clientapollostackgraphql-subscriptions

Apollo: Client-Side Subscription Code?


Here's my client-side code for Apollo subscriptions, based on the Githunt-React-master sample code:

componentWillReceiveProps(nextProps) {
    const fromID = Meteor.userId();
    const toID = nextProps.toID;
    if (!this.subscription && !nextProps.loading) {
        this.subscription = this.props.subscribeToMore({
            document: IM_SUBSCRIPTION_QUERY,
            variables: {fromID: `${fromID}`, toID: `${toID}`},
            updateQuery: (previousResult, {subscriptionData}) => {
                if (!subscriptionData.data) {
                    return previousResult;
                }
                const newFeedItem = subscriptionData.data.createIM[0];
                return update(previousResult, {
                    instant_message: {
                        $push: [newMsg],
                    },
                });
            }
        });
    }
}

My subscription resolver isn't being called. That is, a debugger statement placed in it, never halts program execution.

What am I missing?

Thanks in advance to all for any info.


Solution

  • In the standard case this.props does not contain a subscribeToMore functionality. It is added in the end of the file at githunt react master.

    Did you also add something like:

    const withData = graphql(COMMENT_QUERY, {
      options: ({
        params
      }) => ({
        variables: {
          repoName: `${params.org}/${params.repoName}`
        },
      }),
      props: ({
        data: {
          loading,
          currentUser,
          entry,
          subscribeToMore
        }
      }) => ({
        loading,
        currentUser,
        entry,
        subscribeToMore, // props.data.subscribeToMore is mapped to props.subscribeToMore
      }),
    });

    Else you could try and use this.props.data.subscribeToMore inside your componentWillRecieveProps functionality