Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
feat(NA): applied changes according pull request review.
Browse files Browse the repository at this point in the history
feat(NA): applied changes according pull request review..

feat(NA): added static method for instance creation.

refact(NA): change the class names back to subscriptionserver and subscriptionclient.

feat(NA): applied changes according pull request review.

feat(NA): added static method for instance creation. fix(NA): fixed error thrown when we try to print and query its already a string.

refact(NA): change the class names back to subscriptionserver and subscriptionclient.

feat(NA): added new helper function to fully extend network interface with ws transport. fix(NA): built legacy messages on server side and other bad object accesses.

feat(NA): enclose wsclient creation inside addGraphQLWS function.
  • Loading branch information
mistic committed Apr 20, 2017
1 parent 98a7f52 commit 0fedbd3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
17 changes: 12 additions & 5 deletions src/client.ts
Expand Up @@ -6,12 +6,14 @@ import * as Backoff from 'backo2';
import { EventEmitter, ListenerFn } from 'eventemitter3';
import isString = require('lodash.isstring');
import isObject = require('lodash.isobject');
import { ExecutionResult, print } from 'graphql';
import { ExecutionResult, print, getOperationAST } from 'graphql';

import MessageTypes from './message-types';
import { GRAPHQL_WS } from './protocol';
import { WS_TIMEOUT } from './defaults';

export * from './helpers';

export interface RequestOptions {
query: string;
variables?: Object;
Expand All @@ -38,7 +40,7 @@ export interface ClientOptions {
connectionCallback?: (error: Error[], result?: any) => void;
}

export class GraphQLTransportWSClient {
export class SubscriptionClient {
public client: any;
public requests: Requests;
private url: string;
Expand Down Expand Up @@ -159,11 +161,11 @@ export class GraphQLTransportWSClient {
}

if (
!isString(query) ||
( !isString(query) && !getOperationAST(query, operationName)) ||
( operationName && !isString(operationName)) ||
( variables && !isObject(variables))
) {
throw new Error('Incorrect option types. `query` must be a string,' +
throw new Error('Incorrect option types. `query` must be a string or a document,' +
'`operationName` must be a string, and `variables` must be an object.');
}

Expand All @@ -175,7 +177,12 @@ export class GraphQLTransportWSClient {
}

private buildMessage(id: number, type: string, payload: any) {
const payloadToReturn = payload && payload.query ? { ...payload, query: print(payload.query) } : payload;
const payloadToReturn = payload && payload.query ?
{
...payload,
query: typeof payload.query === 'string' ? payload.query : print(payload.query),
} :
payload;

return {
id,
Expand Down
27 changes: 19 additions & 8 deletions src/helpers.ts
@@ -1,25 +1,36 @@
import { GraphQLTransportWSClient } from './client';
import { print } from 'graphql/language/printer';
import { SubscriptionClient, ClientOptions } from './client';
import { ExecutionResult } from 'graphql';

/**
* @deprecated This method becomes deprecated in the new package graphql-transport-ws.
* @deprecated This method will become deprecated in the new package graphql-transport-ws.
* Start using the GraphQLTransportWSClient to make queries, mutations and subscriptions over websockets.
*/

// Quick way to add the subscribe and unsubscribe functions to the network interface
export function addGraphQLSubscriptions(networkInterface: any, wsClient: GraphQLTransportWSClient): any {
// We will move this into a new package in the future
export function addGraphQLSubscriptions(networkInterface: any, wsClient: SubscriptionClient): any {
console.warn('This method becomes deprecated in the new package graphql-transport-ws. Start using the ' +
'GraphQLTransportWSClient to make queries, mutations and subscriptions over websockets.');

return Object.assign(networkInterface, {
subscribe(request: any, handler: any): number {
return wsClient.subscribe({
query: print(request.query),
variables: request.variables,
}, handler);
return wsClient.subscribe(request, handler);
},
unsubscribe(id: number): void {
wsClient.unsubscribe(id);
},
});
}

export function addGraphQLWS(networkInterface: any, wsClientOptions: ClientOptions): any {
if (typeof networkInterface._uri !== 'string') {
throw new Error('Given networkInterface should have an uri defined to config the websocket.');
}

const wsClient = new SubscriptionClient(networkInterface._uri, wsClientOptions);
return Object.assign(addGraphQLSubscriptions(networkInterface, wsClient), {
query(request: any): Promise<ExecutionResult> {
return wsClient.query(request);
},
});
}
12 changes: 8 additions & 4 deletions src/server.ts
Expand Up @@ -46,7 +46,7 @@ export interface ServerOptions {
// triggerGenerator?: (name: string, args: Object, context?: Object) => Array<{name: string, filter: Function}>;
}

export class GraphQLTransportWSServer {
export class SubscriptionServer {
/**
* @deprecated onSubscribe is deprecated, use onRequest instead
*/
Expand All @@ -62,6 +62,10 @@ export class GraphQLTransportWSServer {
private wsServer: WebSocket.Server;
private subscriptionManager: SubscriptionManager;

public static create(options: ServerOptions, socketOptions: WebSocket.IServerOptions) {
return new SubscriptionServer(options, socketOptions);
}

constructor(options: ServerOptions, socketOptions: WebSocket.IServerOptions) {
const {subscriptionManager, onSubscribe, onUnsubscribe, onRequest, onRequestComplete, onConnect, onDisconnect, keepAlive} = options;

Expand Down Expand Up @@ -96,6 +100,7 @@ export class GraphQLTransportWSServer {
const connectionContext: ConnectionContext = Object.create(null);
connectionContext.isLegacy = false;
connectionContext.socket = socket;
connectionContext.requests = {};

// Regular keep alive messages if keepAlive is set
if (keepAlive) {
Expand All @@ -120,7 +125,7 @@ export class GraphQLTransportWSServer {
}

private unsubscribe(connectionContext: ConnectionContext, reqId: string) {
if (connectionContext.requests[reqId]) {
if (connectionContext.requests && connectionContext.requests[reqId]) {
this.subscriptionManager.unsubscribe(connectionContext.requests[reqId]);
delete connectionContext.requests[reqId];
}
Expand Down Expand Up @@ -300,7 +305,7 @@ export class GraphQLTransportWSServer {

// NOTE: The old protocol support should be removed in the future
private parseLegacyProtocolMessage(connectionContext: ConnectionContext, message: any) {
let messageToReturn;
let messageToReturn = message;

switch (message.type) {
case MessageTypes.INIT:
Expand Down Expand Up @@ -352,7 +357,6 @@ export class GraphQLTransportWSServer {
}
break;
default:
messageToReturn = message;
break;
}

Expand Down
2 changes: 1 addition & 1 deletion unpkg-webpack.config.js
Expand Up @@ -6,6 +6,6 @@ module.exports = {
output: {
path: path.join(__dirname, '/browser'),
filename: 'client.js',
library: 'GraphQLTransportWs'
library: 'SubscriptionsTransportWs'
}
};

0 comments on commit 0fedbd3

Please sign in to comment.