This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
graphql-lightning-talk/README.md
2018-04-20 14:52:41 +12:00

1.3 KiB

Extra for experts

Our existing subscription only triggers if someone else uses the importPerson mutation to trigger the subscription event - what if we want to let clients know whenever a row is added to our people table? Maybe it's added by another service, or something.

In this branch we're using PostgreSQL triggers and stored procedures to send notifications whenever a row is added to the people table and we're using Postgrex.Notifications to subscribe to them. A substantial portion of the code for this example comes from this hacker noon post.

Things to look at:

  • priv/repo/migrations/20180408032823_broadcast_people_table_changes.exs
  • lib/faces/gallery/event_listener.ex

Demo

Let's try manually inserting a row (via psql faces_dev):

INSERT INTO "people" ("username", "name", "location", "avatar_url", "inserted_at", "updated_at") VALUES ('pupper', 'pupper', 'the bath', 'https://media.giphy.com/media/3o6Zt9ved5rCuidNlK/giphy.gif', NOW(), NOW());

I want to point out that this is a really bad idea, mostly for the same reasons that Rails ActiveRecord callbacks are a bad idea.

Next, move on to step-7.