Skip to content

Commit b55d9ff

Browse files
committed
section 3 setup and initial trigger migrations
1 parent cce5e91 commit b55d9ff

7 files changed

+142
-6
lines changed

client/src/index.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,41 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
5-
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
5+
import {
6+
ApolloClient,
7+
InMemoryCache,
8+
ApolloProvider,
9+
split,
10+
HttpLink,
11+
} from '@apollo/client';
12+
import { getMainDefinition } from '@apollo/client/utilities';
13+
import { WebSocketLink } from '@apollo/client/link/ws';
614

7-
console.log(process.env);
15+
const httpLink = new HttpLink({
16+
uri: 'http://localhost:4000/graphql',
17+
});
18+
19+
const wsLink = new WebSocketLink({
20+
uri: 'ws://localhost:4000/graphql',
21+
options: {
22+
reconnect: true,
23+
},
24+
});
25+
26+
const splitLink = split(
27+
({ query }) => {
28+
const definition = getMainDefinition(query);
29+
return (
30+
definition.kind === 'OperationDefinition' &&
31+
definition.operation === 'subscription'
32+
);
33+
},
34+
wsLink,
35+
httpLink
36+
);
837

938
const client = new ApolloClient({
10-
uri: 'http://127.0.0.1:3333/graphql' /* variables from toplevel project .env */,
39+
link: splitLink,
1140
cache: new InMemoryCache({
1241
typePolicies: {
1342
Query: {
@@ -20,7 +49,7 @@ const client = new ApolloClient({
2049
},
2150
},
2251
},
23-
}) /*NOTHING TO SEEEE!!{ dataIdFromObject: (object) => object.nodeId }*/,
52+
}),
2453
});
2554

