howtographql / react-apollo

MIT License
716 stars 320 forks source link

TypeError: Cannot read property 'id' of null #96

Open ilyas-shah opened 4 years ago

ilyas-shah commented 4 years ago

Got an error when subscribing to new links.


updateQuery: (prev, { subscriptionData }) => {
        if (!subscriptionData.data) return prev;

        const newLink = subscriptionData.data.newLink;

        **const exists = prev.feed.links.find((link) => link.id === newLink.id);**

        if (exists) return prev;

        return Object.assign({}, prev, {
          feed: [newLink, ...prev.feed.links],
          count: prev.feed.links.length + 1,
          __typename: prev.feed.__typename,
        });
      },
    });

when the "linkList" page loads, the value of newLink is null. So rather than proceeding further, we return the prev/current list.


updateQuery: (prev, { subscriptionData }) => {

        if (!subscriptionData.data) return prev;

        const newLink = subscriptionData.data.newLink;

       **if (!newLink) return prev;**

        const exists = prev.feed.links.find((link) => link.id === newLink.id);

        if (exists) return prev;

        return Object.assign({}, prev, {
          feed: [newLink, ...prev.feed.links],
          count: prev.feed.links.length + 1,
          __typename: prev.feed.__typename,
        });
      },
    });

ilyas-shah commented 4 years ago

I also found the updateQuery function is called 2 times when someone posts a link. First time with the prev data and second without prev data.