hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.17k stars 2.76k forks source link

How to subscribe to the delete event #5623

Open true-eye opened 4 years ago

true-eye commented 4 years ago

Hi, Team! I have a question about the delete subscription I'm building a simple chat application and I have a 'notes' table. When I delete a 'note' from one side, I would like to subscribe to it on the other side to delete it on the UI.

subscription {
    notes(order_by: { created_at: desc }, limit: 1) {

For the performance, I just subscribe to the latest But when I delete a note, it doesn't subscribe to the deleted note. Instead, it returns the previously created one.

Please help me Thanks in advance.

tirumaraiselvan commented 4 years ago

Subscriptions are "live queries" and will give you the result of the subscribed query, whenever it changes. In your case, you have subscribed to the result of "last created note" and hence after you delete a note you get the previously created one.

There are many ways to find out what notes got deleted through this query. One way to delete all notes which are > created_at (assuming only newer notes are deleted). Another way is use the subscription as a "notification" service and refetch the new list of notes that you want to display. Another way is to put a deleted column (i.e. a soft delete) instead of removing the entire row.

You can also talk to our community for such questions here: https://discord.com/invite/hasura