Source for a talk I gave about GraphQL and Elixir/Phoenix.
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.
Find a file
2018-04-20 14:52:41 +12:00
.vscode Client-side subscriptions via Apollo work. 2018-04-08 15:26:22 +12:00
assets Client-side subscriptions via Apollo work. 2018-04-08 15:26:22 +12:00
config Use an access token because it avoids the rate limit. 2018-04-10 18:18:38 +12:00
lib Use an access token because it avoids the rate limit. 2018-04-10 18:18:38 +12:00
priv Add postgres database triggers and use them to notify subscribers. 2018-04-08 16:18:48 +12:00
test Add face importing from github. 2018-04-08 10:06:11 +12:00
.gitignore Use an access token because it avoids the rate limit. 2018-04-10 18:18:38 +12:00
go.sh Add go.sh 2018-04-10 12:47:04 +12:00
mix.exs Add absinthe-socket-link and send all GraphQL via a Phoenix channel. 2018-04-08 15:00:11 +12:00
mix.lock Add absinthe-socket-link and send all GraphQL via a Phoenix channel. 2018-04-08 15:00:11 +12:00
README.md Add note to readme acout callbacks. 2018-04-20 14:52:41 +12:00

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.