-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (47 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/***** Setup a GraphQL subscription observable ******************************/
const { 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;
};
// wsurl: GraphQL endpoint
// query: GraphQL query (use gql`` from the 'graphql-tag' library)
// variables: Query variables object
const createSubscriptionObservable = (wsurl, query, variables) => {
const link = new WebSocketLink(getWsClient(wsurl));
return execute(link, {query: query, variables: variables});
};
/*****************************************************************************/
/*********** Sample usage from your nodejs code ******************************/
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
}
}
`;
function main() {
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);
});
}
main();