Skip to content

Commit

Permalink
Enable query subscriptions (#202)
Browse files Browse the repository at this point in the history
* Add command for starting graphql with subscriptions

* Connect to Dockerfile

* Add sample script to subscribe to local query-node

* Use yarn instead of npm

* Rename docker container name to api

* Refer query-node as api

* Update graphql api commands

* Spin new query-node for subscriptions

* Set image name as query-node

* Reorder command when starting processor
  • Loading branch information
saboonikhil committed Oct 13, 2022
1 parent 923441e commit 4c87c65
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -34,4 +34,4 @@ CMD ["node", "lib/processor.js"]


FROM squid AS query-node
CMD ["yarn", "squid-graphql-server"]
CMD ["yarn", "api:start"]
11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -6,6 +6,9 @@
"author": "",
"license": "ISC",
"scripts": {
"api:dev:start": "sh scripts/api.sh dev start",
"api:start": "squid-graphql-server --subscriptions",
"api:stop": "sh scripts/api.sh stop",
"archive:start": "./scripts/archive.sh local up",
"archive:stop": "./scripts/archive.sh local down",
"build": "rm -rf lib && tsc",
Expand All @@ -17,12 +20,10 @@
"processor:dev:resume": "./scripts/processor.sh dev resume",
"processor:dev:start": "./scripts/processor.sh dev start",
"processor:stop": "./scripts/processor.sh stop",
"query-node:dev:start": "./scripts/query-node.sh dev start",
"query-node:stop": "./scripts/query-node.sh stop",
"redis:up": "docker-compose up -d redis",
"squid:start": "yarn archive:start && sh scripts/processor.sh local start && sh scripts/query-node.sh local start",
"squid:mac:start": "yarn archive:start && sh scripts/processor.sh mlocal start && sh scripts/query-node.sh mlocal start",
"squid:stop": "yarn archive:stop && yarn query-node:stop && docker-compose down && yarn processor:stop",
"squid:start": "yarn archive:start && sh scripts/processor.sh local start && sh scripts/api.sh local start",
"squid:mac:start": "yarn archive:start && sh scripts/processor.sh mlocal start && sh scripts/api.sh mlocal start",
"squid:stop": "yarn archive:stop && yarn api:stop && docker-compose down && yarn processor:stop",
"typegen": "squid-substrate-typegen typegen.json"
},
"dependencies": {
Expand Down
23 changes: 12 additions & 11 deletions scripts/query-node.sh → scripts/api.sh
@@ -1,39 +1,40 @@
#!/bin/sh

__usage="
Usage: ./scripts/query-node.sh <first> <second>
Usage: ./scripts/api.sh <first> <second>
Options for <first>:
local Query data on local chain
mlocal Query local chain running on mac
dev Query data on battery-station (dev chain)
stop Stop the already running query-node
stop Stop the already running api
Options for <second>:
start Build & start query-node
start Build & start api
"

if [ "$1" = "stop" ]; then
echo "Stopping query-node..."
docker stop zeitgeist-query-node
echo "Stopping api..."
docker stop api
exit
elif [ "$2" = "start" ]; then
echo "Building query-node..."
echo "Building api..."
docker build . --target query-node -t query-node
echo "Starting query-node..."
echo "Starting api..."
else
echo "$__usage"
exit
fi

if [ "$1" = "local" ]; then
docker run -d --network=host --rm -e NODE_ENV=local --env-file=.env.local --name=zeitgeist-query-node query-node
docker run -d --network=host --rm -e NODE_ENV=local --env-file=.env.local --name=api query-node
elif [ "$1" = "mlocal" ]; then
docker run -d -p 4350:4350 --rm -e NODE_ENV=mlocal --env-file=.env.mlocal --name=zeitgeist-query-node query-node
docker run -d -p 4350:4350 --rm -e NODE_ENV=mlocal --env-file=.env.mlocal --name=api query-node
elif [ "$1" = "dev" ]; then
docker run -d -p 4350:4350 --rm -e NODE_ENV=dev --env-file=.env.dev --name=zeitgeist-query-node query-node
docker run -d -p 4350:4350 --rm -e NODE_ENV=dev --env-file=.env.dev --name=api query-node
elif [ "$1" = "d" ] || [ "$1" = "t1" ] || [ "$1" = "t2" ] || [ "$1" = "m1" ] || [ "$1" = "m2" ]; then
docker run -d --network=host --rm -e NODE_ENV=$1 --env-file=.env.$1 --name=zeitgeist-query-node query-node
docker run -d --network=host --rm -e NODE_ENV=$1 --env-file=.env.$1 --name=api query-node
docker run -d --network=host --rm -e GQL_PORT=4000 -e NODE_ENV=$1 --env-file=.env.$1 --name=sub-api query-node
else
echo "$__usage"
fi
9 changes: 5 additions & 4 deletions scripts/processor.sh
Expand Up @@ -21,9 +21,9 @@ if [ "$1" = "stop" ]; then
exit
elif [ "$2" = "start" ]; then
echo "Starting services..."
yarn db:up && yarn redis:up && yarn migration:apply
echo "Building processor..."
yarn db:up && yarn redis:up
docker build . --target processor -t processor
yarn migration:apply
echo "Starting processor..."
elif [ "$2" = "resume" ]; then
echo "Building processor..."
Expand All @@ -32,8 +32,9 @@ elif [ "$2" = "resume" ]; then
elif [ "$2" = "restart" ]; then
echo "Stopping processor..."
docker stop zeitgeist-processor
echo "Stopping query-node..."
docker stop zeitgeist-query-node
echo "Stopping api..."
docker stop api
docker stop sub-api
echo "Stopping services..."
docker-compose down
echo "Starting services..."
Expand Down
35 changes: 35 additions & 0 deletions scripts/sub-client.js
@@ -0,0 +1,35 @@
const WebSocket = require('ws');
const { createClient } = require('graphql-ws');

const port = process.env.GQL_PORT || 4350
const host = process.env.GQL_HOST || 'localhost'
const proto = process.env.GQL_PROTO || 'ws'


const client = createClient({
webSocketImpl: WebSocket,
url: `${proto}://${host}:${port}/graphql`,
});

client.subscribe(
{
query: `
subscription {
historicalAccountBalances(limit: 5, orderBy: id_DESC) {
accountId
}
}
`,
},
{
next: (data) => {
console.log(`New transfers: ${JSON.stringify(data)}`);
},
error: (error) => {
console.error('error', error);
},
complete: () => {
console.log('done!');
},
}
);

0 comments on commit 4c87c65

Please sign in to comment.