2655
ReactDOM.render(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var dbm;
4+
var type;
5+
var seed;
6+
var fs = require('fs');
7+
var path = require('path');
8+
var Promise;
9+
10+
/**
11+
* We receive the dbmigrate dependency from dbmigrate initially.
12+
* This enables us to not have to rely on NODE_PATH.
13+
*/
14+
exports.setup = function(options, seedLink) {
15+
dbm = options.dbmigrate;
16+
type = dbm.dataType;
17+
seed = seedLink;
18+
Promise = options.Promise;
19+
};
20+
21+
exports.up = function(db) {
22+
var filePath = path.join(__dirname, 'sqls', '20211014012149-trigger-function-todo-up.sql');
23+
return new Promise( function( resolve, reject ) {
24+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
25+
if (err) return reject(err);
26+
console.log('received data: ' + data);
27+
28+
resolve(data);
29+
});
30+
})
31+
.then(function(data) {
32+
return db.runSql(data);
33+
});
34+
};
35+
36+
exports.down = function(db) {
37+
var filePath = path.join(__dirname, 'sqls', '20211014012149-trigger-function-todo-down.sql');
38+
return new Promise( function( resolve, reject ) {
39+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
40+
if (err) return reject(err);
41+
console.log('received data: ' + data);
42+
43+
resolve(data);
44+
});
45+
})
46+
.then(function(data) {
47+
return db.runSql(data);
48+
});
49+
};
50+
51+
exports._meta = {
52+
"version": 1
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TRIGGER IF EXISTS trigger_todo ON todo_public.todo;
2+
DROP FUNCTION IF EXISTS notify_todo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE OR REPLACE FUNCTION notify_todo()
2+
RETURNS trigger AS $$
3+
BEGIN
4+
PERFORM pg_notify(
5+
'postgraphile:todo',
6+
json_build_object('__node__', json_build_array('todos', NEW.id))::text);
7+
RETURN NEW;
8+
END;
9+
$$ LANGUAGE plpgsql;
10+
11+
CREATE TRIGGER trigger_todo
12+
AFTER INSERT
13+
ON todo_public.todo
14+
FOR EACH ROW
15+
EXECUTE PROCEDURE notify_todo();

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"license": "MIT",
99
"dependencies": {
1010
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
11+
"@graphile/pg-pubsub": "^4.12.1",
1112
"cors": "^2.8.5",
1213
"db-migrate": "^1.0.0-beta.16",
1314
"db-migrate-pg": "^1.2.2",

server/postgraphile.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const { postgraphile } = require('postgraphile');
1+
const { postgraphile, makePluginHook } = require('postgraphile');
22
const PgSimplifyInflectorPlugin = require('@graphile-contrib/pg-simplify-inflector');
3+
const { default: PgPubsub } = require('@graphile/pg-pubsub');
4+
5+
const pluginHook = makePluginHook([PgPubsub]);
36

47
const {
58
POSTGRES_DB,
@@ -26,5 +29,8 @@ module.exports = postgraphile(
2629
simpleCollections: 'only',
2730
graphileBuildOptions: { pgOmitListSuffix: true },
2831
classicIds: true,
32+
pluginHook,
33+
subscriptions: true,
34+
simpleSubscriptions: true,
2935
}
3036
);

yarn.lock

+31-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
dependencies:
1515
tslib "^2.0.1"
1616

17+
"@graphile/pg-pubsub@^4.12.1":
18+
version "4.12.1"
19+
resolved "https://registry.yarnpkg.com/@graphile/pg-pubsub/-/pg-pubsub-4.12.1.tgz#c47e1bafde6227f53304ea9ce70558c76fd0578a"
20+
integrity sha512-0HR7HN/CZcwkw+pLrmcxGcEkk7U9TvB+rhknjoN8M7agMeYNP/yMgJ2L+4Rgcsnu8a+vszx+AYNFdkGtKE/edw==
21+
dependencies:
22+
"@types/debug" "^4.1.4"
23+
"@types/pg" ">=6 <9"
24+
debug "^4.1.1"
25+
graphql-subscriptions "^1.1.0"
26+
tslib "^2.0.1"
27+
1728
"@sindresorhus/is@^0.14.0":
1829
version "0.14.0"
1930
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"
@@ -26,6 +37,13 @@
2637
dependencies:
2738
defer-to-connect "^1.0.1"
2839

40+
"@types/debug@^4.1.4":
41+
version "4.1.7"
42+
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.7.tgz#7cc0ea761509124709b8b2d1090d8f6c17aadb82"
43+
integrity sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==
44+
dependencies:
45+
"@types/ms" "*"
46+
2947
"@types/json5@^0.0.30":
3048
version "0.0.30"
3149
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.30.tgz#44cb52f32a809734ca562e685c6473b5754a7818"
@@ -38,6 +56,11 @@
3856
dependencies:
3957
"@types/node" "*"
4058

59+
"@types/ms@*":
60+
version "0.7.31"
61+
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
62+
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
63+
4164
"@types/node@*":
4265
version "16.10.3"
4366
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.10.3.tgz#7a8f2838603ea314d1d22bb3171d899e15c57bd5"
@@ -737,6 +760,13 @@ [email protected]:
737760
debug "^4.1.1"
738761
tslib "^2.0.1"
739762

763+
graphql-subscriptions@^1.1.0:
764+
version "1.2.1"
765+
resolved "https://registry.yarnpkg.com/graphql-subscriptions/-/graphql-subscriptions-1.2.1.tgz#2142b2d729661ddf967b7388f7cf1dd4cf2e061d"
766+
integrity sha512-95yD/tKi24q8xYa7Q9rhQN16AYj5wPbrb8tmHGM3WRc9EBmWrG/0kkMl+tQG8wcEuE9ibR4zyOM31p5Sdr2v4g==
767+
dependencies:
768+
iterall "^1.3.0"
769+
740770
graphql-ws@^5.1.2:
741771
version "5.5.0"
742772
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.5.0.tgz#79f10248d23d104369eaef93acb9f887276a2c42"
@@ -935,7 +965,7 @@ is-yarn-global@^0.3.0:
935965
resolved "https://registry.yarnpkg.com/is-yarn-global/-/is-yarn-global-0.3.0.tgz#d502d3382590ea3004893746754c89139973e232"
936966
integrity sha512-VjSeb/lHmkoyd8ryPVIKvOCn4D1koMqY+vqyjjUfc3xyKtP4dYOxM44sZrnqQSzSds3xyOrUTLTC9LVCVgLngw==
937967

938-
iterall@^1.0.2, iterall@^1.2.1, iterall@^1.2.2:
968+
iterall@^1.0.2, iterall@^1.2.1, iterall@^1.2.2, iterall@^1.3.0:
939969
version "1.3.0"
940970
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
941971
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==

0 commit comments

Comments
 (0)