Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate
Boilerplate to setup GraphQL subscriptions in your nodejs code
https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate
boilerplate graphql graphql-subscriptions nodejs
Last synced: 3 days ago
JSON representation
Boilerplate to setup GraphQL subscriptions in your nodejs code
- Host: GitHub
- URL: https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate
- Owner: hasura
- License: mit
- Created: 2018-07-25T17:03:33.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-12T19:39:17.000Z (about 5 years ago)
- Last Synced: 2024-04-14T11:41:35.241Z (7 months ago)
- Topics: boilerplate, graphql, graphql-subscriptions, nodejs
- Language: JavaScript
- Homepage: https://hasura.io
- Size: 18.6 KB
- Stars: 79
- Watchers: 5
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Boilerplate to test GraphQL subscriptions using nodejs
Although it takes just a [few lines of code](./index.js#L1-L21) to set up your NodeJS code to use GraphQL subscriptions, it does involve going through a few pages of docs. Here's a few lines of boilerplate you can use to get started quickly (copy-paste 🤘).## Usage:
- Reference code:
Refer to [index.js](./index.js#L1-L21) for the boilerplate code that sets up a GraphQL subscription observable.#### Step 1: Setup the subscription client
This is all the code you need
```javascriptconst { execute } = require('apollo-link');
const { WebSocketLink } = require('apollo-link-ws');
const { SubscriptionClient } = require('subscriptions-transport-ws');
const ws = require('ws');const getWsClient = function(wsurl) {
const client = new SubscriptionClient(
wsurl, {reconnect: true}, ws
);
return client;
};const createSubscriptionObservable = (wsurl, query, variables) => {
const link = new WebSocketLink(getWsClient(wsurl));
return execute(link, {query: query, variables: variables});
};
```#### Step 2: Use it
```javascript
const gql = require('graphql-tag');
// A subscription query to get changes for author with parametrised id
// using $id as a query variable
const SUBSCRIBE_QUERY = gql`
subscription liveAuthor($id: Int!) {
author (where: {id: {_eq: $id}}) {
id
name
}
}
`;const subscriptionClient = createSubscriptionObservable(
'https://test-gql-sub.herokuapp.com/v1alpha1/graphql', // GraphQL endpoint
SUBSCRIBE_QUERY, // Subscription query
{id: 1} // Query variables
);
var consumer = subscriptionClient.subscribe(eventData => {
// Do something on receipt of the event
console.log("Received event: ");
console.log(JSON.stringify(eventData, null, 2));
}, (err) => {
console.log('Err');
console.log(err);
});
```#### Dependencies:
This uses the [apollo link](https://github.com/apollographql/apollo-link), [subscription-transport-ws](https://github.com/apollographql/subscriptions-transport-ws) libraries.
```
npm install --save graphql graphql-tag apollo-link apollo-link-ws subscriptions-transport-ws ws
```--------------------
## Try it out:
- Use [hasura](https://hasura.io) as a test backend: [![Deploy to Heroku](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/hasura/graphql-engine-heroku)
- Create a table `author` with columns `id: integer`, `name: text`
- Set the GraphQL endpoint in `index.js` to `https://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