@graphql-tools/executor-graphql-ws
Version:
A set of utils for faster development of GraphQL tools
35 lines (34 loc) • 1.12 kB
JavaScript
import { Repeater } from '@repeaterjs/repeater';
import { print } from 'graphql';
import { createClient } from 'graphql-ws';
import WebSocket from 'isomorphic-ws';
export function buildGraphQLWSExecutor(url, webSocketImpl = WebSocket, connectionParams) {
const graphqlWSClient = createClient({
url,
webSocketImpl,
connectionParams,
lazy: true,
});
return function GraphQLWSExecutor({ document, variables, operationName, extensions, }) {
const query = print(document);
return new Repeater(function repeaterExecutor(push, stop) {
const unsubscribe = graphqlWSClient.subscribe({
query,
variables,
operationName,
extensions,
}, {
next(data) {
return push(data);
},
error(error) {
return stop(error);
},
complete() {
return stop();
},
});
return stop.finally(unsubscribe);
});
};
}