Although it takes just a few lines of code to set up your NodeJS code to use GraphQL subscriptions, it does involve going through a few pages of docs. We've put together a few lines of boilerplate code to make it easy to get started.
-
Reference code: Refer to index.js for the boilerplate code that sets up a GraphQL subscription observable.
-
Dependencies: This uses the apollo link, subscription-transport-ws libraries.
npm install --save graphql graphql-tag apollo-link apollo-link-ws subscriptions-transport-ws ws
- Use hasura as a test backend:
- Create a table
author
with columnsid: integer
,name: text
- Set the GraphQL endpoint in
index.js
tohttps://HEROKU_APP_NAME.herokuoapp.com/v1alpha1/graphql
git clone https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate.git
cd nodejs-graphql-subscriptions-boilerplate
npm i
node index.js
- Now head to the Hasura console and insert an author with id: 1, name: honeysingh
- Changes will appear on your nodejs code
- Edit the row on the console
- Changes will appear on your nodejs code