supertest-graphql
Advanced tools
Comparing version 1.1.3--canary.6.2004793411.0 to 1.1.3--canary.6.2005663092.0
@@ -1,6 +0,5 @@ | ||
/// <reference types="zen-observable" /> | ||
import { DocumentNode, ExecutionResult } from "graphql"; | ||
import { Disposable } from "graphql-ws"; | ||
import { ObjMap } from "graphql/jsutils/ObjMap"; | ||
import { Observable, Observer } from "zen-observable-ts"; | ||
import { Subscriber } from "zen-observable-ts"; | ||
import { Variables } from "./types"; | ||
@@ -26,8 +25,4 @@ export declare type WebSocketProtocol = "graphql-transport-ws" | "graphql-ws"; | ||
private client; | ||
private observable; | ||
private queue; | ||
constructor(client: Disposable, observable: Observable<ExecutionResult<TData>>); | ||
subscribe(observer: Observer<ExecutionResult<TData>>): { | ||
unsubscribe: () => void; | ||
}; | ||
constructor(client: Disposable, subscriber: Subscriber<ExecutionResult<TData>>); | ||
/** | ||
@@ -34,0 +29,0 @@ * Get the next result that the operation is emitting. |
@@ -16,3 +16,2 @@ "use strict"; | ||
exports.SuperTestExecutionStreamingResultPool = exports.SuperTestExecutionStreamingResult = exports.SuperTestExecutionNextResult = exports.LEGACY_WEBSOCKET_PROTOCOL = void 0; | ||
const delay_1 = __importDefault(require("delay")); | ||
const graphql_1 = require("graphql"); | ||
@@ -22,3 +21,2 @@ const graphql_ws_1 = require("graphql-ws"); | ||
const ws_1 = __importDefault(require("ws")); | ||
const zen_observable_ts_1 = require("zen-observable-ts"); | ||
const utils_1 = require("./utils"); | ||
@@ -64,7 +62,6 @@ /** | ||
class SuperTestExecutionStreamingResult { | ||
constructor(client, observable) { | ||
constructor(client, subscriber) { | ||
this.client = client; | ||
this.observable = observable; | ||
this.queue = new utils_1.BlockingQueue(); | ||
this.observable.subscribe({ | ||
subscriber({ | ||
next: (res) => this.queue.push(res), | ||
@@ -77,7 +74,5 @@ complete: () => { | ||
}, | ||
closed: false, | ||
}); | ||
} | ||
subscribe(observer) { | ||
return this.observable.subscribe(observer); | ||
} | ||
/** | ||
@@ -211,15 +206,14 @@ * Get the next result that the operation is emitting. | ||
const url = new URL(this._path, this._hostname).toString(); | ||
const run = this._protocol === "graphql-ws" | ||
? runSubscriptionLegacy | ||
: runSubscription; | ||
const connect = this._protocol === "graphql-ws" | ||
? connectAndAdaptLegacyClient | ||
: connectClient; | ||
const client = yield connect(url, this._connectionParams); | ||
if (!this._query) | ||
throw new Error("Missing a query"); | ||
const [observable, disposable] = yield run({ | ||
url, | ||
connectionParams: this._connectionParams, | ||
query: this._query, | ||
const query = this._query; | ||
const streamingResult = new SuperTestExecutionStreamingResult(client, (observer) => client.subscribe({ | ||
query, | ||
variables: this._variables, | ||
operationName: this._operationName, | ||
}); | ||
const streamingResult = new SuperTestExecutionStreamingResult(disposable, observable); | ||
}, observer)); | ||
(_a = this._pool) === null || _a === void 0 ? void 0 : _a.add(streamingResult); | ||
@@ -240,4 +234,16 @@ if (onfulfilled) | ||
exports.default = SuperTestWSGraphQL; | ||
const runSubscriptionLegacy = ({ url, query, connectionParams, variables, operationName, }) => __awaiter(void 0, void 0, void 0, function* () { | ||
const client = yield new Promise((res, reject) => { | ||
const connectClient = (url, connectionParams) => __awaiter(void 0, void 0, void 0, function* () { | ||
return yield new Promise((res, reject) => { | ||
const client = (0, graphql_ws_1.createClient)({ | ||
url, | ||
connectionParams, | ||
lazy: false, | ||
onNonLazyError: (error) => reject(error), | ||
webSocketImpl: ws_1.default, | ||
}); | ||
client.on("connected", () => res(client)); | ||
}); | ||
}); | ||
const connectAndAdaptLegacyClient = (url, connectionParams) => __awaiter(void 0, void 0, void 0, function* () { | ||
const legacyClient = yield new Promise((res, reject) => { | ||
const client = new subscriptions_transport_ws_1.SubscriptionClient(url, { | ||
@@ -254,41 +260,12 @@ connectionParams, | ||
}); | ||
const observable = client.request({ | ||
query: query, | ||
variables: variables, | ||
operationName: operationName, | ||
}); | ||
const disposable = { | ||
dispose: () => client.close(), | ||
return { | ||
dispose: () => { | ||
legacyClient.close(); | ||
}, | ||
subscribe: (args, observer) => { | ||
// @ts-expect-error not exact but fine | ||
const { unsubscribe } = legacyClient.request(args).subscribe(observer); | ||
return unsubscribe; | ||
}, | ||
}; | ||
return [ | ||
observable, | ||
disposable, | ||
]; | ||
}); | ||
const runSubscription = ({ url, query, connectionParams, variables, operationName, }) => __awaiter(void 0, void 0, void 0, function* () { | ||
const client = yield new Promise((res, reject) => { | ||
const client = (0, graphql_ws_1.createClient)({ | ||
url, | ||
connectionParams, | ||
lazy: false, | ||
onNonLazyError: (error) => reject(error), | ||
webSocketImpl: ws_1.default, | ||
}); | ||
client.on("connected", () => res(client)); | ||
}); | ||
const observable = new zen_observable_ts_1.Observable((observer) => { | ||
client.subscribe({ | ||
query, | ||
variables, | ||
operationName, | ||
}, observer); | ||
}); | ||
const disposable = { | ||
dispose: () => __awaiter(void 0, void 0, void 0, function* () { | ||
yield client.dispose(); | ||
// dispose rely on emiter to close subsctiptions | ||
yield (0, delay_1.default)(5); | ||
}), | ||
}; | ||
return [observable, disposable]; | ||
}); |
{ | ||
"name": "supertest-graphql", | ||
"version": "1.1.3--canary.6.2004793411.0", | ||
"version": "1.1.3--canary.6.2005663092.0", | ||
"description": "Extends supertest to test a GraphQL endpoint", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
47882
1118