@graphql-tools/executor-graphql-ws
Version:
A set of utils for faster development of GraphQL tools
40 lines (39 loc) • 1.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildGraphQLWSExecutor = void 0;
const tslib_1 = require("tslib");
const repeater_1 = require("@repeaterjs/repeater");
const graphql_1 = require("graphql");
const graphql_ws_1 = require("graphql-ws");
const isomorphic_ws_1 = tslib_1.__importDefault(require("isomorphic-ws"));
function buildGraphQLWSExecutor(url, webSocketImpl = isomorphic_ws_1.default, connectionParams) {
const graphqlWSClient = (0, graphql_ws_1.createClient)({
url,
webSocketImpl,
connectionParams,
lazy: true,
});
return function GraphQLWSExecutor({ document, variables, operationName, extensions, }) {
const query = (0, graphql_1.print)(document);
return new repeater_1.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);
});
};
}
exports.buildGraphQLWSExecutor = buildGraphQLWSExecutor;
;