Possible to subscribe to DELETE events when using graphql-yoga?


#1

I’m using the subscription example from the prisma/prisma-examples repo and changed the subscribe function to also fire when a post is deleted.

  Subscription: {
    posts: {
      subscribe: async (parent, args, context) => {
        return context.prisma.$subscribe
          .post()
          .node()
      },
      resolve: payload => {
        return payload
      },
    },
},

Now when a post is deleted the subscription fires, but the payload is null so the client only gets null and thus can’t determine which post was deleted.

Is there a way to be able to return the deleted post? Or any best practices for dealing with this kind of situation where the client needs to know when a specific post was deleted?

Thanks in advance!


#2

You can specify you want the previous data (which in your case will be the deleted data).

subscription {
 posts {
  previousValues {
   title
  }
 }
}

#3

Didn’t now about this, will give it a try.
Thank you!