supertest-graphql
Advanced tools
Comparing version 1.1.3--canary.6.1973672715.0 to 1.1.3--canary.6.2004542793.0
@@ -14,4 +14,5 @@ import * as http from "http"; | ||
export * from "./SuperTestGraphQL"; | ||
export * from "./SuperTestWSGraphQL"; | ||
export * from "./types"; | ||
export { SuperTestGraphQL }; | ||
export default supertest; |
@@ -82,3 +82,4 @@ "use strict"; | ||
__exportStar(require("./SuperTestGraphQL"), exports); | ||
__exportStar(require("./SuperTestWSGraphQL"), exports); | ||
__exportStar(require("./types"), exports); | ||
exports.default = supertest; |
@@ -38,2 +38,3 @@ "use strict"; | ||
const subscriptions_transport_ws_1 = require("subscriptions-transport-ws"); | ||
const ws_1 = require("graphql-ws/lib/use/ws"); | ||
const graphql_1 = require("graphql"); | ||
@@ -44,2 +45,3 @@ const schema_1 = require("@graphql-tools/schema"); | ||
const _1 = __importStar(require("./")); | ||
const ws_2 = require("ws"); | ||
const typeDefs = (0, apollo_server_express_1.gql) ` | ||
@@ -60,3 +62,3 @@ type Query { | ||
const pubsub = new graphql_subscriptions_1.PubSub(); | ||
const initTestServer = ({ graphqlPath, } = {}) => __awaiter(void 0, void 0, void 0, function* () { | ||
const initTestServer = ({ graphqlPath = "/graphql", wsProtocol = "graphql-transport-ws", } = {}) => __awaiter(void 0, void 0, void 0, function* () { | ||
const app = (0, express_1.default)(); | ||
@@ -85,9 +87,21 @@ const httpServer = (0, http_1.createServer)(app); | ||
}); | ||
const subscriptionServer = subscriptions_transport_ws_1.SubscriptionServer.create({ | ||
schema, | ||
execute: graphql_1.execute, | ||
subscribe: graphql_1.subscribe, | ||
}, { | ||
server: httpServer, | ||
}); | ||
let closeWsMobile; | ||
if (wsProtocol === "graphql-ws") { | ||
const subscriptionServer = subscriptions_transport_ws_1.SubscriptionServer.create({ | ||
schema, | ||
execute: graphql_1.execute, | ||
subscribe: graphql_1.subscribe, | ||
}, { | ||
server: httpServer, | ||
}); | ||
closeWsMobile = () => __awaiter(void 0, void 0, void 0, function* () { return subscriptionServer.close(); }); | ||
} | ||
else { | ||
const server = new ws_2.WebSocketServer({ | ||
server: httpServer, | ||
path: graphqlPath, | ||
}); | ||
const serverCleanup = (0, ws_1.useServer)({ schema }, server); | ||
closeWsMobile = () => __awaiter(void 0, void 0, void 0, function* () { return yield serverCleanup.dispose(); }); | ||
} | ||
const server = new apollo_server_express_1.ApolloServer({ | ||
@@ -103,3 +117,3 @@ schema, | ||
return __awaiter(this, void 0, void 0, function* () { | ||
subscriptionServer.close(); | ||
closeWsMobile(); | ||
}); | ||
@@ -245,6 +259,31 @@ }, | ||
}); | ||
describe("test ws legacy", () => { | ||
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { | ||
server = yield initTestServer({ wsProtocol: "graphql-ws" }); | ||
yield new Promise((res) => server.listen(0, "localhost", () => res())); | ||
})); | ||
afterEach(() => __awaiter(void 0, void 0, void 0, function* () { | ||
yield _1.supertestWs.end(); | ||
server.close(); | ||
})); | ||
it("should work", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const sub = yield (0, _1.supertestWs)(server).protocol(_1.LEGACY_WEBSOCKET_PROTOCOL) | ||
.subscribe((0, apollo_server_express_1.gql) ` | ||
subscription { | ||
onHi | ||
} | ||
`); | ||
// there is no wayt to know if the subscription is active, | ||
// to avoid race conditions we need to wait a bit | ||
yield (0, delay_1.default)(200); | ||
pubsub.publish("ON_HI", {}); | ||
const res = yield sub.next().expectNoErrors(); | ||
expect(res.data).toEqual({ onHi: "Hi unknown" }); | ||
})); | ||
}); | ||
describe("test ws", () => { | ||
beforeEach((done) => { | ||
server.listen(0, "localhost", done); | ||
}); | ||
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { | ||
server = yield initTestServer(); | ||
yield new Promise((res) => server.listen(0, "localhost", () => res())); | ||
})); | ||
afterEach(() => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -251,0 +290,0 @@ yield _1.supertestWs.end(); |
@@ -0,5 +1,13 @@ | ||
/// <reference types="zen-observable" /> | ||
import { DocumentNode, ExecutionResult } from "graphql"; | ||
import { Disposable } from "graphql-ws"; | ||
import { ObjMap } from "graphql/jsutils/ObjMap"; | ||
import { Observable, Observer, SubscriptionClient } from "subscriptions-transport-ws"; | ||
import { Observable, Observer } from "zen-observable-ts"; | ||
import { Variables } from "./types"; | ||
export declare type WebSocketProtocol = "graphql-transport-ws" | "graphql-ws"; | ||
/** | ||
* The protocol implemented by the library `subscriptions-transport-ws` and | ||
* that is now considered legacy. | ||
*/ | ||
export declare const LEGACY_WEBSOCKET_PROTOCOL = "graphql-ws"; | ||
export declare class SuperTestExecutionNextResult<TData> implements PromiseLike<ExecutionResult<TData>> { | ||
@@ -16,8 +24,8 @@ private pop; | ||
} | ||
export declare class SuperTestExecutionStreamingResult<TData> implements Observable<ExecutionResult<TData>> { | ||
export declare class SuperTestExecutionStreamingResult<TData> { | ||
private client; | ||
private observable; | ||
private queue; | ||
constructor(client: SubscriptionClient, observable: Observable<ExecutionResult<TData>>); | ||
subscribe(observer: Observer<ExecutionResult<TData, ObjMap<unknown>>>): { | ||
constructor(client: Disposable, observable: Observable<ExecutionResult<TData>>); | ||
subscribe(observer: Observer<ExecutionResult<TData>>): { | ||
unsubscribe: () => void; | ||
@@ -57,2 +65,3 @@ }; | ||
private _path; | ||
private _protocol; | ||
private _connectionParams?; | ||
@@ -96,2 +105,7 @@ constructor(_hostname: string, _pool?: SuperTestExecutionStreamingResultPool | undefined); | ||
/** | ||
* Set the GraphQL WebSocket porotocol. | ||
* You can set the legacy protocol with the variable `LEGACY_WEBSOCKET_PROTOCOL`. | ||
*/ | ||
protocol(wsProtocol: WebSocketProtocol): this; | ||
/** | ||
* Set connection params. | ||
@@ -98,0 +112,0 @@ */ |
@@ -15,7 +15,14 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SuperTestExecutionStreamingResultPool = exports.SuperTestExecutionStreamingResult = exports.SuperTestExecutionNextResult = void 0; | ||
exports.SuperTestExecutionStreamingResultPool = exports.SuperTestExecutionStreamingResult = exports.SuperTestExecutionNextResult = exports.LEGACY_WEBSOCKET_PROTOCOL = void 0; | ||
const graphql_1 = require("graphql"); | ||
const graphql_ws_1 = require("graphql-ws"); | ||
const subscriptions_transport_ws_1 = require("subscriptions-transport-ws"); | ||
const ws_1 = __importDefault(require("ws")); | ||
const zen_observable_ts_1 = require("zen-observable-ts"); | ||
const utils_1 = require("./utils"); | ||
/** | ||
* The protocol implemented by the library `subscriptions-transport-ws` and | ||
* that is now considered legacy. | ||
*/ | ||
exports.LEGACY_WEBSOCKET_PROTOCOL = "graphql-ws"; | ||
class SuperTestExecutionNextResult { | ||
@@ -98,3 +105,3 @@ constructor(pop) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.client.close(); | ||
yield this.client.dispose(); | ||
}); | ||
@@ -124,2 +131,3 @@ } | ||
this._path = "/graphql"; | ||
this._protocol = "graphql-transport-ws"; | ||
} | ||
@@ -182,2 +190,10 @@ /** | ||
/** | ||
* Set the GraphQL WebSocket porotocol. | ||
* You can set the legacy protocol with the variable `LEGACY_WEBSOCKET_PROTOCOL`. | ||
*/ | ||
protocol(wsProtocol) { | ||
this._protocol = wsProtocol; | ||
return this; | ||
} | ||
/** | ||
* Set connection params. | ||
@@ -193,15 +209,11 @@ */ | ||
try { | ||
const client = yield new Promise((res, reject) => { | ||
const client = new subscriptions_transport_ws_1.SubscriptionClient(new URL(this._path, this._hostname).toString(), { | ||
connectionParams: this._connectionParams, | ||
connectionCallback: (error) => { | ||
if (error) { | ||
client.close(); | ||
return reject(error); | ||
} | ||
res(client); | ||
}, | ||
}, ws_1.default); | ||
}); | ||
const observable = client.request({ | ||
const url = new URL(this._path, this._hostname).toString(); | ||
const run = this._protocol === "graphql-ws" | ||
? runSubscriptionLegacy | ||
: runSubscription; | ||
if (!this._query) | ||
throw new Error("Missing a query"); | ||
const [observable, disposable] = yield run({ | ||
url, | ||
connectionParams: this._connectionParams, | ||
query: this._query, | ||
@@ -211,3 +223,3 @@ variables: this._variables, | ||
}); | ||
const streamingResult = new SuperTestExecutionStreamingResult(client, observable); | ||
const streamingResult = new SuperTestExecutionStreamingResult(disposable, observable); | ||
(_a = this._pool) === null || _a === void 0 ? void 0 : _a.add(streamingResult); | ||
@@ -228,1 +240,47 @@ 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 client = new subscriptions_transport_ws_1.SubscriptionClient(url, { | ||
connectionParams, | ||
connectionCallback: (error) => { | ||
if (error) { | ||
client.close(); | ||
return reject(error); | ||
} | ||
res(client); | ||
}, | ||
}, ws_1.default); | ||
}); | ||
const observable = client.request({ | ||
query: query, | ||
variables: variables, | ||
operationName: operationName, | ||
}); | ||
const disposable = { | ||
dispose: () => client.close(), | ||
}; | ||
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); | ||
}); | ||
return [observable, client]; | ||
}); |
{ | ||
"name": "supertest-graphql", | ||
"version": "1.1.3--canary.6.1973672715.0", | ||
"version": "1.1.3--canary.6.2004542793.0", | ||
"description": "Extends supertest to test a GraphQL endpoint", | ||
@@ -58,5 +58,7 @@ "main": "dist/index.js", | ||
"@types/supertest": "^2.0.11", | ||
"graphql-ws": "^5.6.3", | ||
"subscriptions-transport-ws": "^0.11.0", | ||
"supertest": "^6.1.6", | ||
"ws": "^8.4.0" | ||
"ws": "^8.4.0", | ||
"zen-observable-ts": "^1.1.0" | ||
}, | ||
@@ -63,0 +65,0 @@ "auto": { |
48501
1138
7
+ Addedgraphql-ws@^5.6.3
+ Addedzen-observable-ts@^1.1.0
+ Addedgraphql-ws@5.16.2(transitive)
+ Addedzen-observable@0.8.15(transitive)
+ Addedzen-observable-ts@1.2.5(transitive)