Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #46 from vulcanize/db_triggers2
Browse files Browse the repository at this point in the history
triggers for tables to emit graphql notifications
  • Loading branch information
ramilexe authored Sep 23, 2020
2 parents 6632d00 + c1ee7a4 commit 43a1eef
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,34 @@ e.g.
This will stand up a Postgraphile server on the public and eth schemas- exposing GraphQL endpoints for all of the tables contained under those schemas.
All of their data can then be queried with standard [GraphQL](https://graphql.org) queries.

Also, graphql subscription is available. Using the following query you will be notified when new data come in.
For `topic` use table name.

e.g.

```
subscription MySubscription {
listen(topic: "receipt_cids") {
relatedNodeId
relatedNode {
nodeId
... on ReceiptCid {
id
contract
contractHash
logContracts
topic0S
topic1S
topic2S
topic3S
txId
}
}
}
}
```

* Use PG-IPFS to expose the raw IPLD data. More information on how to stand up an IPFS node on top
of Postgres can be found [here](./documentation/ipfs.md)

Expand Down
70 changes: 70 additions & 0 deletions db/migrations/00013_potgraphile_triggers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-- +goose Up
-- +goose StatementBegin
create function eth.graphql_subscription() returns trigger as $$
declare
table_name text = TG_ARGV[0];
attribute text = TG_ARGV[1];
id text;
begin
execute 'select $1.' || quote_ident(attribute)
using new
into id;
perform pg_notify('postgraphile:' || table_name,
json_build_object(
'__node__', json_build_array(
table_name,
id
)
)::text
);
return new;
end;
$$ language plpgsql;
-- +goose StatementEnd

CREATE TRIGGER header_cids_ai
after insert on eth.header_cids
for each row
execute procedure eth.graphql_subscription('header_cids', 'id');

CREATE TRIGGER receipt_cids_ai
after insert on eth.receipt_cids
for each row
execute procedure eth.graphql_subscription('receipt_cids', 'id');

CREATE TRIGGER state_accounts_ai
after insert on eth.state_accounts
for each row
execute procedure eth.graphql_subscription('state_accounts', 'id');

CREATE TRIGGER state_cids_ai
after insert on eth.state_cids
for each row
execute procedure eth.graphql_subscription('state_cids', 'id');

CREATE TRIGGER storage_cids_ai
after insert on eth.storage_cids
for each row
execute procedure eth.graphql_subscription('storage_cids', 'id');

CREATE TRIGGER transaction_cids_ai
after insert on eth.transaction_cids
for each row
execute procedure eth.graphql_subscription('transaction_cids', 'id');

CREATE TRIGGER uncle_cids_ai
after insert on eth.uncle_cids
for each row
execute procedure eth.graphql_subscription('uncle_cids', 'id');

-- +goose Down
drop trigger uncle_cids_ai on eth.uncle_cids;
drop trigger transaction_cids_ai on eth.transaction_cids;
drop trigger storage_cids_ai on eth.storage_cids;
drop trigger state_cids_ai on eth.state_cids;
drop trigger state_accounts_ai on eth.state_accounts;
drop trigger receipt_cids_ai on eth.receipt_cids;
drop trigger header_cids_ai on eth.header_cids;

drop function eth.graphql_subscription();

0 comments on commit 43a1eef

Please sign in to comment.