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

22 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2018-04-08 17:21:40 +12:00
# Extra for experts
2018-04-08 08:48:56 +12:00
2018-04-08 17:21:40 +12:00
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.
2018-04-08 14:34:46 +12:00
2018-04-09 13:14:15 +12:00
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](https://hackernoon.com/get-notified-of-user-signups-and-plan-changes-automatically-using-postgres-phoenix-pubsub-e67d061b04bc).
2018-04-08 14:34:46 +12:00
Things to look at:
2018-04-08 15:27:20 +12:00
* `priv/repo/migrations/20180408032823_broadcast_people_table_changes.exs`
* `lib/faces/gallery/event_listener.ex`
[Demo](http://localhost:4000)
Let's try manually inserting a row (via `psql faces_dev`):
```sql
2018-04-08 17:21:40 +12:00
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());
```
2018-04-20 14:52:41 +12:00
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](http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/).
Next, move on to `step-7`.