I’m using graphql in a project, and I’m trying to understand the best practices when it comes to subscriptions. When should you use shouldResubscribe
? When shouldn’t you? I’ve been trying to find information about resubscribing, but I haven’t found that much information.
Specifically I’m trying to figure out whether it would be useful if you delete a post that’s contained within the subscribed data:
Currently I have:
export const GET_POST = gql`
query ($offset: Int!, $topic: String) {
getMessage(offset: $offset, limit: 20, topic: $topic) {
_id
sender {
senderId
}
post {
text
topic
dateSent
}
}
}
`;
export const CREATE_POST_MUTATION = gql`
mutation createPost($input: PostInput!) {
createPost(input: $input) {
post {
text
dateSent
}
}
}
`;
export const POST_SUBSCRIPTION = gql`
subscription {
_id
newPost {
sender {
senderId
}
post {
text
topic
dateSent
}
}
}
`;
export default function PostScreen() {
const [createPost,{ data: createPostDate, error:createPostError}] = useMutation(CREATE_POST_MUTATION)
async function createPostHandler (item: any) {
createPost({
variables: {
input: {
...
},
}})
}
const {data, loading, error, subscribeToMore, fetchMore: fetchMorePosts} = useQuery(GET_POST, {fetchPolicy: 'cache-and-network', variables: { offset: 0, topic: ''}})
useEffect(() => {
subscribeToMore({
document: POST_SUBSCRIPTION,
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData) {
return prev;
}
const updatePost = subscriptionData.data.updatePost.data;
const action = subscriptionData.data.updatePost.action;
if (action === "CREATE") {
return {
getPost:[updatePost, ...prev.getPost]
};
}
else if (action === "DELETE"){
getPost ...
},
onError: err => console.error(err)
});
}, []);
And I’m trying to figure out if it would be considered a good practice/ efficient to resubscribe if action === "DELETE"
.