@bigtest/client
Advanced tools
Comparing version 0.2.0-c3de7466 to 0.2.0-c7597659
@@ -9,7 +9,9 @@ # @bigtest/client | ||
connect to a bigtest orchestrator | ||
- f5092973: Add support for variables in GraphQL queries | ||
### Patch Changes | ||
- 62252502: Provide a nice error message when running tests without a server | ||
- 83153e3f: Upgrade effection dependencies to latest versions, upgrade to new style of subscriptions | ||
- Updated dependencies [83153e3f] | ||
- @bigtest/effection@0.5.3 |
import { Operation } from 'effection'; | ||
import { Mailbox } from '@bigtest/effection'; | ||
import { Variables } from './protocol'; | ||
export declare class Client { | ||
@@ -7,7 +8,7 @@ private socket; | ||
static create(url: string): Operation<Client>; | ||
query(source: string): Operation; | ||
mutation(source: string): Operation; | ||
liveQuery(source: string): Operation<Mailbox>; | ||
subscription(source: string): Operation<Mailbox>; | ||
query(source: string, variables?: Variables): Operation; | ||
mutation(source: string, variables?: Variables): Operation; | ||
liveQuery(source: string, variables?: Variables): Operation<Mailbox>; | ||
subscription(source: string, variables?: Variables): Operation<Mailbox>; | ||
private send; | ||
} |
@@ -9,2 +9,3 @@ "use strict"; | ||
const protocol_1 = require("./protocol"); | ||
const errors_1 = require("./errors"); | ||
let responseIds = 0; | ||
@@ -17,9 +18,22 @@ class Client { | ||
let socket = new websocket_1.w3cwebsocket(url); | ||
yield effection_1.spawn(function* detectStartupError() { | ||
let [error] = yield events_1.once(socket, 'error'); | ||
if (isErrorEvent(error)) { | ||
throw new errors_1.NoServerError(`Could not connect to server at ${url}`); | ||
} | ||
else { | ||
throw error; | ||
} | ||
}); | ||
let client = new Client(socket); | ||
let res = yield effection_1.resource(client, function* () { | ||
yield effection_2.ensure(() => socket.close()); | ||
let [{ reason, code }] = yield events_1.once(socket, 'close'); | ||
if (code !== 1000) { | ||
throw new Error(`websocket server closed connection unexpectedly: [${code}] ${reason}`); | ||
try { | ||
let [{ reason, code }] = yield events_1.once(socket, 'close'); | ||
if (code !== 1000) { | ||
throw new Error(`websocket server closed connection unexpectedly: [${code}] ${reason}`); | ||
} | ||
} | ||
finally { | ||
socket.close(); | ||
} | ||
}); | ||
@@ -29,17 +43,17 @@ yield events_1.once(socket, 'open'); | ||
} | ||
*query(source) { | ||
let subscription = yield this.send("query", source, false); | ||
*query(source, variables) { | ||
let subscription = yield this.send("query", source, false, variables); | ||
return yield subscription.receive(); | ||
} | ||
*mutation(source) { | ||
let subscription = yield this.send("mutation", source); | ||
*mutation(source, variables) { | ||
let subscription = yield this.send("mutation", source, false, variables); | ||
return yield subscription.receive(); | ||
} | ||
*liveQuery(source) { | ||
return yield this.send("query", source, true); | ||
*liveQuery(source, variables) { | ||
return yield this.send("query", source, true, variables); | ||
} | ||
*subscription(source) { | ||
return yield this.send("subscription", source); | ||
*subscription(source, variables) { | ||
return yield this.send("subscription", source, false, variables); | ||
} | ||
*send(type, source, live = false) { | ||
*send(type, source, live = false, variables) { | ||
let mailbox = new effection_2.Mailbox(); | ||
@@ -50,3 +64,3 @@ let { socket } = this; | ||
let responseId = `${responseIds++}`; //we'd want a UUID to avoid hijacking? | ||
socket.send(JSON.stringify({ [type]: source, live, responseId })); | ||
socket.send(JSON.stringify({ [type]: source, live, responseId, variables })); | ||
while (true) { | ||
@@ -78,2 +92,5 @@ let { value: [event] } = yield messages.next(); | ||
exports.Client = Client; | ||
function isErrorEvent(error) { | ||
return error.type === 'error'; | ||
} | ||
//# sourceMappingURL=client.js.map |
@@ -0,1 +1,2 @@ | ||
export declare type Variables = Record<string, unknown>; | ||
export interface Message { | ||
@@ -5,2 +6,3 @@ query?: string; | ||
subscription?: string; | ||
variables?: Variables; | ||
responseId?: string; | ||
@@ -7,0 +9,0 @@ } |
import { Operation } from 'effection'; | ||
import { Mailbox } from '@bigtest/effection'; | ||
import { Variables } from './protocol'; | ||
export declare class Client { | ||
@@ -7,7 +8,7 @@ private socket; | ||
static create(url: string): Operation<Client>; | ||
query(source: string): Operation; | ||
mutation(source: string): Operation; | ||
liveQuery(source: string): Operation<Mailbox>; | ||
subscription(source: string): Operation<Mailbox>; | ||
query(source: string, variables?: Variables): Operation; | ||
mutation(source: string, variables?: Variables): Operation; | ||
liveQuery(source: string, variables?: Variables): Operation<Mailbox>; | ||
subscription(source: string, variables?: Variables): Operation<Mailbox>; | ||
private send; | ||
} |
import { w3cwebsocket } from 'websocket'; | ||
import { resource } from 'effection'; | ||
import { ensure, Mailbox } from '@bigtest/effection'; | ||
import { resource, spawn } from 'effection'; | ||
import { Mailbox } from '@bigtest/effection'; | ||
import { on, once } from '@effection/events'; | ||
import { isErrorResponse, isDataResponse, isDoneResponse } from './protocol'; | ||
import { NoServerError } from './errors'; | ||
let responseIds = 0; | ||
@@ -13,9 +14,22 @@ export class Client { | ||
let socket = new w3cwebsocket(url); | ||
yield spawn(function* detectStartupError() { | ||
let [error] = yield once(socket, 'error'); | ||
if (isErrorEvent(error)) { | ||
throw new NoServerError(`Could not connect to server at ${url}`); | ||
} | ||
else { | ||
throw error; | ||
} | ||
}); | ||
let client = new Client(socket); | ||
let res = yield resource(client, function* () { | ||
yield ensure(() => socket.close()); | ||
let [{ reason, code }] = yield once(socket, 'close'); | ||
if (code !== 1000) { | ||
throw new Error(`websocket server closed connection unexpectedly: [${code}] ${reason}`); | ||
try { | ||
let [{ reason, code }] = yield once(socket, 'close'); | ||
if (code !== 1000) { | ||
throw new Error(`websocket server closed connection unexpectedly: [${code}] ${reason}`); | ||
} | ||
} | ||
finally { | ||
socket.close(); | ||
} | ||
}); | ||
@@ -25,17 +39,17 @@ yield once(socket, 'open'); | ||
} | ||
*query(source) { | ||
let subscription = yield this.send("query", source, false); | ||
*query(source, variables) { | ||
let subscription = yield this.send("query", source, false, variables); | ||
return yield subscription.receive(); | ||
} | ||
*mutation(source) { | ||
let subscription = yield this.send("mutation", source); | ||
*mutation(source, variables) { | ||
let subscription = yield this.send("mutation", source, false, variables); | ||
return yield subscription.receive(); | ||
} | ||
*liveQuery(source) { | ||
return yield this.send("query", source, true); | ||
*liveQuery(source, variables) { | ||
return yield this.send("query", source, true, variables); | ||
} | ||
*subscription(source) { | ||
return yield this.send("subscription", source); | ||
*subscription(source, variables) { | ||
return yield this.send("subscription", source, false, variables); | ||
} | ||
*send(type, source, live = false) { | ||
*send(type, source, live = false, variables) { | ||
let mailbox = new Mailbox(); | ||
@@ -46,3 +60,3 @@ let { socket } = this; | ||
let responseId = `${responseIds++}`; //we'd want a UUID to avoid hijacking? | ||
socket.send(JSON.stringify({ [type]: source, live, responseId })); | ||
socket.send(JSON.stringify({ [type]: source, live, responseId, variables })); | ||
while (true) { | ||
@@ -73,2 +87,5 @@ let { value: [event] } = yield messages.next(); | ||
} | ||
function isErrorEvent(error) { | ||
return error.type === 'error'; | ||
} | ||
//# sourceMappingURL=client.js.map |
@@ -0,1 +1,2 @@ | ||
export declare type Variables = Record<string, unknown>; | ||
export interface Message { | ||
@@ -5,2 +6,3 @@ query?: string; | ||
subscription?: string; | ||
variables?: Variables; | ||
responseId?: string; | ||
@@ -7,0 +9,0 @@ } |
import { Operation } from 'effection'; | ||
import { Mailbox } from '@bigtest/effection'; | ||
import { Variables } from './protocol'; | ||
export declare class Client { | ||
@@ -7,7 +8,7 @@ private socket; | ||
static create(url: string): Operation<Client>; | ||
query(source: string): Operation; | ||
mutation(source: string): Operation; | ||
liveQuery(source: string): Operation<Mailbox>; | ||
subscription(source: string): Operation<Mailbox>; | ||
query(source: string, variables?: Variables): Operation; | ||
mutation(source: string, variables?: Variables): Operation; | ||
liveQuery(source: string, variables?: Variables): Operation<Mailbox>; | ||
subscription(source: string, variables?: Variables): Operation<Mailbox>; | ||
private send; | ||
} |
@@ -0,1 +1,2 @@ | ||
export declare type Variables = Record<string, unknown>; | ||
export interface Message { | ||
@@ -5,2 +6,3 @@ query?: string; | ||
subscription?: string; | ||
variables?: Variables; | ||
responseId?: string; | ||
@@ -7,0 +9,0 @@ } |
{ | ||
"name": "@bigtest/client", | ||
"version": "0.2.0-c3de7466", | ||
"version": "0.2.0-c7597659", | ||
"description": "Communicate with a BigTest Orchestrator", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26267
31
431