I'm trying to use GraphQL Apollo
in API subscriptions
for faster and realtime updates in my React
Project. I'm using "subscriptions-transport-ws": "^0.8.2"
. But delete and update is throwing error. I'm quite new to react and JS and am not able to get this work. I tried searching for a solution but no luck yet!
Here's the basic front-end layout:
And here's the related react component code for graphql subscriptions
:
componentWillReceiveProps(nextProps) {
if (!this.subscription && !nextProps.data.loading) {
let { subscribeToMore } = this.props.data
this.subscription = [subscribeToMore(
{
document: postDeleted,
updateQuery: (previousResult, { subscriptionData }) => {
const delPost = subscriptionData.data.postDeleted;
const mainData = previousResult.Posts;
let post = mainData.find(post => post.id == delPost.id);
let updatedList = mainData.splice(post, 1);
return updatedList;
},
}),
subscribeToMore(
{
document: postUpdated,
updateQuery: (previousResult, { subscriptionData }) => {
previousResult.Posts = previousResult.Posts.map((p) => {
if(p.id === subscriptionData.data.postEdited.id) {
return subscriptionData.data.postEdited
} else {
return p;
}
});
return previousResult;
},
}
)]
}
}
But I'm facing following errors:
Delete Button Error:
TypeError: Cannot add/remove sealed array elements
And Update Button Error:
Cannot assign to read only property 'Posts' of object '#<Object>'
Can someone please help me get realtime updates in the component with graphql subscriptions?