Skip to content

Commit

Permalink
fix: connection params
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Jan 19, 2021
1 parent f5071bc commit 3bfba3c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
9 changes: 7 additions & 2 deletions packages/ws/src/Protocol/GQL/GQLProtoHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ export abstract class GQLProtoHandle<C extends IContext,

readonly #connection = new GQLClientConnection(this.getShadowState());

public abstract subscribe(payload: GQLClientPayload): Promisify<AsyncIterableIterator<any>>;
public abstract subscribe(payload: GQLClientPayload,
params: Record<string, any>): Promisify<AsyncIterableIterator<any>>;

public async run(): Promise<void> {
const layer = new GQLProtoLayer(this.#connection, (payload) => this.subscribe(payload));
const layer = new GQLProtoLayer(
this.#connection,
(payload, params) => this.subscribe(payload, params),
);

for await (const operation of this.#connection) {
await layer.handle(operation);
}
Expand Down
12 changes: 7 additions & 5 deletions packages/ws/src/Protocol/GQL/GQLProtoLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class GQLProtoLayer {
readonly #subscribe: GQLSubscribeFunction;
readonly #client: GQLClientConnection;
readonly #subscriptions = new Map<string, AsyncIterableIterator<any>>();
readonly #params: Record<string, any> = {};

constructor(client: GQLClientConnection, factory: GQLSubscribeFunction) {
this.#client = client;
Expand All @@ -30,7 +31,8 @@ export class GQLProtoLayer {
assert(this.isClientOperation(operation), `Wrong the Operation Message`);
switch (operation.type) {
case GQLClientOperationType.CONNECTION_INIT:
this.#client.send({type: GQLServerOperationType.CONNECTION_ACK});
Object.assign(this.#params, operation.payload);
await this.#client.send({type: GQLServerOperationType.CONNECTION_ACK});
break;
case GQLClientOperationType.CONNECTION_TERMINATE:
this.terminate();
Expand All @@ -49,18 +51,18 @@ export class GQLProtoLayer {

private async createSubscription(id: string, payload: GQLClientPayload): Promise<void> {
try {
const subscription = await this.#subscribe(payload);
const subscription = await this.#subscribe(payload, this.#params);
assert(isFunction(subscription.return), "AsyncIterator should be cancelable");

this.#subscriptions.set(id, subscription);
for await (const next of subscription) {
this.#client.send({id, payload: next, type: GQLServerOperationType.DATA});
await this.#client.send({id, payload: next, type: GQLServerOperationType.DATA});
}
} catch (error) {
this.#client.send({id, payload: this.serializeError(error), type: GQLServerOperationType.ERROR});
await this.#client.send({id, payload: this.serializeError(error), type: GQLServerOperationType.ERROR});
}

this.#client.send({id, type: GQLServerOperationType.COMPLETE});
await this.#client.send({id, type: GQLServerOperationType.COMPLETE});
}

private isClientOperation(operation: GQLOperationMessage): operation is GQLClientOperation {
Expand Down
3 changes: 2 additions & 1 deletion packages/ws/src/Protocol/GQL/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ export type GQLServerOperation = IGQLOperationConnectionAsk

export type GQLOperationMessage = GQLClientOperation | GQLServerOperation;

export type GQLSubscribeFunction = (payload: GQLClientPayload) => Promisify<AsyncIterableIterator<any>>;
export type GQLSubscribeFunction = (payload: GQLClientPayload,
params: Record<string, any>) => Promisify<AsyncIterableIterator<any>>;

0 comments on commit 3bfba3c

Please sign in to comment.