Skip to content

Instantly share code, notes, and snippets.

@JeffML
Last active December 29, 2018 23:55
Show Gist options
  • Save JeffML/775eb1718d01bc60075141b2fc16bec6 to your computer and use it in GitHub Desktop.
Save JeffML/775eb1718d01bc60075141b2fc16bec6 to your computer and use it in GitHub Desktop.
/* eslint-env mocha */
/* eslint-disable import/no-extraneous-dependencies */
// see https://www.apollographql.com/docs/link/index.html#standalone
import fetch from 'node-fetch';
import { execute, makePromise } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import ws from 'ws';
import { HttpLink } from 'apollo-link-http';
import gql from 'graphql-tag';
const uri = 'http://localhost:3031/graphql';
const wsUri = 'ws://localhost:3031/graphql';
const link = new HttpLink({ uri, fetch });
const wsClient = new SubscriptionClient(wsUri, { reconnect: true }, ws);
const wsLink = new WebSocketLink(wsClient);
const doQuery = (query) => {
const operation = {
query: gql`${query}`,
};
return makePromise(execute(link, operation));
};
const subscribe = (query, handlers) => {
const operation = {
query: gql`${query}`,
};
return execute(wsLink, operation).subscribe(handlers);
// return execute(link, operation).subscribe(handlers);
};
export default doQuery;
export { subscribe };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment