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

Commit

Permalink
Merge pull request #272 from DxCx/deprecate-api
Browse files Browse the repository at this point in the history
[BREAKING CHANGE]: Cleanup for deprecated API
  • Loading branch information
NeoPhi authored Sep 14, 2017
2 parents 71dde80 + 673ad96 commit aefd199
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 693 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### vNEXT
- Add support for GraphQL 0.11.0 [PR #261](https://github.com/apollographql/subscriptions-transport-ws/pull/261)
- **BREAKING CHANGE**: Remove support for Subscription Manager [PR #261](https://github.com/apollographql/subscriptions-transport-ws/pull/261)
- **BREAKING CHANGE**: Remove support for all deprecated API [PR #272](https://github.com/apollographql/subscriptions-transport-ws/pull/272)

### 0.8.3
- docs(README): Fix options example for subscribe methods [PR #266](https://github.com/apollographql/subscriptions-transport-ws/pull/266)
Expand Down
45 changes: 3 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,12 @@ ReactDOM.render(
- `webSocketImpl?: Object` - optional, WebSocket implementation. use this when your environment does not have a built-in native WebSocket (for example, with NodeJS client)

### Methods
#### `subscribe(options, handler) => number`: returns the operation id that identifies the operation
- `options: {SubscriptionOptions}`
#### `request(options) => Observable<ExecutionResult>`: returns observable to execute the operation.
- `options: {OperationOptions}`
* `query: string` : GraphQL subscription
* `variables: Object` : GraphQL subscription variables
* `operationName: string` : operation name of the subscription
* `context: Object` : use to override context for a specific call
- `handler: (errors: Error[], result?: any) => void` : function to handle any errors and results from the subscription response

#### `unsubscribe(id) => void` - unsubscribes from a specific subscription
- `id: string` : the subscription ID of the subscription to unsubscribe from

#### `unsubscribeAll() => void` - unsubscribes from all active subscriptions.

Expand Down Expand Up @@ -269,61 +265,26 @@ ReactDOM.render(
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.

### `query(options: OperationOptions) => Promise<ExecutionResult>` : Executes GraphQL operation over the WebSocket
- `options: OperationOptions`:
* `query: string` - GraphQL operation as string or parsed GraphQL document node
* `variables?: Object` - Object with GraphQL variables
* `operationName?: string` - GraphQL operation name
* `context?: any` - Execution context for the operation

### `close() => void` - closes the WebSocket connection manually, and ignores `reconnect` logic if it was set to `true`.

### `use(middlewares: MiddlewareInterface[]) => SubscriptionClient` - adds middleware to modify `OperationOptions` per each request
- `middlewares: MiddlewareInterface[]` - Array contains list of middlewares (implemented `applyMiddleware` method) implementation, the `SubscriptionClient` will use the middlewares to modify `OperationOptions` for every operation

### `status: number` : returns the current socket's `readyState`

#### **@deprecated** `onConnect(callback, thisContext) => Function` - shorthand for `.on('connect', ...)`
- `callback: Function`: function to be called when websocket connects and initialized.
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.

#### **@deprecated** `onReconnect(callback, thisContext) => Function` - shorthand for `.on('reconnect', ...)`
- `callback: Function`: function to be called when websocket re-connects and initialized.
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.

#### **@deprecated** `onDisconnect(callback, thisContext) => Function` - shorthand for `.on('disconnect', ...)`
- `callback: Function`: function to be called when websocket disconnects.
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.


## Client-side helpers

#### `addGraphQLSubscriptions(networkInterface, wsClient) => void`
- `networkInterface: any` - network interface to extend with `subscribe` and `unsubscribe` methods.
- `wsClient: SubscriptionClient` - network interface to extend with `subscribe` and `unsubscribe` methods.

A quick way to add the `subscribe` and `unsubscribe` functions to the [network interface](http://dev.apollodata.com/core/network.html#createNetworkInterface), when using Hybrid socket mode.


## SubscriptionServer
### `Constructor(options, socketOptions)`
- `options: {ServerOptions}`
* `rootValue?: any` : Root value to use when executing GraphQL root operations
* `schema?: GraphQLSchema` : GraphQL schema object
* `execute?: (schema, document, rootValue, contextValue, variableValues, operationName) => Promise<ExecutionResult> | AsyncIterator<ExecutionResult>` : GraphQL `execute` function, provide the default one from `graphql` package. Return value of `AsyncItrator` is also valid since this package also support reactive `execute` methods.
* `subscribe?: (schema, document, rootValue, contextValue, variableValues, operationName) => Promise<AsyncIterator<ExecutionResult>>` : GraphQL `subscribe` function, provide the default one from `graphql` package.
* `subscribe?: (schema, document, rootValue, contextValue, variableValues, operationName) => Promise<ExecutionResult | AsyncIterator<ExecutionResult>>` : GraphQL `subscribe` function, provide the default one from `graphql` package.
* `onOperation?: (message: SubscribeMessage, params: SubscriptionOptions, webSocket: WebSocket)` : optional method to create custom params that will be used when resolving this operation
* `onOperationComplete?: (webSocket: WebSocket, opId: string)` : optional method that called when a GraphQL operation is done (for query and mutation it's immeditaly, and for subscriptions when unsubscribing)
* `onConnect?: (connectionParams: Object, webSocket: WebSocket)` : optional method that called when a client connects to the socket, called with the `connectionParams` from the client, if the return value is an object, its elements will be added to the context. return `false` or throw an exception to reject the connection. May return a Promise.
* `onDisconnect?: (webSocket: WebSocket)` : optional method that called when a client disconnects
* `keepAlive?: number` : optional interval in ms to send `KEEPALIVE` messages to all clients


* **@deprecated** `onSubscribe?: (message: SubscribeMessage, params: SubscriptionOptions, webSocket: WebSocket)` : optional method to create custom params that will be used when resolving this subscription
* **@deprecated** `onUnsubscribe?: (webSocket: WebSocket)` : optional method that called when a client unsubscribe

- `socketOptions: {WebSocket.IServerOptions}` : options to pass to the WebSocket object (full docs [here](https://github.com/websockets/ws/blob/master/doc/ws.md))
* `server?: HttpServer` - existing HTTP server to use (use without `host`/`port`)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"devDependencies": {
"@types/chai": "^4.0.0",
"@types/graphql": "^0.11.2",
"@types/graphql": "^0.11.3",
"@types/lodash": "^4.14.68",
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.8",
Expand Down
111 changes: 12 additions & 99 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import { GRAPHQL_WS } from './protocol';
import { WS_TIMEOUT } from './defaults';
import MessageTypes from './message-types';

export * from './helpers';

export interface Observer<T> {
next?: (value: T) => void;
error?: (error: Error) => void;
Expand Down Expand Up @@ -180,11 +178,7 @@ export class SubscriptionClient {
) {
const observer = getObserver(observerOrNext, onError, onComplete);

opId = executeOperation({
query: request.query,
variables: request.variables,
operationName: request.operationName,
}, function (error: Error[], result: any) {
opId = executeOperation(request, (error: Error[], result: any) => {
if ( error === null && result === null ) {
if ( observer.complete ) {
observer.complete();
Expand Down Expand Up @@ -212,55 +206,6 @@ export class SubscriptionClient {
};
}

/**
* @deprecated This method will become deprecated in the next release.
* request should be used.
*/
public query(options: OperationOptions): Promise<ExecutionResult> {
return new Promise((resolve, reject) => {
const handler = (error: Error[], result?: any) => {
if (result) {
resolve(result);
} else {
reject(error);
}
};

// NOTE: as soon as we move into observables, we don't need to wait GQL_COMPLETE for queries and mutations
this.executeOperation(options, handler);
});
}

/**
* @deprecated This method will become deprecated in the next release.
* request should be used.
*/
public subscribe(options: OperationOptions, handler: (error: Error[], result?: any) => void) {
const legacyHandler = (error: Error[], result?: any) => {
let operationPayloadData = result && result.data || null;
let operationPayloadErrors = result && result.errors || null;

if (error) {
operationPayloadErrors = error;
operationPayloadData = null;
}

if (error !== null || result !== null) {
handler(operationPayloadErrors, operationPayloadData);
}
};

if (this.client === null) {
this.connect();
}

if (!handler) {
throw new Error('Must provide an handler.');
}

return this.executeOperation(options, legacyHandler);
}

public on(eventName: string, callback: ListenerFn, context?: any): Function {
const handler = this.eventEmitter.on(eventName, callback, context);

Expand All @@ -269,36 +214,6 @@ export class SubscriptionClient {
};
}

/**
* @deprecated This method will become deprecated in the next release.
* You can use onConnecting and onConnected instead.
*/
public onConnect(callback: ListenerFn, context?: any): Function {
this.logWarningOnNonProductionEnv('This method will become deprecated in the next release. ' +
'You can use onConnecting and onConnected instead.');
return this.onConnecting(callback, context);
}

/**
* @deprecated This method will become deprecated in the next release.
* You can use onDisconnected instead.
*/
public onDisconnect(callback: ListenerFn, context?: any): Function {
this.logWarningOnNonProductionEnv('This method will become deprecated in the next release. ' +
'You can use onDisconnected instead.');
return this.onDisconnected(callback, context);
}

/**
* @deprecated This method will become deprecated in the next release.
* You can use onReconnecting and onReconnected instead.
*/
public onReconnect(callback: ListenerFn, context?: any): Function {
this.logWarningOnNonProductionEnv('This method will become deprecated in the next release. ' +
'You can use onReconnecting and onReconnected instead.');
return this.onReconnecting(callback, context);
}

public onConnected(callback: ListenerFn, context?: any): Function {
return this.on('connected', callback, context);
}
Expand All @@ -319,13 +234,6 @@ export class SubscriptionClient {
return this.on('reconnecting', callback, context);
}

public unsubscribe(opId: string) {
if (this.operations[opId]) {
delete this.operations[opId];
this.sendMessage(opId, MessageTypes.GQL_STOP, undefined);
}
}

public unsubscribeAll() {
Object.keys(this.operations).forEach( subId => {
this.unsubscribe(subId);
Expand Down Expand Up @@ -369,6 +277,10 @@ export class SubscriptionClient {
}

private executeOperation(options: OperationOptions, handler: (error: Error[], result?: any) => void): string {
if (this.client === null) {
this.connect();
}

const opId = this.generateOperationId();
this.operations[opId] = { options: options, handler };

Expand Down Expand Up @@ -436,12 +348,6 @@ export class SubscriptionClient {
}
}

private logWarningOnNonProductionEnv(warning: string) {
if (process && process.env && process.env.NODE_ENV !== 'production') {
console.warn(warning);
}
}

private checkOperationOptions(options: OperationOptions, handler: (error: Error[], result?: any) => void) {
const { query, variables, operationName } = options;

Expand Down Expand Up @@ -694,4 +600,11 @@ export class SubscriptionClient {
throw new Error('Invalid message type!');
}
}

private unsubscribe(opId: string) {
if (this.operations[opId]) {
delete this.operations[opId];
this.sendMessage(opId, MessageTypes.GQL_STOP, undefined);
}
}
}
26 changes: 0 additions & 26 deletions src/helpers.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './client';
export * from './server';
export * from './helpers';
export * from './message-types';
export * from './protocol';
11 changes: 0 additions & 11 deletions src/legacy/define-deprecation-function-wrapper.ts

This file was deleted.

Loading

0 comments on commit aefd199

Please sign in to comment.